Build dll from command line [ Visual Studio ] - visual-studio

I have developed a DLLin c++ using visual studio 2008. I want to run static code analysis on it using a tool which requires me to compile it on command line.
How can i use cl.exe to achieve this.
My project depends on the boost date_time library and couple other referenced projects.
How to build a DLL from the command line in Windows using MSVC
UPDATE: command line
cl /Od /GL /I "..\..\..\..\..\..\..\..\..\..\..\..\..\boost144" /I "..\include" /I "..\..\..\..\..\cincludes" /I "..\..\BASBESUtil\include" /I "..\..\..\..\..\..\..\..\..\..\..\wutility\include" /I "..\..\..\t\include" /I "..\..\..\..\..\..\..\..\..\..\..\enterprise\common\LicenseLib" /I "..\..\Common" /I "..\\" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "UNICODE" /D "_UNICODE_" /D "_WINDLL" /D "_UNICODE" /FD /EHa /MD /Zc:wchar_t- /Fo"Release\\" /Fd"Release\vc90.pdb" /W3 /c /Wp64 /Zi /TP <<src files >> /OUT:".\Release\snaplv.dll" /INCREMENTAL:NO /LIBPATH:"..\..\..\..\..\..\..\..\..\..\..\..\..\boost144\stage\lib" /DLL /MANIFEST /MANIFESTFILE:"Release\snaplv.dll.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"c:\DevEnv\PerforceWorkspace\dev\adamodarachar\snap-tools\LicenseValidator\main\enterprise\BAS\main\server\nativeutil\src\core\main\cpp\xyxmgr\xyxmgr\Release\snaplv.pdb" /SUBSYSTEM:CONSOLE /OPT:REF /OPT:ICF /LTCG /DYNAMICBASE:NO /MACHINE:X86 /NXCompat /DynamicBase version.lib Ws2_32.lib rpcrt4.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib <<obj files>>

Instead of calling the compiler executable directly and passing all those options manually, you can simply call MSVC, and pass the project file on the command line.
devenv.com MyProject.sln /Build "Release|x86_64" /Project MyLib
In my copy of MSVS v8, devenv.com can be found in C:\Programs\Microsoft Visual Studio 8\Common7\IDE. Call devenv.com /? for a list of options.

From the Tools menu select the option Visual Studio 2008 Command Prompt ?

Related

How to ensure that the /EDITANDCONTINUE directive is not ignored

I get this message when I try to edit and continue in VSC15:
'file.cpp' in 'LIB.DLL' was not linked with Edit and Continue enabled.
Ensure that /INCREMENTAL linking is enabled, and the /EDITANDCONTINUE directive is not ignored.
I've already ensured that /INCREMENTAL is enabled but can't figure out the second part.
Compiler command line:
/Yu"stdfx.h" /GS /analyze- /W3 /Gy /Zc:wchar_t /ZI /Gm- /Od /Fd".\Debug\vc140.pdb" /Zc:inline /fp:fast /D "x86" /D "WIN32" /D "_WINDOWS" /D "DEBUG" /D "_UNICODE" /D "UNICODE" /D "_WINDLL" /errorReport:none /WX- /Zc:forScope /RTC1 /GR /Gd /Oy- /MTd /Fa".\Debug\" /EHsc /Fo".\Debug\" /Fp".\Debug\LIB.pch"
Linker command line:
/OUT:".\Debug\LIB.dll" /MANIFEST:NO /NXCOMPAT /PDB:".\Debug\LIB.pdb" /DYNAMICBASE /DEF:"EXPORT.DEF" /IMPLIB:".\Debug\LIB.lib" /DLL /MACHINE:X86 /NODEFAULTLIB:"libc.lib" /OPT:REF /SAFESEH /INCREMENTAL /PGD:".\Debug\LIB.pgd" /SUBSYSTEM:WINDOWS /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:".\Debug\LIB.dll.intermediate.manifest" /MAP /OPT:ICF
Looking at the command lines:
Compiler command line: Edit and Continue isn't really compatible with /Gm-, it requires "Enable Minimal Rebuild" (/Gm).
Linker command line: /OPT:REF, /SAFESEH, /OPT:ICF are all incompatible with Edit and Continue and should cause LNK4075.
If you try a clean build of LIB.dll, you should see warnings such as:
1>LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/OPT:REF' specification
1>ConsoleApplication1.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/OPT:ICF' specification
Try turning off SAFESEH on the advanced page of the linker settings.
/SAFESEH:NO
The complete official answer for vs2015 case is here https://blogs.msdn.microsoft.com/vcblog/2016/07/01/c-edit-and-continue-in-visual-studio-2015-update-3/ Also it may be helpful to read this one https://blogs.msdn.microsoft.com/vcblog/2013/10/29/the-visual-c-linker-best-practices-developer-iteration/
As for my case I haven't seen any uncompatible flags in linking command line, and it turned out that
/LTCG
is turned on by default, so I had to turn it of manually with additional linker option in every project of my solution
/LTCG:OFF
I had the same problem, did all steps described above but no luck.
I use VS2017.
Helped next: you have to specify /ZI for each specific *.cpp file in your project:
right click on the *.cpp file in the Solution Explorer
Properties > C/C++ > General > Debug Information Format = Program Database for Edit And Continue (/ZI)

