2

For learning purposes, i wanted to create a static library, a "package" of the lib files used in opencv to then link it against my app "opencvuser". Doing so, i get tremendous amounts of erros. (LNK2005 and LNK2019)

My Setup:

Project: staticLib

  • I've created a static library application without precompiled headers.
  • Under librarian i've put D:\OpenCV248\build\x64\vc10\staticlib as an additional library directory. And I've specified all available .lib files as additional dependencies. (opencv_core248d.lib, opencv_imgproc248d.lib, opencv_highgui248d.lib, ...) Source

Project: opencvuser

  • I've added C:\OpenCV240\build\include as an additional include directory
  • Then i've listed "staticLib" under "Properties -> References"

What i expect: Now i should get the same functionallity, as i would add the opencv lib files instead of my built staticLib.lib is my expectation correct?

What i've checked so far:

  • All Projects are x64
  • Runtime-Library is set in both Projects to "Multi-threaded Debug"

Anyone knows if the Runtime-Library setting on the static libraries are set to "Multi-threaded Debug"?

enter image description here

user1767754
  • 23,311
  • 18
  • 141
  • 164
  • Please try this first. http://stackoverflow.com/questions/7583172/opencv-as-a-static-library-cmake-options To be able to compile OpenCV for a static library, the option needs to be configured in the CMake, before it generates the Microsoft Visual Studio solution files for you. If you have already generated these solution files, it will be a hassle to change them to static build. – rwong Mar 09 '14 at 03:04
  • I don't really get it , i am using already the static pre-built libraries of OPENCV to embed it in my custom static libraries. Or do you mean something different? thx for the comment – user1767754 Mar 09 '14 at 10:50
  • 1
    From what i understood you firstly try to compile staticLib which includes all OpenCV lib files, trying to get only one static library. Then you try to reference that file and use OpenCV functions. Am I right ? – yutasrobot Mar 10 '14 at 08:41
  • Exactly, thats right. – user1767754 Mar 10 '14 at 09:00

1 Answers1

1

You are getting those linker errors because the OpenCV libs you are trying to use were statically linked against the CRT. In your project, you are dynamically linking to CRT and these things won't mix. I would recommend that you don't try to create a "package" of all the OpenCV libs and instead just link to the specific libs you need where you need them.

But I am also going to show you how to solve your problem:

  • You need to recompile OpenCV without statically linking to the CRT.

    You can check out the OpenCV documentation for instructions on how to compile OpenCV using CMake and Visual Studio 2010. When you run CMake, after you pressed the "Configure" button, look for an option called "BUILD_WITH_STATIC_CRT" and disable it. Then you can press "Generate", open the solution with VS2010 and compile OpenCV.

  • In your VS2010 project, use the following settings:

    In the "opencvuser" project configuration, under Librarian, additional library directories you need to add the path to where the .lib files that you built are located. For me, it's in "c:\opencv248\mybuild\lib\Debug\". Under Additional Dependencies, you need to include all the OpenCV lib files (opencv_core248d.lib, etc). I also needed to include Comctl32.lib and zlibd.lib because if I didn't I would get some linker errors.

Librarian settings

Here are the dependencies I put in:

opencv_calib3d248d.lib opencv_contrib248d.lib opencv_core248d.lib opencv_features2d248d.lib opencv_flann248d.lib opencv_gpu248d.lib opencv_haartraining_engined.lib opencv_highgui248d.lib opencv_imgproc248d.lib opencv_legacy248d.lib opencv_ml248d.lib opencv_nonfree248d.lib opencv_objdetect248d.lib opencv_ocl248d.lib opencv_photo248d.lib opencv_stitching248d.lib opencv_superres248d.lib opencv_ts248d.lib opencv_video248d.lib opencv_videostab248d.lib Comctl32.lib zlibd.lib

Also, in the "opencvuser" project you need to add an empty .cpp file. If you don't add this file, the solution will be empty and Visual Studio won't compile it. I just added a file called "dummy.cpp" to the project. That file is completely empty. Don't put a "main()" function in it because it will collide with the main function in the other project and you will get an error.

In the "staticlib" project, under Linker->General, Additional library directories, you need to include the path to the opencvuser.lib file. For me, it's "..\debug". Also, under Linker->Input, Additional Dependencies, you need to add the "opencvuser.lib" file.

Linker general Linker input

  • Set project dependencies

    You also need to make sure that the projects are built in the right order (first opencvuser, then staticlib). To do this, right-click on the solution and choose Properties. In that window, under Common Properties->Project dependencies, make sure that "opencvuser" does not have a dependency on "staticlib", but "staticlib" must have a dependency on "opencvuser".

Project dependencies

That's it, now your project should work. Here are the contents of the two files, and the project running.

Contents of the two files Project working

Ove
  • 6,227
  • 2
  • 39
  • 68
  • First of all Thank you for your detailed response. I did all the steps several times, checking every point again and again, but when building the final app (staticLib) i get error ** error LNK2019: unresolved external symbol __imp_CreateToolbarEx referenced in function "int __cdecl icvCreateTrackbar...* (by the way, my staticLib is in your example the opencvuser...but i've noticed this.). Any suggestion? – user1767754 Mar 10 '14 at 11:52
  • CreateToolbarEx is in Comctl32.lib. Are you sure you included comctl32.lib in the Additional Dependencies? (it should be next to all the opencv .lib files). I get the same error if I remove comctl32.lib. – Ove Mar 10 '14 at 12:38
  • It's added, maybe because my target platform is x64 ? – user1767754 Mar 10 '14 at 13:03
  • Yes, that's probably the reason. I compiled my version on 32bit, not 64. I need to investigate some more... – Ove Mar 10 '14 at 13:11
  • I just compiled OpenCV on 64bit using the same steps as above, and I converted the two projects to 64bit and everything is working. I still needed to add the reference to comctl32.lib in order for the code to work. I put my project files on pastebin, please check and see that you are using the same settings as me: [StaticLib.vcxproj](http://pastebin.com/bYQE6Xb2) [opencvuser.vcxproj](http://pastebin.com/pK14KgAV) – Ove Mar 10 '14 at 13:39
  • I will compare your files with mine, by the way, when i compile opencvUser (the "lib package") then i get following warning: **opencv_core248d.lib(opencv_core_pch.obj) : warning LNK4221: This object file does not define any previously undefined public symbols, so it will not be used by any link operation that consumes this library** i get this on 13 Lib files. – user1767754 Mar 10 '14 at 13:56
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49409/discussion-between-ove-and-user1767754) – Ove Mar 10 '14 at 14:28