MSBuild.exe doesn't respect /MT compiler flag? - c++

I was surprised to see that after switching our build system from VS2008 to VS2010 our application now complains that MSVCR100.dll is missing at runtime. I checked our project's build options and we are specifying Multi-threaded (/MT) for the Runtime Library. However, when we build using the GUI, the DLL is statically linked and the error does not appear. The error only happens when we build using MSBuild.exe from the command line.
I compared the command lines, and they are almost the same, both specify /MT:
VS2010 GUI:
/nologo /W3 /WX- /O2 /Oi /Oy- /GL /D "_CRT_SECURE_NO_WARNINGS" /D "GSL_DLL" /D "WIN32" /D >"_WINDOWS" /D "_USRDLL" /D "_VC80_UPGRADE=0x0710" /D "__STDC_CONSTANT_MACROS" /D >"_WINDLL" /D "_UNICODE" /D "UNICODE" /Gm- /EHsc /MT /GS /Gy /fp:precise /Zc:wchar_t >/Zc:forScope /Fp"Release\Temp\ourProject.pch" /Fa"Release\Temp\" /Fo"Release\Temp\" >/Fd"Release\Temp\vc100.pdb" /Gd /analyze- /errorReport:queue
MSBuild.exe:
MSBuild.exe "%WORKSPACE%\OurProject.vcxproj" /t:Rebuild /p:Configuration=Release
/nologo /W3 /WX- /O2 /Oi /Oy- /GL /D _CRT_SECURE_NO_WARNINGS /D GSL_DLL /D WIN32 /D _WINDOWS /D _USRDLL /D _VC80_UPGRADE=0x0710 /D __STDC_CONSTANT_MACROS /D _WINDLL /D _UNICODE /D UNICODE /Gm- /EHsc /MT /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope > /Fo"Release\Temp\" /Fd"Release\Temp\vc100.pdb" /Gd /TP /analyze- /errorReport:queue
I have seen some posts about VS2010 static linking not behaving as expected, however my project builds and links just fine. It is not until runtime that it cannot find the required DLL. Installing the VS2010 runtime or shipping with the runtime are options we are trying to avoid.
Thanks!

Related

Visual Studio: two little projects with identical "Command Lines" but one cannot find the headers?

I have a solution with two one-source-file projects in it. Each file is:
#include <mosquitto.h>
The first compiles fine. The second says it cannot find a header. The source code in the second is identical to the first, so it is a mystery why it cannot compile.
Pre-compiled headers are not being used in either, so it is curious that the /Fp option is issued, I don't see how it can be playing a role.
/permissive- /ifcOutput "x64\Release\" /GS /GL /W3 /Gy /Zc:wchar_t /I"C:\Program Files\mosquitto2.0.14\devel" /Zi /Gm- /Od /sdl /Fd"x64\Release\vc143.pdb" /Zc:inline /fp:precise /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /Gd /Oi /MD /FC /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Fp"x64\Release\Good.pch" /diagnostics:column
/permissive- /ifcOutput "x64\Release\" /GS /GL /W3 /Gy /Zc:wchar_t /I"C:\Program Files\mosquitto2.0.14\devel" /Zi /Gm- /O2 /sdl /Fd"x64\Release\vc143.pdb" /Zc:inline /fp:precise /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /Gd /Oi /MD /FC /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Fp"x64\Release\Bad.pch" /diagnostics:column
Another user suggests posting the actual failed output.
Build started...
1>------ Build started: Project: Bad, Configuration: Debug x64 ------
1>Bad.cpp
1>T:\MyFirm\dev\MinorProjects\Bad\Bad.cpp(55,10): fatal error C1083: Cannot open include file: 'mosquitto.h': No such file or directory
1>Done building project "Bad.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I turned on Tools->Options->Projects and Solutions->Build and Run->verbosity=Detailed, and compared the CL.exe commands issued.
To my surprise, the /I include option was NOT present on the failed build command, despite being in the Properties dialog "Command Line" page as pasted above.
The issue was that I was building in Debug, but for some reason the Properties dialog was editing Release options. So, while the "Command Line" page was showing the options that would be used were I building Release, they weren't used while building Debug.

How to use Standard Library Header Units in MSVC?

According to Microsoft ( https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=msvc-160 ), it implemented P1502R1 Standard library header units for C++20:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1502r1.html
in the latest Visual Studio 2019.
So one can write, e.g.:
import <vector>;
But if I do it in my Visual Studio 2019 16.10.3 (with /std:c++latest switch), the following compilation error arises:
error C7612: could not find header unit for 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\vector'
The complete command-line in Debug configuration:
/JMC /experimental:module /external:env:"EXTERNAL_INCLUDE" /permissive- /ifcOutput "x64\Debug\" /GS /W4 /Zc:wchar_t /ZI /Gm- /Od /sdl /Fd"x64\Debug\vc142.pdb" /Zc:inline /fp:precise /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MDd /std:c++latest /FC /Fa"x64\Debug\" /EHsc /nologo /Fo"x64\Debug\" /Fp"x64\Debug\Project1.pch" /diagnostics:column
Please help how to activate this new feature in MSVC?

Build an x64 DLL with Visual Studio