CMake fails to configure

Why isn't CMake working?
CMake build message:
The C compiler identification is MSVC 16.0.30319.1
Check for working C compiler using: Visual Studio 10 2010
Check for working C compiler using: Visual Studio 10 2010 -- broken
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.4/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler "C:/Program Files (x86)/Microsoft Visual Studio
10.0/VC/bin/cl.exe" is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Users/theWindows7User/Desktop/glfw-3.1.2/CMakeFiles/CMakeTmp
Run Build
Command:"C:/Windows/Microsoft.NET/Framework/v4.0.30319/MSBuild.exe"
"cmTC_318a4.vcxproj" "/p:Configuration=Debug" "/p:VisualStudioVersion=10.0"
Microsoft (R) Build Engine version 4.0.30319.34209
[Microsoft .NET Framework, version 4.0.30319.34209]
Copyright (C) Microsoft Corporation. All rights reserved.
Build started 01-Jan-2016, Friday 6:35:52 AM.
Project
"C:\Users\theWindows7User\Desktop\glfw-3.1.2\CMakeFiles\CMakeTmp\cmTC_318a4.vcxproj"
on node 1 (default targets).
PrepareForBuild:
Creating directory "cmTC_318a4.dir\Debug\".
Creating directory "C:\Users\theWindows7User\Desktop\glfw-3.1.2\CMakeFiles\CMakeTmp\Debug\".
InitializeBuildStatus:
Creating "cmTC_318a4.dir\Debug\cmTC_318a4.unsuccessfulbuild" because "AlwaysCreate" was specified.
ClCompile:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\CL.exe /c /Zi /W3 /WX- /Od /Ob0 /Oy- /D WIN32 /D _WINDOWS /D _DEBUG /D "CMAKE_INTDIR=\"Debug\"" /D _MBCS /Gm- /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"cmTC_318a4.dir\Debug\\" /Fd"cmTC_318a4.dir\Debug\vc100.pdb" /Gd /TC /analyze- /errorReport:queue testCCompiler.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
cl /c /Zi /W3 /WX- /Od /Ob0 /Oy- /D WIN32 /D _WINDOWS /D _DEBUG /D "CMAKE_INTDIR=\"Debug\"" /D _MBCS /Gm- /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"cmTC_318a4.dir\Debug\\" /Fd"cmTC_318a4.dir\Debug\vc100.pdb" /Gd /TC /analyze- /errorReport:queue testCCompiler.c
testCCompiler.c
ManifestResourceCompile:
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\rc.exe /nologo /fo"cmTC_318a4.dir\Debug\cmTC_318a4.exe.embed.manifest.res" cmTC_318a4.dir\Debug\cmTC_318a4_manifest.rc
Link:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\link.exe /ERRORREPORT:QUEUE /OUT:"C:\Users\theWindows7User\Desktop\glfw-3.1.2\CMakeFiles\CMakeTmp\Debug\cmTC_318a4.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /ManifestFile:"cmTC_318a4.dir\Debug\cmTC_318a4.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:/Users/theWindows7User/Desktop/glfw-3.1.2/CMakeFiles/CMakeTmp/Debug/cmTC_318a4.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:/Users/theWindows7User/Desktop/glfw-3.1.2/CMakeFiles/CMakeTmp/Debug/cmTC_318a4.lib" /MACHINE:X86 cmTC_318a4.dir\Debug\cmTC_318a4.exe.embed.manifest.res
cmTC_318a4.dir\Debug\testCCompiler.obj /machine:X86 /debug
LINK : fatal error LNK1123: failure during conversion to COFF: file invalid
or corrupt
[C:\Users\theWindows7User\Desktop\glfw-3.1.2\CMakeFiles\CMakeTmp\cmTC_318a4.vcxproj]
Done Building Project
"C:\Users\theWindows7User\Desktop\glfw-3.1.2\CMakeFiles\CMakeTmp\cmTC_318a4.vcxproj"
(default targets) -- FAILED.
Build FAILED.
"C:\Users\theWindows7User\Desktop\glfw-3.1.2\CMakeFiles\CMakeTmp\cmTC_318a4.vcxproj"
(default target) (1) ->
(Link target) ->
LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt [C:\Users\theWindows7User\Desktop\glfw-3.1.2\CMakeFiles\CMakeTmp\cmTC_318a4.vcxproj]
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:00.43
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:1 (project)
Configuring incomplete, errors occurred!
See also "C:/Users/theWindows7User/Desktop/glfw-3.1.2/CMakeFiles/CMakeOutput.log".
See also "C:/Users/theWindows7User/Desktop/glfw-3.1.2/CMakeFiles/CMakeError.log".
LINK : fatal error LNK1123: failure during conversion to COFF: file invalid
It's a classic Visual Studio issue. You have to install the service pack to fix it. See here: https://msdn.microsoft.com/en-us/library/7dz62kfh%28v=vs.110%29.aspx

