How to stop Linking error without changing Runtime Library? - c++

Ok so I am new to C++ and I am trying to include libcURL. I have created a static library as opposed to using the DLL as I want to be able to reduce my programs dependencies by as much as possible.I have also set my Runtime Library in Visual Studio 2012 to MT and MTd as I understand this removes the need for redistributables to be installed?
Anyway, as soon as I add the line #define CURL_STATICLIB, MSVCR110.dll & LIBCMTD.lib clash with each other. I understand that this is because MSVCR110.dll is built with /MD and LIBCMTD.lib is built with /MT but I need to use the static library AND have the Runtime set to /MT
How can I fix the linking error without changing the Runtime Library? if thats no possible then How can fix this while keeping zero dependencies for my application?
Also, If the linking error only concerns MSVCR110.dll & LIBCMTD.lib, then why does it only occur when #define CURL_STATICLIB is set, and not all the time?
Thanks

Related

what is libcpmtd.lib for?

All of a sudden, my program got link errors like below:
libcpmtd.lib(xlock.obj) : error LNK2038: 'RuntimeLibrary' unmatched.The value 'MTd_StaticDebug' is different from that of [project x], which is: 'MDd_DynamicDebug'
Since this error suddenly appeared on multiple computers, I believe this was not caused by unintended modification of any file.
Version: Visual Studio 2013
Question-1:
Could you give any hint about the reason of this error and how to solve it?
What I did:
I remove the libcpmtd.lib from the import library list of project setting, then the build error disappeared and everything was OK.
But, I am not sure what libcpmtd.lib is for?
Google told me there is CRT inside, but what are the content specifically?
Perhaps this library was added and then not relevant from some point of time.
Question-2:
what is in side libcpmtd.lib? I want to figure out what I might have lost after I removed the lib.
These are the C++ standard runtime libraries. With Visual C++, you have 2 options, which give you 4 permutations of the library it links to.
Debug v.s. Release
As it suggests, do you want the C++ runtime lib that has additional error checking?
Static v.s. Dynamic
If you happen to be compiling only a simple exe, then linking to the library statically should be fine. If however you have a large project consisting of multiple DLL's, then it makes sense to load the CRT dynamically (so it can be shared between the DLL's, rather than being duplicated into each one).
So what you have is a mis-configured build. You'll need to check the C/C++ Runtime Library settings for each library, DLL, and exe within your project (which you can find in your project settings, under C/C++ -> Code Generation).
You will need to make sure that each one links to the same runtime library (i.e. Debug DLL for all debug settings, Release DLL for all release settings).
If that doesn't fix it, then there are two other possible causes:
You have started linking to a 3rd party library, and that's the cause of the CRT mis-match. Most libs ship debug and release builds for this reason, so hopefully you'll just need to update the libs you are linking to.
You are inadvertently pulling in a Debug lib into a release build (or a release build lib, into a debug exe). For this you will need to check all of the additional library directories, and/or check to make sure all of the debug & release output directories are correct (i.e. you aren't accidentally compiling a debug lib into a release build folder).

Why I still need dll when using /MT in an OpenCV project in Visual Studio?

Hi I'm trying to use /MT(Multi-threaded) in an OpenCV project in VS2015, and when I deleted the 'opencv_world400.dll', the program came with an error, indicating lack of this dll. However I think /MT means static linking so no dll is needed, what could be the problem or reason?
The /MT and /MD parameters specify how your project links to the C++ run-time libraries, not OpenCV. The only thing this setting has with external libraries, is that to escape conflicts all static libraries used should have the same value for this parameter, as your project.
Since you are using a dynamic build of OpenCV changing this setting does not impact OpenCV. If you would switch to a static build of OpenCV, then this parameter should match in OpenCV and your project.

Static CRT link

im trying to build my project and I want to link the windows CRT statically, but I find some errors trying to linking it,
I've included in the linking list these ones:
libcmt.lib
LIBCPMT.LIB
libucrt.lib
libvcruntime.lib
but I get these linking errors, saying that these symbols are missing:
__imp_copysignf
__imp__aligned_free
__imp__aligned_malloc
__imp_modf
__imp_modff
I've already added libucrt (which is where must be malloc functions but still giving errors), what lib am I missing?
EDIT: Im using Visual Studio 2017 15.4.4 targeting Windows 10 ver.10.0.16299.0
Double-check that you have also set the /MT compiler flag. If it is not set, it is possible that it defaults to dynamic linking setting. In that case, _MT preprocessor define is missing, which causes the compiler to use calls to dynamic runtime functions. You can see the __imp__ prefix that normally comes from DLL import libraries.

The /MT option (static link for MSVCR100.dll dependency) does not work with Visual Studio

I am using a DLL that I compile with MSVC9 (Visual Studio 10 C++).
There is a dependency on msvcp100d.dll and msvcr100d.dll.
I would to make a static link in order to use my DLL standalone . To do so I use /MT instead of /MD option in my makefile.
Howewer when I check with dumpbin or dependency walker the dependency are still present, the msvcp100d.dll and msvcr100d.dll seems to be still dynamically linked.
Another thing that seems to be related, if I use the DLL through VS I have no problem, If I use another program I have a crash - it seems to be releated !
Thanks !
Thanks #Hans Passant he was right, I was linking using another DLL where the dependencies msvcp100d.dll and msvcr100d.dll present.
So even with a static compilation of my DLL I still need the others DLL.
I'll deliver with these DLL in release mode.

How can a static release lib be linked into a debug build?

The Gameplay Project distributes a set of static libraries of all it's dependencies. The libraries they include will link in a debug or release build of an application. I ran the strings command on their lib and compared it to one I compiled. The linker directive /FAILIFMISMATCH:"RuntimeLibrary=MD_DynamicRelease" is set in my library, but not theirs.
How did they compile their library this way?
Edit: Clarification
I failed to mention that this occurs on the next branch, which uses libs compiled for VS2012. The _MSC_VER of the libraries is 1700.
I am aware that you shouldn't link different versions of the CRT, but I'm wondering how they were even able to compile it this way.
It seems MS has started adding /FAILIFMISMATCH:"RuntimeLibrary line from vs2012. /FAILIFMISMATCH itself is introduced in vs2010. I've chekced Gameplay Project sln file and it is using vs2010. Try to use vs2010 and see if the directive is gone.
I think you already know this but it doesn't really matter if the directive is there or not because you must match the runtime library compile option anyway.