how to convert program to lib?

J

joe

Hi I have a little executable that has this description for the main
procedure
main(int argc, char **argv)
is it possible to call the program from another program if i compile it as
a lib? or do i need to modifiay the main function to a different name? do i
need to create a .h file for the procedure definition? where can i find
info about this? thanks
 
R

Rob Williscroft

joe wrote in
Hi I have a little executable that has this description for the main
procedure
main(int argc, char **argv)
is it possible to call the program from another program if i compile
it as a lib? or do i need to modifiay the main function to a different
name? do i need to create a .h file for the procedure definition?

Yes (to both question's).
where can i find info about this? thanks

In your compilers documentation or online help, compiler specific
newsgroups (there FAQ's if they have them). Also maybe there is
some third party library (with sources preferably) that you can
get for you compiler, if so, take a look see how they do it.


The easy part is rewriting the source code:

rename int main( int argc, char **argv ) to (say (*))

int yourlib_main( int argc, char **argv )
{
// current code.

// note if you may need to add:

return 0; // as main() is special in that you can
// omit its return statement
}


Create a header file:

#if !defined( YOURLIB_GUARD_H )
#define YOURLIB_GUARD_H

#if !defined( __cplusplus )
# error "Must use C++ with yourlib"
#endif

int yourlib_main( int argc, char **argv );

#endif

I'd also suggest while your at it you may want to change the arguments
to yourlib_main() to something easier to create maybe:

#include <vector>
#include <string>

int yourlib_main( std::vector< std::string > const &args );

(*) Replace yourlib/YOURLIB above with something more meaninfull
and unique to this library you are creating.

HTH.

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top