Error in premake generated linker command for (at least) visual studio 2010 express

There seems to be an error when generating VS2010 projects from premake, even with the "Hello World" example (Also tried with clink (here) and premake itself)
hello.c
#include <stdio.h>
int main(void) {
puts("Hello, word!");
return 0;
}
premake4.lua
-- premake4.lua
solution "HelloWorld"
configurations "Any"
project "HelloWorld"
kind "ConsoleApp"
language "C"
files "*.c"
defines { "DEBUG" }
flags { "Symbols" }
This simple example fails to link with the message "LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt"
The logfile says:
Build started 09-02-2015 16:06:34.
1>Project "E:\D\Source\premake-dev\pm-test\HelloWorld.vcxproj" on node 2 (build target(s)).
1>InitializeBuildStatus:
Creating "obj\Any\HelloWorld.unsuccessfulbuild" because "AlwaysCreate" was specified.
ClCompile:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /Od /Oy- /D DEBUG /D _MBCS /Gm /EHsc /RTC1 /MDd /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Fo"obj\Any\\" /Fd".\HelloWorld.pdb" /Gd /TC /analyze- /errorReport:prompt hello.c
hello.c
ManifestResourceCompile:
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\rc.exe /nologo /fo"obj\Any\HelloWorld.exe.embed.manifest.res" obj\Any\HelloWorld_manifest.rc
Link:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:".\HelloWorld.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /ManifestFile:"obj\Any\HelloWorld.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"E:\D\Source\premake-dev\pm-test\HelloWorld.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /ENTRY:"mainCRTStartup" /DYNAMICBASE /NXCOMPAT /IMPLIB:".\HelloWorld.lib" /MACHINE:X86 obj\Any\HelloWorld.exe.embed.manifest.res
obj\Any\hello.obj
1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
1>Done Building Project "E:\D\Source\premake-dev\pm-test\HelloWorld.vcxproj" (build target(s)) -- FAILED.
Build FAILED.
Time Elapsed 00:00:00.33
SOLVED!!
It was an error in my pristine VS2010 install!! See
https://social.msdn.microsoft.com/Forums/vstudio/en-US/d10adba0-e082-494a-bb16-2bfc039faa80/vs2012-rc-installation-breaks-vs2010-c-projects?forum=vssetup
Although i never installed anything but VS2010 ..
SOLUTION: From this page:
http://blogs.msdn.com/b/heaths/archive/2011/04/01/visual-c-2010-sp1-compiler-update-for-the-windows-sdk-7-1.aspx
Copy c:\windows\Microsoft.Net\Framework\v4.0.30319\ cvtres.exe (42Kb)
into Programs(x86)\Microsoft Visual Studio 10.0\VC\Bin\

