Using a static library when building a DLL in visual studio - c++

I'm trying to build a DLL using Visual Studio 12 Community that depends on OpenCV.
I want to include OpenCV as .lib files so I don't have to distribute it seperately, but I need my file to be built as a DLL.
But I can't configure Visual Studio to import a lib into a DLL. If in
My Project -> Properties -> Configuration Properties -> General -> Configuration Type,
I select "static library (.lib)" and in:
My Project -> Properties -> Configuration Properties -> VC++ Directories -> Library Directories,
I select the path to the OpenCV .lib files, and in
My Project -> Properties -> Configuration Properties -> Linker -> Addition Dependencies
I add a reference to each .lib, it works.
But if I change the Configuration Type do "dynamic library (.dll)", Visual studio tells me:
opencv_highgui2410d.lib(window.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in VectorsImport.obj
It seems I can only make .lib files with .lib files. But that seems very unlikely. I've looked this up, but I only find guides on how to make .lib files or .dll files or one out of the other. This must be pretty simple, but I can't figure it out. I'm used to Linux, where a .o can pretty easily be included into a .so . This puzzles me.

The error indicates that you are trying to link a OpenCV module which has been compiled for using the static C/C++ runtime with debugging support with a module VectorsImport.obj (probably from your own project), which has been compiled for using the dynamic C/C++ runtime with debugging support. The four variants of the C runtime library are not compatible in the Microsoft SDK, so all object files (either from your project or from statically linked libs) have to match on that setting. On Visual Studio 2010, it could be found at C/C++-Compiler -> Codegeneration -> Runtime library.
Please note that (as the bold face should emphasize) it is not about OpenCV being a DLL or .lib, but in case OpenCV is linked as separate DLL, it is allowed to use a different kind of C runtime library, so the mismatch does not matter.

Related

Visual Studio 2017 looking for DLL rather than a LIB

A C/C++-Win32-project used to compile, link and run just fine using Visual Studio 2010. This project uses a 3rd party library XYZ.LIB that comes as a (statically linked) object library (not an import library).
I transferred this project to Visual Studio 2017 and while it still compiles, it won't run anymore. Upon starting the programm, a message pops up telling that XYZ.DLL (!) is missing. I already added XYZ.LIB under Project -> Properties -> Configuration Properties -> Linker -> Additional Dependencies. I actually don't have the XYZ.DLL and VS2010 never complained about needing a DLL rather than the provided LIB.
How can I get VS2017 to use the regular LIB rather than looking for a DLL with the same name ?

Visual Studio static library : NuGet package not found when using in another project

I'm building a static library using Visual Studio, which involves installing nupengl and glm. Then I linked it to a test project via this method :
Project Properties -> VC++ Directories -> Additional include library (I entered the directory which contains my header files from the library)
Project Properties -> Linker -> Input (I entered the directory which contains the .lib file for the library)
I added the header files in the library to my test project
But when I run the test program, it produces things like :
GLFW/glfw3 not found
How can I use the installed NuGet package for the library when calling the library from another project ? Any help would be highly appreciated

Visual Studio Static Linking for Standalone Exe

I've read multiple posts with regards to this topic, but none of them have enabled me to build a statically linked exe.
In my release configuration (x64) I have the following:
Configuration Properties -> General : Use of MFC - Use MFC in a Static Library
Configuration Properties -> C/C++ -> Code Generation : Runtime Library - Multi-threaded (/MT)
Configuration Properties -> Linker -> Command Line : Additional Options - I have all the required Windows libs "kernel32.lib", etc. (as use of MFC removed them from the "All Options" window above)
Configuration Properties -> Manifest Tool -> Input and Output : Embed Manifest - No
Note that in Configuration Properties -> Linker -> Input there are 5 lib files that I'm using in my project, eg glfw3.lib and I'm using Full optimisation (/Ox).
After building the project and running the exe myself I receive errors "The code execution cannot proceed because glfw3.dll was not found..." etc.
Using dependencywalker I can see that it needs the dlls associated with the libs, which it of course cannot find.
Am I misunderstanding how to do this or is there something else that might be wrong?
(I'm using Visual Studio 2017)
Yes, it appears you have a slight misunderstanding.
If something is offered as a DLL, then it is meant to be used as a DLL. There may be some way to incorporate a DLL into an executable, but it would be a hack. That's not how things are supposed to work.
The lib file that you are linking against exists simply in order to provide you with functions that you can link against, and these do nothing but delegate to the corresponding functions in the dynamically loaded DLL. Without it you would have to locate each entrypoint of the DLL yourself, which would be perfectly doable, but a bit cumbersome.
So: you must either find a version of glfw3 which is packaged as a statically linkable library (I have no idea whether one exists) or live with the fact that your .exe will need to be shipped together with glfw3.dll.

SCIP and Visual Studio: error LNK2001

I am trying to make the SCIP work with C++ in Windows 10.
I want to solve the VRP problem using SCIP (version 3.2.1) in Visual Studio 2010.
I have downloaded Source files and then precompiled dlls from the http://scip.zib.de and included the .hpp and cpp files in my VS project.
there was a .dll, a .exe and a .lib file in the archive.
In the project properties I did the following:
C/C++ -> General -> Additional Include Directories:
C:\scipoptsuite-3.2.1\scip-3.2.1\src\
C/C++ -> Linker -> Input-> Additional Dependencies: C:\Users\Mahla\Desktop\src\scip-3.2.1.win.x86_64.msvc.opt.spx.ld.lib;
C/C++ -> General -> Input-> Additional Library Dependencies:
C:\Users\Mahla\Desktop\src\scip-3.2.1.win.x86_64.msvc.opt.spx.ld.lib;
VC++ Directories-> Include Directories:
C:\Users\Mahla\Desktop\src;
VC++ Directories-> Library Directories:
C:\Users\Mahla\Desktop\src;
src folder is a copy of src down in the scip root directory.
I copied .dll, .lib and .exe in the Debug folder at the same level as the solution .sln file in Visual Studio.
When building, I get many errors like :
main_vrp.obj : error LNK2001: unresolved external symbol _SCIPcreateConsLinear
Can anyone tell me what is the problem or how to set up an example in VS.
Thank you very much.
I am using a x64 version of SCIP. So my "Platform" should be x64, not Win32, or it will give LNK errors.
I simply changed the platform in configuration manager to x64 and everything was okay then.

Unresolved externals on static linking OpenSceneGraph libraries in Visual Studio

I'm using OpenSceneGraph libraries in my application and I want to link them statically, so I don't have to carry DLLs with binary. I have downloaded OpenSceneGraph 3.4.0 sources and generated Visual Studio project by cmake. I did disabled DYNAMIC_OPENSCENEGRAPH and DYNAMIC_OPENTHREADS in cmake GUI. After that I successfully compiled OSG and have static libraries (.lib). In my application I added include and library directories in project properties -> VC++ Directories -> Include Directories & Library Directories (I'm using Visual Studio 2013 by the way). I also added directory with OSG .lib's in Linker -> General -> Additional Library Directories and typed all required libs in Linker -> Input -> Additional Dependencies:
ot20-OpenThreads.lib
osg130-osg.lib
osg130-osgUtil.lib
osg130-osgGA.lib
osg130-osgDB.lib
osg130-osgViewer.lib
osg130-osgText.lib
opengl32.lib
osgdb_3ds.lib
osgdb_freetype.lib
osgdb_png.lib
And I'm still getting unresolved externals errors for OSG symbols. I also tried setting General -> Character Set to Unicode but it didn't help (in OSG project generated by cmake I have Multi-Byte Character anyway). I don't have any ideas how to solve that now, it's my first time compiling application with static linked libraries.
I would really appreciate any help.
P.s. Sorry for no code, I can't provide it but I try to give as much details as I can. With dynamic linking I can build my application without any problems.
I managed to solve this problem myself.
I was missing two more libraries in Linker -> Input -> Additional Dependencies:
msvcrt.lib
libcmt.lib
And I also had to add preprocessor definition OSG_LIBRARY_STATIC in C/C++ -> Preprocessor -> Preprocessor Definitions. The last thing was adding these few lines of code in my main.cpp to link OSG plugins:
#ifdef OSG_LIBRARY_STATIC
USE_OSGPLUGIN(freetype)
USE_OSGPLUGIN(png)
USE_OSGPLUGIN(3ds)
#endif
Now my application is successfully compiling and working :)
P.s. On first build I had errors with PNG and FreeType plugins which were caused by lack of .lib-s for libpng and freetype. There errors were solved in my case by adding libpng16_static.lib and freetype250.lib to Linker -> Input -> Additional Dependencies and copying them into my libraries directory (obviously other plugins would depend on other libraries).