I'm trying to build my dll file as x64, here are the configurations I have done on VisualStudio:
I have set solution and project platform to x64:
According to this answer, I have changed naming convention from __cdecl to __fastcall:
According to this comment I have added the argument _WIN64 to Preprocessor Definitions:
But still, when I rebuild my project and open my dll file using notepad, I see PE L which means my dll file is 32 bit.
Here's the command line options of the C/C++ settings (for better reading, I put a couple of line breaks in there):
/JMC /permissive- /GS /W3 /Zc:wchar_t /ZI /Gm- /Od /sdl
/Fd"x64\Debug\vc142.pdb" /Zc:inline /fp:precise /D "_WIN64" /D "_DEBUG" /D
"_CONSOLE" /D "_WINDLL" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX-
/Zc:forScope /RTC1 /Gr /MDd /FC /Fa"x64\Debug\" /EHsc /nologo /Fo"x64\Debug\"
/Fp"x64\Debug\DllCall-CppConsoleApp-1.pch" /diagnostics:column
Linker settings:
What should I do?

How to make 2 Visual Studio C++ settings identical and remove managed targeted code assembling error?

I have 2 very similar projects (concole Visual C++) but in one of them in the Release Mode I am getting the error:
managed targeted code requires a '/clr' option
I am comparing the Properties C/C++ command line:
The error Project:
/Zi /nologo /W3 /WX- /O2 /Oy- /GL /D "WIN32" /D "NDEBUG" /D "_UNICODE" /D "UNICODE" /Gm- /MT /GS- /fp:precise /Zc:wchar_t /Zc:forScope /Fp"Release\airl.pch" /Fa"Release\" /Fo"Release\" /Fd"Release\vc100.pdb" /FU"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\System.Core.dll" /analyze- /errorReport:queue
The No Error (good) Project
/Zi /nologo /W3 /WX- /O2 /Oy- /GL /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm- /EHsc /MT /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fp"Release\prj.pch" /Fa"Release\" /Fo"Release\" /Fd"Release\vc100.pdb" /Gd /analyze- /errorReport:queue
There are a few differences, but I do not know how to remove
/FU"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\System.Core.dll"
What about Gd option in the good configuration?

Visual Studio 2010 LibTomCrypt Build or Library Linking Error

Using the the entire LibTomCrypt source, I've built a library file with Visual Studio 2010 which compiles without issue. However, when creating a simple test console application that links the with TomCrypt library, I receive a linker error for the following code:
Test Code:
#include <stdio.h>
#include <tomcrypt.h>
int main()
{
int Cipher;
register_cipher( &aes_desc );
Cipher = find_cipher( "aes" );
if( Cipher != CRYPT_OK )
return 0;
printf( "Cipher name: %s\n", cipher_descriptor[ Cipher ].name );
unregister_cipher( &aes_desc );
return 0;
}
Linker Error:
error LNK1120: 1 unresolved externals
error LNK2001: unresolved external symbol _aes_desc
Interestingly, the debug library build works perfectly with the test code. It is the release build of tomcrypt.lib that seems to be missing some symbols.
Now I'm not new to building and using library files, but I'm wondering, are there some specific compiler flags or precautions I can make to build the library in release mode and have it link correctly in my test program? Could it be that the static aes_desc structure, defined in the LibTomCrypt code, be missing from the release build of the library by way of some compiler optimization?
I hope someone can offer some insight for myself and anyone else experiencing this issue.
I just ran into a related issue today. The project configs for visual studio include custom steps for building aes.c, but only cover Debug builds. Once I made comparable provisions for Release builds, all was well.
Open the Visual Studio 2010 project in text editor and replace the custom build steps with the ones below. This will also fix some warnings for the Debug build:
<CustomBuild Include="src\ciphers\aes\aes.c">
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">cl /nologo /MTd /W3 /Gm /EHsc /ZI /Od /I "src\headers" /I "..\libtommath" /D "_DEBUG" /D "LTM_DESC" /D "WIN32" /D "_MBCS" /D "_LIB" /D "LTC_SOURCE" /D "USE_LTM" /Fp"Debug/libtomcrypt.pch" /Fo"Debug/" /Fd"Debug/" /FD /RTC1 /c %(FullPath)
cl /nologo /DENCRYPT_ONLY /MTd /W3 /Gm /EHsc /ZI /Od /I "src\headers" /I "..\libtommath" /D "_DEBUG" /D "LTM_DESC" /D "WIN32" /D "_MBCS" /D "_LIB" /D "LTC_SOURCE" /D "USE_LTM" /Fp"Debug/libtomcrypt.pch" /Fo"Debug/aes_enc.obj" /Fd"Debug/" /FD /RTC1 /c %(FullPath)</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Debug/aes.obj;Debug/aes_enc.obj;%(Outputs)</Outputs>
</CustomBuild>
<CustomBuild Include="src\ciphers\aes\aes.c">
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">cl /nologo /MT /W3 /O2 /I "src\headers" /I "..\libtommath" /D "NDEBUG" /D "LTM_DESC" /D "WIN32" /D "_MBCS" /D "_LIB" /D "LTC_SOURCE" /D "USE_LTM" /Fp"Release/libtomcrypt.pch" /Fo"Release/" /Fd"Release/" /FD /c %(FullPath)
cl /nologo /DENCRYPT_ONLY /MT /W3 /O2 /I "src\headers" /I "..\libtommath" /D "NDEBUG" /D "LTM_DESC" /D "WIN32" /D "_MBCS" /D "_LIB" /D "LTC_SOURCE" /D "USE_LTM" /Fp"Release/libtomcrypt.pch" /Fo"Release/aes_enc.obj" /Fd"Release/" /FD /c %(FullPath)</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Release/aes.obj;Release/aes_enc.obj;%(Outputs)</Outputs>
</CustomBuild>