Dll with MFC and static linking [duplicate] - c++

This question already has answers here:
#error Please use the /MD switch for _AFXDLL builds
(2 answers)
Closed 8 years ago.
I have dll using MFC and I need it to work on another computer without additional instalations.
If "C/C++ -> Code Generation -> Runtime Library" is Multi-threaded DLL (/MD) and "General -> Use of MFC" is set to Use MFC in a Shared DLL - my dll needs msvcr80.dll.
If "General -> Use of MFC" is set to Use MFC in a Static Library and "C/C++ -> Code Generation -> Runtime Library" to Multi-threaded (/MT) - I have an error
#error Please use the /MD switch for _AFXDLL builds
Is there any solution? I'd appreciate any help. I'm searching the answer for a long time and in many places it is said that the second variant should work without errors.

You should build your client in the same mode as library your link with.
Librarian warns you about that. So to resolve the error - build your client that uses MFC lib with /MD flag instead of /MT

MFC extension DLLs must be linked with the DLL version of MFC. Only MFC apps can be statically linked to the MFC framwwork.

Related

MFC application failing to build project even after shared dll specified? I tried all the ways I found on online but it still doesn't work

#ifdef _DLL
#ifndef _AFXDLL
#error Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d]
#endif
#endif
I am not knowing how to fix this error if someone could help me that would be great
I tried changing the mfc properties but still doesn't work
From the error message Setting: Project Properties -> Advanced -> Use of MFC = "Use MFC in a Shared DLL" is in conflict with setting: Project Properties -> C/C++ -> Code Generation -> Runtime Library -> "Multi-threaded Debug DLL (/MDd)".
Make sure both are set as above. As an example you will get this error if trying to build with "Use MFC in a Static Library". Also make sure these settings are not being inherited in conflicting state.
what is difference btw /MD and /MDD in VisualStudio C++?

Why is "Use MFC in a Static Library" setting not followed when building a DLL with VS 2008?

My goal is to create an MFC/C++ DLL that does not have any dependencies other than the basic Win32 DLLs. So I chose "Use MFC in a Static Library" setting in the project properties -> General -> Use of MFC:
but when I build this DLL and check the result with the Dependency Walker I get this:
Showing dependencies on the following MFC Dlls:
MFC90U.DLL
MSVCR90.DLL
MSVCP90.DLL
So what am I doing wrong here?
PS. I'm using Visual Studio 2008
I think I got it. What messed me up was the project setting in C++ -> Code Generation -> Runtime Library. It was changed to Multi-threaded DLL (/MD) and then the following was added to the stdafx.h file:
#define _AFXDLL
So to make it statically link to MFC libraries, I had to change the first setting to Multi-threaded (/MT) and comment out the second one.

Build a C++ binary w/o dll dependency

I wrote some C++ code. I sent it to my friend who isn't a developer. On windows 7 he got the error "the program can't start because msvcr120.dll is missing"
How can I build it so it will run? Do I need to use compile using MSVC 2008? 2005? I'd like this to work on vista+
I think you can statically link the run time library using /MT instread of /MD
see http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
Right Click your project -> Properties -> C/C++ -> Code Generation -> Set "Runtime Library" to "/MTd" for debug and "/MT" for release builds

How to not link against msvcr100.dll?

I am compiling project in Visual Studio 10, compiled executable runs fine on win 7 but it doesnt works on win xp because of missing msvcrt100.dll.
I tried to use "/NOTDEFAULTLIB" but it also removes some other external libs that i use.
Is there way to not link against latest Microsoft runtime library ?
Thanks in advance.
To not link against the DLL you must link against the runtime statically.
To do this, go to C/C++ Properties -> Code Generation and under Runtime Library select Multi-threaded (/MT) or Multi-threaded Debug (/MTd) from the drop-down. Note, once you're using a static runtime library all of your other library code you link against must also be built with the same setting. And you will also likely have to add additional libraries to the Linker -> Input under Additional Dependencies.
It only works on Windows 7 machine by accident, somebody installed that DLL earlier.
Short from creating an installer (easy to do with a Setup project), a simple fix is copying msvcr100.dll along with your own binaries. If you only have an EXE then the simple solution is to link the static version of the CRT. Switch to the Release build, right-click the project, Properties, C/C++, Code Generation, Runtime Library setting. Change it to /MT from the default /MD.

Linking error -> Managed DLL to Unmanaged Lib

I have a managed C++ dll which uses a unmanaged C++ lib. I've added the lib file in the managed project's "Additional Dependencies". Unfortunately I get a dozen of std::locale already defined in msvcprtd.lib linking errors.
Any idea? Do I have to build both as dll and link them together?
You probably need to change the runtime library setting for one of your projects so that they are both the same. The "Multi-threaded Debug DLL" option in the runtime library settings means that your project will be linked against the DLL version of the runtime lirary, not that your project is a DLL. Where-as "Multi-Threaded Debug" means it will link against a .lib version of the standard library.
When you link together 2 projects that use different settings, then they end up with duplicate references. One reference from the static runtime library, and one from the DLL runtime library. This is the source of your errors.
Which setting you should pick depends on whether you want to distribute the runtime DLLs with your project (or count on the user already having them). If you want to go for this option, select the DLL runtime library, otherwise select the non-DLL version. The down side of the non-DLL version is that all the runtime library code will be embedded in your DLL/EXE, which will increase it's size.
EDIT: Actually, looking into it a bit more. This link indicates that with CLR projects (which I suspect yours is, being managed C++) you can't use the static linked option, so you need to use the "Multi-threaded Debug DLL" option for both.