Andrew Florko
Andrew Florko

Reputation: 7750

C++. Error LNK1104. Cannot open library file for the first time solution is built

I have legacy MSVS solution with C++ projects. When I take it from Version Control System and try to build it for the very first time (when it's clean) - I get error: LNK 1104 library can't be found.

When I try to build it for the second time - everything is built ok.

Project "A" (C++) depends on library (lib file) that is Project "B" (also C++).

"A" has code like that in one of it's cpp files (may be stdafx.h is the better place for #pragma comments?):

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

that causes Linker error for the very first built

I investigated the first built results: "B" project is successfully compiled, "lib" file is produced, and "A" project (that depends on "B") is the last project in solutions projects build order.

I wonder why "A" doesn't see "B" library for the very first time as "B" should be built earlier than "A". When I build the same solution for the second time - no linker errors.

Upvotes: 3

Views: 4629

Answers (2)

Luchian Grigore
Luchian Grigore

Reputation: 258598

This has to do with the build order. If project A is built first, B.lib doesn't yet exist. So you get the error. Then project B is built, and B.lib generated. When you build again, as the library exists, the error is gone.

To solve this, you need to tell the solution that A depends on B. Right click on the solution, go to project dependencies, select A in "Projects" and check B in "Depends on". Something like:

enter image description here

Upvotes: 1

Itsik
Itsik

Reputation: 3930

Right Click on Project A > Project Dependencies
Check Project B

VS doesn't deduct the dependencies from the code, you need to tell it explicitly

Upvotes: 1

Related Questions