1

I don't understand how Visual Studio (2015 for me) can detect the .lib file name from the header file.

Example 1
I use boost/asioto write a simple program to connect via HTTP request. The source file includes <boost/asio.hpp> on top of it. I also config C/C++ / General / Additional Include Directories to point to boost's folder.
After that, I build the solution and encounter an error:

Error LNK1104 cannot open file 'libboost_system-vc140-mt-gd-1_61.lib'

But I don't know where Visual Studio finds the name libboost_system-vc140-mt-gd-1_61.lib from.
- Looking in Linker / Input / Additional Dependencies / Edit... for both Evaluated Value and Inherited values, there is no item named libboost_system-vc140-mt-gd-1_61.lib
- I think that in <boost/asio.hpp> by somehow it set some information to the compiler that: "If you need lib file, look for libboost_system-vc140-mt-gd-1_61.lib, it is the implementation of mine", but I search in boost folder, there is no source file contain "libboost_system-vc140-mt-gd-1_61.lib"
enter image description here


By generally, if some function declared in a header file, and Visual Studio can't file the implement of it, the LNK2019: unresolved external symbol ... will be throw.
Then if its implement is in abcxyz.lib file, the user goes to Linker / Input / Additional Dependencies and add abcxyz.lib, without specifying the folder contain the .lib file in Linker / General / Additional Libary Directories. In this situation, the Error LNK1104 cannot open file 'abcxyz.lib' will be thrown

So, why the above example encounter the LNK1104 error instead of LNK2019 error?

Example 2
I'm working for a project that use OpenCV 2.4.10 library. In the distribution, they give me the project with source code but without attaching the library to me.
Then, I download the latest OpenCV 2.4.13 and set up for the project, such as additional include folder, additional library folder, Additional Dependencies (opencv_core2413.lib).
Everything looks like ok, but when I build the project, it gives me the error:

Error LNK1104 cannot open file 'opencv_core2410d.lib'

I have looked at Linker \ Input \ Additional Dependencies on both Evaluated value (contain opencv_core2413.lib) and Inherited value, but there is no item named 'opencv_core2410d.lib'.
Try looking in another shared Property Sheet that the project apply but there is no entry item named 'opencv_core2410d.lib'
I want to delete this entry but can't find where it is

So, what's happen here? Any one can explain for me? Thanks

kenba
  • 4,303
  • 1
  • 23
  • 40
phibao37
  • 2,230
  • 4
  • 26
  • 35
  • 1
    `libboost_system-vc140-mt-gd-1_61.lib` comes from `#pragma comment(lib libboost_system-vc140-mt-gd-1_61.lib)` there is an option in boost to turn that off. http://www.boost.org/doc/libs/1_61_0/boost/config/auto_link.hpp – drescherjm Jul 17 '16 at 15:24
  • `opencv_core2410d.lib` is the debug import library needed for debug builds. – drescherjm Jul 17 '16 at 15:27
  • 1
    Here is more on the boost usage of #pragma comment(lib libname): http://stackoverflow.com/questions/5184889/how-does-the-c-linker-know-which-lib-contains-which-functions – drescherjm Jul 17 '16 at 15:30
  • I know that, but I can't find where it come from. I'm using OpenCV 2.4.13 library + 2.4.13 .lib file. May someone in old project add `#pragma comment (lib opencv_core2410d.lib)` like above and the compiler will look for it? – phibao37 Jul 17 '16 at 15:31
  • Thanks for your help, @drescherjm – phibao37 Jul 17 '16 at 15:34
  • 1
    Yes if someone added `#pragma comment (lib opencv_core2410d.lib)` the linker will look for it. Although it will not find it if it is not in the paths that the linker is searching for libraries. Also since `opencv_core2410d.lib` is an import library and you are using Visual Studio 2015 make sure you are using opencv binaries from the vc14 folder. You can not safely use opencv compiled with any other compiler or a different version of Visual Studio. – drescherjm Jul 17 '16 at 16:31

2 Answers2

1

LNK1104 lets you know that you need to set the library location in the Library Directories.

boost::asio depends on boost::system for error messages. boost::system is not a header-only library.

You need to add the location of the boost libraries to the Library Directories under VC++ Directories on your solution's property pages.

If you haven't built the boost libraries yet, there's an article how to do it here or you can download it from here.

Adding the location of the OpenCV libraries to your solution's property pages should fix Example 2.

Community
  • 1
  • 1
kenba
  • 4,303
  • 1
  • 23
  • 40
0

With visual studio, you can inject into a .obj file a library request using

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

This causes the linker to look for libname in the library path, and ensures the correct library is used.

This can be disabled using /nodefaultlib on the link.

With windows, the implementation of classes changes between release and debug (extra tracking members are added to structures in debug).

The mechanism for implementing tidy up and CRT startup is different depending on whether a DllMain gets called or not.

More recent visual studios also have the

#pragma detect_mismatch( "name", "value")

Which puts a declaration in each object file with its value for "value". This ensures if structures change between version or compile options, they become incompatible with each other.

mksteve
  • 12,614
  • 3
  • 28
  • 50