Statically linking libs in Visual Studio - windows

When you choose /MTd static linking in Visual Studio, would it try to link to each lib statically or there are some exceptions to system libs?

Description: /MTd: Defines _DEBUG and _MT. This option also causes the compiler to place the library name LIBCMTD.lib into the .obj file so that the linker will use LIBCMTD.lib to resolve external symbols.
From what I can see there is no static linking. If you want to do static linking you need to use ILMerge. And even then you should not attempt to merge in the required .Net Framework references as they often reference others which you may miss. It may not even be possible as they use GAC for referencing.

The /MT and /MD flags only define how the C/C++ runtime library is linked in. It has no effect on other libraries, system or user defined.
The system libraries, such as kernel32.lib, user32.lib, etc) are import libraries - there is no static library to link with.

Related

MSVC17 Linking application with runtime statically, still getting missing dll error

I set /MT flag to have my c++ application statically linked with C runtime, so I don't have to worry about redistributing the runtime, however, upon launch I get an error message saying missing "Api-ms-win-core-version-l1-1-0.dll". How do I compile my application so that its completely independent of any runtime etc?
Build Environment: MSVC 2017, windows 10 SDK
Compile and Test machine: 64-Bit Windows 7
The selected answer here helped me solve my problem. In short, you can't link with any of the api-ms-win-core* libraries, you should link with appropriate libraries listed for APIs in the msdn. The api-ms-win-core* libraries are used indirectly by the OS, that's why they show up as missing - when in reality the appropriate windows .lib need to be linked.
In my case, I used depends to figure out which API libraries were missing then looked up the appropriate .lib files in msdn and added those in "additional dependencies". Problem solved.
FYI, /MT flag is working as expected, I don't have to redistribute c-runtime etc.

Building a DLL on Windows that uses a static /MT lib

I have a third-party static lib, libTA.lib. It was built with /MT and I can't change that. I want to link libTA into a DLL I'm building, mydll.{lib,dll}. Of course my dll should be built with /MD; my dll will be used with /MD-built exes (also not under my control). Linking mydll with libTA.lib fails (correctly, I'd say) due to LNK2038, mismatch detected for RuntimeLibrary.
So, what can I do about this? Building mydll with /MT seems like a terrible idea because I don't want to have multiple msvc runtimes.
So my question is, what are my options? Anyone been through this before and found a solution?
Keep all the projects in your solution to /MD so that they will linked dynamically with external DLL libraries. The static libraries could be included in the additional dependencies or through #pragma include () directives. Note the DLL files should have their symbols exported through a .def file (use lib.exe to convert to lib file) or .lib file. However the toolset, target platform settings of all the dynamic and static libraries should be same to avoid linker error.

Linking to open scene graph statically

Is there a way to link to the open scene graph libraries statically?
I compiled osg on windows 7, and it has both static and dlls, but I want to link statically so I don't have to rely on the dlls. The point is so that I can just carry the exe file for distribution and have other people test it without having to worry about the dlls being missing. I don't know what I have to change in the settings for it to link only to the static libraries.
I'm using visual studio 2010.
See the CMake option
DYNAMIC_OPENSCENEGRAPH
Set to ON to build OpenSceneGraph for dynamic linking. Use OFF for static.

remove dependency on CRT in dll

I'm building a dll on Visual Studio 2010, and I'm using some simple C functions like fprintf and fread, and it's linking to msvcr100.dll by default.
This dll is going to be loaded into an app that may be using a different CRT version (eg. msvcr90.dll, msvcrt.dll).
Since I know the app's going to load a CRT before my dll gets loaded, can I remove the dependency on msvcr100.dll and use the C functions in the CRT loaded by the app?
In the end, I decided to build the DLL against the lowest common denominator CRT version used by the target app, msvcr90.dll.
I did this by using the MSVC toolchain, available with Visual C++ 2008 Express (free).
I did try the mingw/gcc toolchain, which allows you to specify which CRT version to link against (see mingw-rt and gcc -specs=msvcr**), however, msvcr90.dll is a new-style SxS assembly so I couldn't get the produced executable to run properly.
It may be worth considering skipping linking against msvcr**.dll entirely; see this post and VC/include/delayhlp.cpp.

Visual Studio DLL project with DLL dependencies, unresolved external symbols

I'm new to Windows concepts. I'm trying to develop a DLL that also dynamically links against other DLL's. I'm using Visual Studio 2010. At linking time Visual studio tells me it's trying to link against a bunch of .lib files. I don't think Visual Studio should attempt to read any .LIB files if I want my code to perform dynamic linking is that correct? Or do I not understand the use of .LIB files enough?
thank you
How I understand, you are a little confused, because you want to use DLLs instead of LIBs.
The trick is that Kernel32.Lib, User32.Lib, AdvAPI32.Lib and so on has no implementation of the function which you use. Moreover if you use functions like CreateFileW or MessageBoxW in your program the references to the function will be unresolved after the compilation. So without having LIBs the unresolved references will stay also in the EXE. That will be bad of cause. To be executable the EXE must have resolved all external references and moreover know exactly which functions should be found in which DLLs. So named import library help to solve the problem. The import library Kernel32.Lib for example has information about CreateFileW function and User32.Lib has information about MessageBoxW. If you use a list of import libraries like Kernel32.Lib, User32.Lib, AdvAPI32.Lib your EXE resolves all referenced and includes the exact list of DLLs which it need and the list of functions used in the corresponding DLL in a special Import table (see http://en.wikipedia.org/wiki/Portable_Executable#Import_Table, http://msdn.microsoft.com/en-us/library/ms809762.aspx and http://www.microsoft.com/whdc/system/platform/firmware/pecoff.mspx). So If one starts your EXE the operation system loads the DLL in the address space of the process and resolves till the end all references up to the addresses of all functions used.
Typically import libraries will be created by linker (link.exe) when one compile a DLL. It is also possible to create an import library which corresponds to the DLL with respect of lib.exe utility. (see http://msdn.microsoft.com/en-us/library/0b9xe492.aspx)
As I recall, Visual Studio's compiler resolves .DLL files by linking against stub .LIB files.
Check, one of them should be kernel32.lib.

Resources