Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio C++ - Check the reason of link a specific library

vs report a error such as:

  1. can not find xxx.lib
    How do I check out why vs need to link xxx.lib? Is there a trace log? My project have not use boost.regex, but vs report a error says can not find regex.lib. So I want to find out which part of code refer to regex

  2. LNK error: LIBCMT.lib: xxx was already defined in LIBCMTD.lib
    How do I check why vs also link yyy.lib even this is a debug build? I have 2 projects, they link to same libs, all libs and project itself was /MTd. But one of them will report above error, I think it shouldn't link LIBCMT.lib because it is a release version lib, and another project is OK so the lib file was build correctly

VS can show the link trace?

like image 518
jean Avatar asked Jan 12 '23 06:01

jean


2 Answers

1) Let's start with how the linker actually knows what to link with. Basically there are 2 categories:

  • Libraries specified in the project settings in the linker options as additional inputs
  • Libraries added in code with precompiler directive #pragma comment (generally the same thing as above but people have different tastes)

Otherwise you just get information about missing symbols but not the actual library they are from. What you can do to help is under visual studio linker options set Show Progress to For Libraries Searched (or just /VERBOSE:LIB linker flag), that will actually show you what dependencies are added after each lib is loaded, this also helps with point 2) to see which library loads which run-time.

2) Already mentioned in 1) that you can make the linker show library load progress, otherwise if a dynamic C run-time is used in the external library you are linking against you could use Dependency Walker to examine the dependencies of the library and find if the C run-time dll that is needed is debug or release by 'd' suffix in the dll name. If the library is already linked with static run-time then I guess only the linker errors will warn you. But I think that most serious libraries are correctly packed and structured so that you will be able to tell which files contain the debug version and which the release. If the library has only release version, well than that's another story. But i mean still, you can reconfigure the Debug configuration of your project to actually link against the release run-time to satisfy the external library, of course this prevents some debugging features, debug heap, etc.

like image 171
Rudolfs Bundulis Avatar answered Jan 22 '23 00:01

Rudolfs Bundulis


For the missing boost regex library problem, I think the cause is due to boost's default autolink behaviour. If you include one of the headers of some of the boost libraries (not all of them, but regex is one of them), then these will cause Visual Studio to automatically link against the library. It uses a special pragma of the form:

#pragma comment(lib, "regex")

This has the effect of automatically adding a flag to the linker command. However, it's only picked up during compilation and so you won't see it in the project properties. In the case of boost, the solution to this is quite simple - find boost/config/user.hpp and uncomment the line

#define BOOST_ALL_NO_LIB

This will turn off the autolink behaviour for all libraries. Alternatively you can use #define BOOST_REGEX_NO_LIB to change this just for the regex library.

To solve the second problem, you need to find out which library is linking against the release build. Try selecting all projects in the solution and open Properties -> C++ -> Code Generation. You will probably find that the Runtime Library setting will be blank (because the option is different for some of the libraries. Force it to the correct threaded/single-threaded Debug option and rebuild.

like image 41
the_mandrill Avatar answered Jan 22 '23 01:01

the_mandrill