I'm trying to set up OpenCV 2.4 as follows:
I've downloaded and extracted the precompiled package to C:\OpenCV240.
In Visual Studio, I've added C:\OpenCV240\build\include as an additional include directory.
Furthermore, I've added C:\OpenCV240\build\x86\vc10\staticlib as an additional library directory.
And I've specified all available .lib files as additional dependencies.
When I compile my "Hello World" program (which compiles just fine when using the DLLs), I get a lot error messages like this:
1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: __thiscall std::_Container_base12::~_Container_base12(void)" (??1_Container_base12#std##QAE#XZ) already defined in opencv_core240d.lib(matrix.obj)
What am I doing wrong?
It sounds like you need to specify linking against the static C runtime library in your program.
If OpenCV linked against the static CRT and you use the dynamic one, you get these types of redefinition errors.
To change this setting, open your project's Properties and go to Configuration Properties -> C/C++ -> Code Generation.
Change Runtime Library from Multi-threaded Debug DLL (/MDd) to Multi-threaded Debug (/MTd). Do the same for your other configurations, using the non-Debug variant where appropriate.
Related
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_core320d.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:
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.
Add in front of [Configuration Properties]->[VC++ Directories]->[Include Directories] $(OPENCV_DIR)\include;. The semicolon is to separate paths.
Add in front of [Configuration Properties]->[VC++ Directories]->[Library Directories] $(OPENCV_DIR)\x86\vc15\staticlib;.
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].
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.
I'm trying to create a program (C++) that can read multiple file formats, on Windows (VS2015).
In order to do that, I created a solution with a Project for MyProgram (which is the main program), and a project for MyLibrary (which contains several parser for different file formats).
In MyProgram I create some parser objects based on the program input.
Everything was working well.
However, I tried to create a new file format parser (NiftiParser) that uses an external library, nifticlib, that I downloaded and compiled (as static library).
So I created my NiftiParser class, that implement some methods, and internally, it calls nifticlib.
I added the include dir and library dir in the project properties, and it compiled without errors.
I then got a Parser.lib that has been created.
However, when I tried to compile MyProgram, I got an error about some functions of nifticlib library not being resolved:
1>------ Build started: Project: Parser, Configuration: Debug x64 ------
1> nifti_parser.cc
1> Parser.vcxproj -> C:\Users\Laurent\Documents\C++-build\Projects\Parser\Debug\Parser.lib
2>------ Build started: Project: MyProgram, Configuration: Debug x64 ------
2>Parser.lib(nifti_parser.obj) : error LNK2019: unresolved external symbol nifti_image_read referenced in function "public: __cdecl NiftiParser::NiftiParser(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0NiftiParser##QEAA#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z)
2>C:\Users\Laurent\Documents\C++-build\Projects\Debug\MyProgram.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 1 succeeded, 1 failed, 3 up-to-date, 0 skipped ==========
I'm not sure to understand why this is happening. I tried to add the path of include and the library dir of nifticlib to MyProgram project, but I still have some issue.
I'm not sure to understand what is the compilation/linking process for Visual Studio (I used to work on Linux).
It appears to me that the projects are only compiled if it's a static library, and the linking part only happen when I try to compile the program itself.
Am I right ?
Also, I'm not sure where nifticlib should link. Should it be to the Parser.lib (where I use the functions of the library) or directly to the program ?
Thank you.
PS: Also my noticed that my install directory of nifticlib only contains headers and .lib. Do I need to have the .cpp in there too, or does the functions are already contained in the .lib ?
Edit: Ok, apparently building something as a static library doesn't link it to anything, so that's probably why I didn't get any issue compiling Parser.lib, because it doesn't link to nifticlib, but MyProgram needs it.
So should I only include the include dir of nitficlib in my Parser projects, and put the Parser.lib and nifticlib in my MyProgram project ?
The compilation/linking of Visual Studio does not differ much from what is happening in Linux toolchains. When you are making a binary (an executable or a dynamic library) all external symbols must be resolved. In GCC you must indicate the libraries to your linker. The command line option -lmath will tell linker to load libmath.a from one of the paths set for library search and use it when building your executable image.
Similarly, in VS you need not only to set the path to library directories, but to explicitly indicate the library file as well. This is usually done on project properties -> Linker -> Input -> Additional dependencies. Note that in Windows you just put in the full library name (MyLib.lib), not the part between lib and .a
Also note that in Windows you cannot link to the binary of a dynamic library (.dll). You will need an import library (.lib) for it.
When you are creating a static library, the toolchain does not resolve the external references. That is why you .lib compiled fine without referencing the nifticlib.
As for where you should link the nifticlib, it depends on the circumstances. If only your MyLibrary will ever be using the functions of nifticlib, it is wise to link it with your static library. However, if someday you'll wish to use some function from nifticlib directly in MyProgram, there may be a conflict at link time. In this case you will need to link the nifticlib only when building the MyProgram.
A rule of the thumb can be formulated like this:
If only MyLibrary is using the headers of nifticlib, link your library with third party one
If MyProgram also uses the headers of nifticlib (and actually calls functions from them), it is better to link the nifticlib when building MyProgram
This is not a strict description, and there are more complicated cases, but the basics are like this.
And no, you don't need the cpp files used when building your static library, when you are linking to that library from a different project. The contents of the library .cpp's is already included into static lib file in the form of object code.
Edit: If you do wish to link you static library with the externals from another static library, you need to go to project properties -> Librarian -> General -> Additional dependencies and put the external .lib there
I have a visual studio 2012 c++ project.
I recently uninstalled it and installed visual studio 2015 and upgraded the project.
When i am building the project, getting error as shown below:
Error LNK2019 unresolved external symbol _memcmp referenced in function
Moreover i have not used anywhere in my code memcmp fucntion.
I used the linker verbose function and could see below in output file:
Found _memcmp
Referenced in MyC++Project.obj
Referenced in libcpmtd.lib(xstrcoll.obj)
Loaded libvcruntimed.lib(__memcmp_.obj)
Two questions here
1.even though i have not used memcmp in my code why i am getting that linker error?
2.why is memcmp being loaded as __memcmp_.obj
I have the following settings also in my project:
1.C++-->Code generation-->Runtime Library is set to /MTd
2.Linker-->Ignore All default libraries is set to nothing
I have tried all the project settings but everything in vain.
I have issue only with this memcmp function which i have not used.
I have used mamcpy and memset and do not have issue with those
Explicitly add vcruntime.lib or other appropriate version of CRT Library to linker parameters (additional dependencies).
When you use memcmp explicitly it is probably handled as intrinsic function and is compiled as inline function.
Try to add vcruntime.lib and ucrt.lib to your additional dependencies. ===> properties->Linker->Input->Additional Dependencies
Sample path of 'vcruntime.lib': "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\lib\vcruntime.lib"
Sample path of 'ucrt.lib' : "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.16299.0\ucrt\x86\ucrt.lib"
My environment: VS2017 (v141)
I've run into this same problem with a legacy Visual C++ 6.0 nmake file with Visual Studio 2015.
This blog article, Introducing the Universal CRT, describes how the Visual Studio 2015 runtime has been split into more than one library. The runtime is now "split the CRT into two logical parts: The VCRuntime, which contained the compiler support functionality required for things like process startup and exception handling, and a “stable” part that contained all of the purely library parts of the CRT" to allow for easier updates.
So long as you do not link with the /nodefaultlib option, all of the
correct library files will be found when you link your project. If you
link with the /nodefaultlib option, you will need to link several
extra libraries when you link. For example, whereas you previously
might have just linked msvcrt.lib in order to use the CRT DLL, you
will now also need to link vcruntime.lib and ucrt.lib. Here is a table
that shows which libraries you will need to link for each “flavor” of
the libraries:
Release DLLs (/MD ): msvcrt.lib vcruntime.lib ucrt.lib
Debug DLLs (/MDd): msvcrtd.lib vcruntimed.lib ucrtd.lib
Release Static (/MT ): libcmt.lib libvcruntime.lib libucrt.lib
Debug Static (/MTd): libcmtd.lib libvcruntimed.lib libucrtd.lib
See also the Microsoft documentation C runtime (CRT) and C++ Standard Library (STL) .lib files which describes details about the libraries.
See also Microsoft C/C++ change history 2003 - 2015.
I'm trying to set up a VS2015 solution I have with OpenCV 3.0.0, and I'm trying to do so using static libraries only.
Header files: the OpenCV header files are included correctly and I can reference them in my source files without issue.
Libraries: the static library file that was created for me when I used CMake to build OpenCV (called "ippicvmt.lib") is included correctly, and loads without issue.
Using OpenCV in code: when I try to use OpenCV classes/functions in my solution, I get linker errors when building similar to
LNK2001: unresolved external symbol "public: virtual double __thiscall cv::VideoCapture::get(int)const
I can right-click and choose "Go to definition" on my use of this function and it opens videoio.hpp and shows the declaration of the virtual method. The linker error I see is likely caused by the inability for visual studio to find the actual implementation of the method in the static library I have included in my solution.
It is my understanding that the only .lib file I need to include is the one static library file. That is all I have included right now.
Does anybody know why VS can't find the implementations of this code in the OpenCV static library?
I fixed my issue by disabling the flag for building with shared libs in CMake, after that way more .lib files show up in the sharedlib folder in my install. I added each .lib file I wanted to use to my additional dependencies in my project properties and my project built.
I've downloaded the ImageMagick source, compiled the wizard to create a Visual Studio solution for static linkage, and included the static library Magick++ project in my sample project (code below). I've also added a dependency on that project and included the .lib file in the solution, nothing helps.
#include <Magick++.h>
int main()
{
Magick::Image image;
bool test = image.isValid();
return 0;
}
This gives several linker errors, such as:
unresolved external symbol "__declspec(dllimport) public: virtual __thiscall Magick::Image::~Image(void)" (__imp_??1Image#Magick##UAE#XZ) referenced in function _main
Why can't it find the implementation?
I'm using Visual Studio 2010 Beta 2.
The problem may rise from that you are using different compiler than the library was compiled with. As your compiler is fairly new, it's very likely it uses different name mangling and can't find method signatures inside the library.
The answer to linking the ImageMagick static libraries is to ensure you link ALL the dependant static libraries as well!
Once you have compiled the solution for ImageMagick static libraries, go to the 'VisualMagick' folder (within the ImageMagick cloned repository) then to to the 'lib' folder there you will see all the _DB_.lib and _RL_.lib files for Debug and Release.
You need to include those names in the 'Additional Dependencies' section for both Release and Debug.
You must also ensure you have the Library folder path in the 'Additional Library Directories' for both Release and Debug.
Another key aspect is to ensure the libraries are built by the same compiler for the same platform architecture as your own application .. (WIN32 or x64).
This error is strange - The compiler is looking for a function from a DLL (__declspec(dllimport)). Are you sure you are using the right header files ?