Search code examples
c++visual-studiodependenciessolution

Importing a library in MS Visual Studio from the same solution


So I have my solution file here, which contains 4 projects.

  • Project A is a library which gets compiled into a .lib.
  • Project B is a program which becomes an .exe.

I have set up Project A as dependency for Project B, and I included

#pragma comment(lib,"terrain.lib")

into the file in Project B which uses the library.

In spite of both projects being compiled into the same Debug / Release folder, MSVCC tells me

1>LINK : fatal error LNK1104: File "terrain.lib" could not be opened.
// <freely translated from German, could mean "not found">

This:

#pragma comment(lib,"../Debug/terrain.lib")

works, but then I have to change it for release.

Is the only valid way for this using #ifdef ?


Solution

  • You need to specify library path in project settings ("additional library directories" in linker settings - at least in vc2008), for both debug and release configuration. You can use macros like ${ConfigurationName} and ${SolutionDir}, so specifying paths within project should be easy enough.

    Also it might be a better idea to include libraries using linker settings instead of #pragma comment.