简体   繁体   中英

Include static library in VS C++ project

Configuration I have

  • Windows 10 64bit
  • Visual Studio Community 2017 with Visual C++ 2017
  • CMake 3.9.0
  • opencv 3.3.0

Aim

Goal is to build opencv as a static library ( .lib ) and include into a Visual C++ project which is a DLL. Everything should be compiled for x86 architecture or simply 32bit.

Process

Latest opencv distributive does not contain dll's compiled for 32bit system and therefore, I need to compile own version. According to the opencv 2.4 documentation on "installation in Windows" . I have compiled the library with BUILD_SHARED_LIBS option disabled and configured target project as described in "how to build applications with OpenCV inside the Microsoft Visual Studio" .

Compilation of my project fails with following errors (totally error count is greater than 800)

Error type 1

LNK2038 mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' 
doesn't match value 'MDd_DynamicDebug' in main.obj  
\opencv_core320d.lib(alloc.obj)

Error type 2

LNK2005 "public: void __thiscall std::basic_ostream<char,struct 
std::char_traits<char> >::_Osfx(void)" (?_Osfx@?$basic_ostream@DU?
$char_traits@D@std@@@std@@QAEXXZ) already defined in 
opencv_core320d.lib(system.obj) \msvcprtd.lib(MSVCP140D.dll)

Error type 3

LNK2001 unresolved external symbol _ippicviHSVToRGB_16u_C3R@24  
\opencv_imgproc320d.lib(color.obj)

I believe it may be due to uncoordinated compilation options or erroneous configuration of my project, but because I am heavy Linux user I experience difficulties with setting up these things on Windows.

Update

After I have matched configuration shown in the screen below block of errors about code generation mismatch disappeared, but undefined references are still there. 运行时库选项

Thank you for help!

To resolve "Error type 3" add *.lib files from "staticlib" dir to "Linker->Input->Additional Dependencies". For example "_ipp" symbols are defined in "ipp_iw.lib" and "ippicvmt.lib".

Just for info. I successfully built opencv from sources as a static and shared library (as opencv_world3**.lib) using MSVS 2012 and cmake 3.9.0. But also done that with MSVS 2017 community.

PS. Maybe a TYPO but you said that you use opencv 3.3.0 but in error messages there is opencv_core 320 d.lib

PPS. "Error type 1" is the result of using static debug runtime version in opencv_core (MTd) and dynamic debug runtime in your app (MDd)

I always have the same problems. This is why I have a property sheet ready for use.

The steps to follow for static compilation are:

  1. Remember to Build the INSTALL project in the CMake generated Solution. Let's call $(OPENCV_DIR) the folder where the install happened (usually something like xxx\\install ). You can create an environment variable for this.
  2. Add in front of [Configuration Properties]->[VC++ Directories]->[Include Directories] $(OPENCV_DIR)\\include; . The semicolon is to separate paths.
  3. Add in front of [Configuration Properties]->[VC++ Directories]->[Library Directories] $(OPENCV_DIR)\\x86\\vc15\\staticlib; .
  4. Match the runtime library linkage mode between OpenCV and your project. If you unselect BUILD_SHARED_LIBS by default the CRT is statically linked (/MT for Release or /MTd for Debug). If you want it dynamically linked deselect BUILD_WITH_STATIC_CRT . So, as you already realized, fix it in [Configuration Properties]->[C/C++]->[Code Generation]->[Runtime Library] .
  5. Copy all filenames matching *.lib ( *d.lib for Debug) and add them in [Configuration Properties]->[Linker]->[Input]->[Additional Dependencies] separated by semicolons.

It's quite painful, so do it once in a Property Sheet and just include it when needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM