6

I built the static openCV 2.3 libraries. My project currently uses the dynamic ones with no problem, but now I want to use static libs. I added the libs to my .pro file:

LIBS += "C:\Program Files\openCV_VS_static\opencv\build\lib\Release\opencv_calib3d231.lib" \
"C:\Program Files\openCV_VS_static\opencv\build\lib\Release\opencv_contrib231.lib" \
"C:\Program Files\openCV_VS_static\opencv\build\lib\Release\opencv_core231.lib" \
"C:\Program Files\openCV_VS_static\opencv\build\lib\Release\opencv_features2d231.lib" \
"C:\Program Files\openCV_VS_static\opencv\build\lib\Release\opencv_flann231.lib" \
"C:\Program Files\openCV_VS_static\opencv\build\lib\Release\opencv_gpu231.lib" \
"C:\Program Files\openCV_VS_static\opencv\build\lib\Release\opencv_haartraining_engine.lib" \
"C:\Program Files\openCV_VS_static\opencv\build\lib\Release\opencv_highgui231.lib" \
"C:\Program Files\openCV_VS_static\opencv\build\lib\Release\opencv_imgproc231.lib" \
"C:\Program Files\openCV_VS_static\opencv\build\lib\Release\opencv_legacy231.lib" \
"C:\Program Files\openCV_VS_static\opencv\build\lib\Release\opencv_ml231.lib" \
"C:\Program Files\openCV_VS_static\opencv\build\lib\Release\opencv_objdetect231.lib" \
"C:\Program Files\openCV_VS_static\opencv\build\lib\Release\opencv_ts231.lib" \
"C:\Program Files\openCV_VS_static\opencv\build\lib\Release\opencv_video231.lib"

And include dirs:

INCLUDEPATH += "C:\Program Files\openCV_VS_static\opencv\build\include"
INCLUDEPATH += "C:\Program Files\openCV_VS_static\opencv\build\include\opencv"

When I try to build I get these errors:

LIBCMT.lib(invarg.obj) : error LNK2005: __invoke_watson already defined in MSVCRT.lib(MSVCR100.dll)
LIBCMT.lib(mlock.obj) : error LNK2005: __unlock already defined in MSVCRT.lib(MSVCR100.dll)
LIBCMT.lib(mlock.obj) : error LNK2005: __lock already defined in MSVCRT.lib(MSVCR100.dll)
LIBCMT.lib(winxfltr.obj) : error LNK2005: __XcptFilter already defined in MSVCRT.lib(MSVCR100.dll)
LIBCMT.lib(crt0dat.obj) : error LNK2005: __initterm_e already defined in MSVCRT.lib(MSVCR100.dll)
LIBCMT.lib(crt0dat.obj) : error LNK2005: _exit already defined in MSVCRT.lib(MSVCR100.dll)
LIBCMT.lib(crt0dat.obj) : error LNK2005: __exit already defined in MSVCRT.lib(MSVCR100.dll)
LIBCMT.lib(crt0dat.obj) : error LNK2005: __cexit already defined in MSVCRT.lib(MSVCR100.dll)
LIBCMT.lib(crt0dat.obj) : error LNK2005: __amsg_exit already defined in MSVCRT.lib(MSVCR100.dll)
LIBCMT.lib(hooks.obj) : error LNK2005: "void __cdecl terminate(void)" (?terminate@@YAXXZ) already defined in MSVCRT.lib(MSVCR100.dll)
LIBCMT.lib(crt0init.obj) : error LNK2005: ___xi_a already defined in MSVCRT.lib(cinitexe.obj)
LIBCMT.lib(crt0init.obj) : error LNK2005: ___xi_z already defined in MSVCRT.lib(cinitexe.obj)
LIBCMT.lib(crt0init.obj) : error LNK2005: ___xc_a already defined in MSVCRT.lib(cinitexe.obj)
LIBCMT.lib(crt0init.obj) : error LNK2005: ___xc_z already defined in MSVCRT.lib(cinitexe.obj)
LIBCMT.lib(errmode.obj) : error LNK2005: ___set_app_type already defined in MSVCRT.lib(MSVCR100.dll)
LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library

Any help or suggestions appreciated.

jzepeda
  • 1,470
  • 2
  • 15
  • 22

3 Answers3

9

On CMake, when building the openCV static libraries, besides unchecking BUILD_SHARED_LIBS I also unchecked BUILD_WITH_STATIC_CRT, and that took care of that particular problem

jzepeda
  • 1,470
  • 2
  • 15
  • 22
  • 2
    +1. Seems like the pre-built Windows static libs are built with static CRT (/MT). If your project uses dynamic CRT (/MD) you must rebuild OpenCV yourself. – Adi Shavit Apr 09 '13 at 09:10
5

Make sure that the Project properties->Configuration Properties->c/c++>Code Generation: Runtime Library is Multi-threaded (/MT)

user1043413
  • 123
  • 2
  • 6
1

The problem seem to be that your linker attempts to combine different, incompatible versions of the Visual C++ runtime library (CRT) into one single binary.
Let me guess in the wild: Each part of your project and the libraries you link against are NOT generated with the same code generation options in Visual C++.
I wonder if the /NODEFAULTLIB switch 'd solve this challenge?

salomon
  • 1,219
  • 1
  • 8
  • 9
  • Is there another solution to doing this? Either rebuilding openCV with specific flags or changing the build options on Qt Creator? For building the OpenCV libs I just built it of the solution file generated by cmake, with the build shared libraries checkbox unchecked, which to my understanding is all you need to do to get a solution for the static libs. And on Qt Creator from what I've read is it should be a simple matter of importing those libs? – jzepeda Mar 15 '12 at 06:05
  • Only for testing purpose: Would you try to compile your project in release mode, instead of debug mode? – salomon Mar 15 '12 at 16:48
  • I've been doing release all along. Debug gives me errors like "opencv_core231.lib(drawing.obj):-1: error: LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj". The below solution fixed it for me, although I suspect I will have to reenable the option (because it looks like QT isn't linked statically, and I will also want that to make my project redistributable) – jzepeda Mar 16 '12 at 02:16