Conditionally link a library

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
 
C

Carl Daniel [VC++ MVP]

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
 
B

Ben Voigt

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
 

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

Top