vc6 project is tyring to link against mfc42.lib under debug mode in vs2008

I have my project converted from vc6 to vs2008。 When i rebuild it under "debug" mode, an link error occurs. " fatal error LNK1104: cannot open file 'mfc42.lib'".
I donot know why.
why the compiler would link against 'mfc42.lib'?
Isn't it supposed to link against 'mfc42d.lib' ??
I have checked my project properties. See the followings:
C/C++:
/Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "USER_CLASS=AFX_EXT_CLASS" /D "GROUP_CLASS=AFX_EXT_CLASS" /D "_VC80_UPGRADE=0x0600" /D "_AFXDLL" /D "_MBCS" /Gm /EHsc /RTC1 /MDd /Yu"stdafx.h" /Fp".\Debug/SunyRTM.pch" /Fo".\Debug/" /Fd".\Debug/" /FR".\Debug/" /W3 /nologo /c /ZI /TP /errorReport:prompt
Linker:
/OUT:".._debug\ChiticRTMD.exe" /INCREMENTAL:NO /NOLOGO /MANIFEST /MANIFESTFILE:".\Debug\ChiticRTMD.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:".\Debug/ChiticRTMD.pdb" /MAP:".\Debug/ChiticRTMD.map" /SUBSYSTEM:WINDOWS /DYNAMICBASE:NO /MACHINE:X86 /ERRORREPORT:PROMPT "..\SNetDrv\Debug\SNetDrvD.lib" "..\RdbRW2008\Debug\RdbRWD.lib" "..\IOC\Debug\IOCD.lib"
when I compared these command lines to another project converted from vc6 to vs2008 and they appear similiarly. However, that project (not current one) is able to run correctly under 'debug' mode.
It is rather confusing , who can help me?

LNK2001 error when compiling on command line, but not in IDE

