Conditionally link a library

  • Thread starter Thread starter Felix
  • Start date Start date
F

Felix

Hello,

I use a #define like WITH_MYSTUFF to conditionally compile code within
#ifdef WITH_MYSTUFF. I would now like the same define to conditionally link
with a library. In the linker settings of VS.NET 2003 I have 'Input ->
Additional
Dependencies' where I can add my .lib files the linker should use.

How do I tell the linker (by a macro ?) somthing like this:

'link with mystuff.lib if WITH_MYSTUFF is defined'

Any idea how this could be done?

Many thanks, Felix
 
Felix said:
Hello,

I use a #define like WITH_MYSTUFF to conditionally compile code within
#ifdef WITH_MYSTUFF. I would now like the same define to
conditionally link with a library. In the linker settings of VS.NET
2003 I have 'Input -> Additional
Dependencies' where I can add my .lib files the linker should use.

How do I tell the linker (by a macro ?) somthing like this:

'link with mystuff.lib if WITH_MYSTUFF is defined'

Any idea how this could be done?

Simple answer: you can't.

More complicated answer: Generally, it's not necessary. If nothing in the
library is referenced (and presumably it's not, otherwise you'd be getting
unresolved external errors from the linker), then the linker won't pull
anything from the library into the linked image. It will make the link take
a little longer (a couple seconds at most, unless the library is extremely
large). You can try adding the /verbose option to your linker command line
options to see what happens to a library that's not referenced.

Most complicated answer: you can use another project system, such as a
hand-written makefile, to do your builds. In such an environment you could
decide which libraries to include in the link based on whatever criteria you
choose.

-cd
 
Felix said:
Hello,

I use a #define like WITH_MYSTUFF to conditionally compile code within
#ifdef WITH_MYSTUFF. I would now like the same define to conditionally
link
with a library. In the linker settings of VS.NET 2003 I have 'Input ->
Additional
Dependencies' where I can add my .lib files the linker should use.

How do I tell the linker (by a macro ?) somthing like this:

'link with mystuff.lib if WITH_MYSTUFF is defined'

#if WITH_MYSTUFF
#pragma comment(lib, "mystuff")
#endif
 
Back
Top