I'm trying to link some parts of a static library into a program written in C++ using g++ under Linux.
my_lib.h
#ifdef USE_EXTERN_LIB
# include <extern_lib.h>
void do_something (struct extern_lib);
#endif
void do_other (int);
my_lib.c
#include "my_lib.h"
#ifdef USE_EXTERN_LIB
void do_something (struct extern_lib l)
{
// do something
}
#endif
void do_other (int a)
{
// do something
}
I'm statically creating libmy_lib.a with -DUSE_EXTERN_LIB preprocessor flag to include all into it.
but what I want to do is to create two programs: one that uses this library with *extern_lib* and one that use it without *extern_lib*, i.e.:
g++ -L/path/to/lib -lmy_lib -o prog_wihtout_lib prog_without_lib.cc
g++ -DUSE_EXTERN_LIB -L/path/to/lib -lmy_lib -o prog_with_lib prog_with_lib.cc
The second program compiles but not the first, it says that extern_lib is undeclared.
With a dynamic library, there is no problem because symbols are loaded at runtime but I want a static library. Is there a way to link only desired modules of a static lib?
EDIT
prog_without_lib.cc
#include "my_lib.h"
int main ()
{
do_other (42);
return 0;
}
prog_with_lib.cc
#include "my_lib.h"
int main ()
{
do_other (42);
struct extern_lib l;
do_something (l);
return 0;
}
Thanks.