Frage

I am using Visual Studio 2010 to create a small c++ executable. Since I now want to use a different editor I have to call cl.exe and link.exe from the command line.

It is great that VS provides the used command-lines in Project->Properties->C/C++->Commandline and ->Linker->CommandLine
however, they dont work quite like they should:

In the compiler command line I added /c and removed the file-renaming-stuff /Fp /Fa /Fo /Fd (http://msdn.microsoft.com/en-us/library/fwkeyyhe.aspx)

In the linker command line I removed the /MANIFEST and /Manifest (http://msdn.microsoft.com/en-us/library/y0zzbyt4.aspx)

the result is this:

cl.exe main.cpp /c /Zi /nologo /W3 /WX- 
    /O1 /Oi /Os /Oy /GL 
    /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" 
    /Gm- /EHsc /GS /Gy /fp:precise 
    /Zc:wchar_t /Zc:forScope /Gd /analyze- /errorReport:queue 


link.exe *.obj /OUT:"test2.exe" /INCREMENTAL:NO /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" 
    /ALLOWISOLATION /MANIFESTUAC:"level='asInvoker' uiAccess='false'" 
    /DEBUG /PDB:"F:\prj\visualstudio2010\test2\Release\test2.pdb" 
    /SUBSYSTEM:CONSOLE /OPT:REF /OPT:ICF 
    /PGD:"F:\prj\visualstudio2010\test2\Release\test2.pgd" 
    /LTCG /TLBID:1 /DYNAMICBASE /NXCOMPAT /MACHINE:X86 /ERRORREPORT:QUEUE 

The resulting exe works the same way as if i had built it using the GUI, but the problem is that it is 145kb instead of 22kb for some reason. Should I just accept that or is there something obvious that the GUI has and that I am missing?

War es hilfreich?

Lösung 2

You can see full build log, including command line switches, in the project output directory, program_name.log file. You can also build the project from command line: http://msdn.microsoft.com/en-us/library/vstudio/xee0c8y7.aspx

Andere Tipps

As an alternative to manually reproducing the individual command line args you can use msbuild.exe to build an entire solution from the command line:

On my machine the command would look like the following:

"c:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe" /t:Build /verbosity:minimal /maxcpucount /p:Configuration=Debug;Platform="x64" "C:\path\to\some_sln.sln"

Full documentation on msbuild can be found here.

I would try running that and see if you're getting the exe size you expect.

Actually, it's something the original poster apparently figured out on their own but a critical part of running Visual Studio tools from the command line is to run the following batch file to set your environment variables correctly. Otherwise, you will get an error about missing DLLs, and so on. (This is Visual Studio 2008, Windows 7, but other combinations may be quite similar.)

C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat

which simply runs:

C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat

You can compile a C++ project from a command line or .bat file. This works for VS2019:

rem path to Visual studio. No quotation marks:
set vspath=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
rem path to vcvars files 
set batpath=%vspath%\VC\Auxiliary\Build
rem set system path
path %batpath%;%path%
rem set all paths and include directories for 64-bit mode compilation
call vcvars64.bat x64
rem compile:
cl [options] test.cpp /Fetest.exe
rem pause to see if compilation succeeds
pause
rem run the compiled program
test.exe
pause
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top