I have a large VC++ project developed in Visual Studio 2010 that I need to be able to build from the command line using CL.exe. In the IDE, the project builds successfully, but I haven't been able to build it on the command line.
I've mostly dealt with linker errors and have gotten it down to one:
LINK : error LNK2001: unresolved external symbol _WinMainCRTStartup
C:\<path_to>\Debug\LargeProject.exe : fatal error LNK1120: 1 unresolved externals
I found in the debug folder a file called "LargeProject.log" that shows the precise command line arguments that get called from the IDE when the IDE builds. I've listed them here, if it helps.
(See edit notes at bottom of this question for some updates)
CL.exe /c /Zi /nologo /W3 /WX- /Od /Oy- /D WIN32 /D _WINDOWS /D _DEBUG /D _MBCS /D _AFXDLL /Gm- /EHa /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Yc"StdAfx.h" /Fp"Debug\LargeProject.pch" /Fo"Debug\\" /Fd"Debug\vc100.pdb" /Gd /TP /analyze- /errorReport:prompt "Source Files\stdafx.cpp" /wd4482 > testDump.txt
CL.exe /c /Zi /nologo /W3 /WX- /Od /Oy- /D WIN32 /D _WINDOWS /D _DEBUG /D _MBCS /D _AFXDLL /Gm- /EHa /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Yu"StdAfx.h" /Fp"Debug\LargeProject.pch" /Fo"Debug\\" /Fd"Debug\vc100.pdb" /Gd /TP /analyze- /errorReport:prompt <list of all source files except the following> /wd4482 > testDump.txt
CL.exe /c /Zi /nologo /W3 /WX- /Od /Oy- /D WIN32 /D _WINDOWS /D _DEBUG /D _MBCS /D _AFXDLL /Gm- /EHa /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Yu"StdAfx.h" /Fp"Debug\LargeProject.pch" /Fo"Debug\\" /Fd"Debug\vc100.pdb" /Gd /TP /analyze- /errorReport:prompt "Source Files\LargeProject.cpp" /wd4482 > testDump.txt
rc.exe /D _DEBUG /D _AFXDLL /l"0x0409" /I"Debug\\" /nologo /fo"Debug\LargeProject.res" LargeProject.rc > testDump.txt
rc.exe /nologo /fo"Debug\LargeProject.exe.embed.manifest.res" "Debug\LargeProject_manifest.rc" > testDump.txt
link.exe /ERRORREPORT:PROMPT /OUT:"C:\<path_to>\Debug\LargeProject.exe" /INCREMENTAL /NOLOGO <list of libraries used> /MANIFEST /ManifestFile:"Debug\LargeProject.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:\<path_to>\Debug\LargeProject.pdb" /SUBSYSTEM:WINDOWS /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\<path_to>\Debug\LargeProject.lib" /MACHINE:X86 "Debug\LargeProject.res" > testDump.txt
// note that I don't run any of the commands below because the above command
// threw an error.
mt.exe /nologo /verbose /out:"Debug\LargeProject.exe.embed.manifest" /manifest "Debug\LargeProject.exe.intermediate.manifest" "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\Include\Manifest\dpiaware.manifest" > testDump.txt
rc.exe /nologo /fo"Debug\LargeProject.exe.embed.manifest.res" "Debug\LargeProject_manifest.rc" > testDump.txt
link.exe /ERRORREPORT:PROMPT /OUT:"C:\<path_to>\Debug\LargeProject.exe" /INCREMENTAL /NOLOGO <list of libraries used> /MANIFEST /ManifestFile:"Debug\LargeProject.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:\<path to>\Debug\LargeProject.pdb" /SUBSYSTEM:WINDOWS /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\<path_to>\Debug\LargeProject.lib" /MACHINE:X86 "Debug\LargeProject.res" > testDump.txt
As you can see, I ran each of these in sequence (until I got an error) and dumped the contents to testDump.txt. Here's testDump.txt's contents after the first link.exe command
LINK : error LNK2001: unresolved external symbol _WinMainCRTStartup
C:\<path_to>\Debug\LargeProject.exe : fatal error LNK1120: 1 unresolved externals
I'm confused that I'm getting this error on the command line, but not from the IDE, which is presumably just running these commands behind the scenes. It's also worth noting that I've run the vcvars32.bat file from my command prompt window, so it can't be a local environment variable issue.
Edit:
I've tried adding msvcrt.lib to the list of libraries in the link command. This produced a new linker error:
msvcrt.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain#16 referenced in function ___tmainCRTStartup
C:\<path_to>\Debug\LargeProject.exe : fatal error LNK1120: 1 unresolved externals
I've read that this means the proper entry point wasn't defined in the project. ie, that there should be a WinMain() function in the project to serve as the the entry point for the application. That seems logical, but I haven't found that function and, to reiterate, the project compiles and links fine in the IDE, and presumably the log file I got these commands from is merely listing what the IDE was doing anyway.
There seem to be other CRT libraries. I tried msvcprt.lib, but that yeilded an identical error message to before I had added anything. The other similar .lib files that exist are msvcmrt.lib,msvcmrtd,msvcprtd,msvcrtd,msvcurt,and msvcurtd. What are the differences between these library files, and how does someone choose which one to use?
I had the same error and I know how to fix it.
In your project, you choose a non GUI project : it is a mistake and to fix it choose (I dont known the exact names, I use VS in French)
option->Link options->System->Sub-système=Windows
and it should work

Resources