2

This is probably one of the sillier questions on SO, but I can't solve it.

I've got a solution containing three projects A, B and C. A and B are static lib projects, and C is an exe project which links with A and B. I've set up the build directories so that the build artifacts for each project are located as follows:

A: $(SolutionDir)\$(PlatformName)\$(Configuration)\Lib\A.lib
B: $(SolutionDir)\$(PlatformName)\$(Configuration)\Lib\B.lib
C: $(SolutionDir)\$(PlatformName)\$(Configuration)\Bin\C.exe

Since the exe built in project C expects the libs in A and B, I've added libraries A.lib and B.lib to project C Properties->Linker->General->Additional Dependencies. I've added the directory locations for A and B exactly as listed above to Project Properties->Linker->General->Additional Libraries Directories.

When linking, I get the linker error LNK1104: cannot open file A.lib

Can anyone help? I've got some experince eof using VS, but this is the first time I've built a multi-project solution from scratch.

Many thanks.

Paul
  • 443
  • 8
  • 18

1 Answers1

0

When you say you've added the directories exactly as listed, do you mean with the filename too? Because only the directory part should be present, i.e. $(SolutionDir)\$(PlatformName)\$(Configuration)\Lib.

Apart from that, you need to make sure the projects are actually being built in the correct order. MSBuild will happily try to compile C before (or in parallel with) A and B otherwise. To do so, either make use of the References property page (under Project->Properties->Common Properties), or manually force it using the Project Dependencies dialog (Project->Project Dependencies...).

I should mention that using the project references mechanism is the "new" and recommended way to manage dependencies -- it's supposed to automatically add the outputs of the referenced projects as input dependencies on the referencing project, without you having to manually specify them. I've not always had much luck getting that part to work in the past myself, but it certainly does at least configure the dependency chain properly.

For completeness, note that a .lib file will only be generated if there's any symbols actually contained within the compiled project (so, if your project is empty or otherwise defines no symbols, compiling it will not produce a .lib file, breaking the build of all dependent projects).

Cameron
  • 96,106
  • 25
  • 196
  • 225
  • I solved it using the #pragma comment( lib, "path_to_library" ) inside a file library.lib.h, including this file inside a file ReferencedLibraries.cpp, and including this last file in the exe project. I needed to use the following to account for Release/Debug builds: `#ifdef _DEBUG #pragma comment ( lib, "path-to-debug-library.lib" ) #else #pragma comment ( lib, "path-to-release-library.lib" ) #endif` – Paul Jul 17 '15 at 15:48
  • @Paul: You shouldn't have had to do that. That's equivalent to adding the lib path in "Linker -> Input -> Additional Dependencies" for the project. – Cameron Jul 17 '15 at 16:49
  • Thanks for your replies. I'll try your other method of using project references. – Paul Jul 18 '15 at 00:58