简体   繁体   English

命令行中的Visual Studio

[英]Visual Studio from the command line

I am using Visual Studio 2010 to create a small c++ executable. 我正在使用Visual Studio 2010创建一个小的c ++可执行文件。 Since I now want to use a different editor I have to call cl.exe and link.exe from the command line. 由于我现在想使用其他编辑器,因此必须从命令行调用cl.exe和link.exe。

It is great that VS provides the used command-lines in Project->Properties->C/C++->Commandline and ->Linker->CommandLine VS在Project->Properties->C/C++->Commandline->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 ) 在编译器命令行中,我添加了/c并删除了文件-renaming-stuff /Fp /Fa /Fo /Fdhttp://msdn.microsoft.com/zh-cn/library/fwkeyyhe.aspx

In the linker command line I removed the /MANIFEST and /Manifest ( http://msdn.microsoft.com/en-us/library/y0zzbyt4.aspx ) 在链接器命令行中,我删除了/MANIFEST/Manifesthttp://msdn.microsoft.com/zh-cn/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. 生成的exe的工作方式与我使用GUI生成它的方式相同,但问题是由于某种原因,它是145kb而不是22kb Should I just accept that or is there something obvious that the GUI has and that I am missing? 我应该接受吗?或者GUI是否有明显的东西并且我缺少了?

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: 作为手动复制单个命令行参数的替代方法,您可以使用msbuild.exe从命令行构建整个解决方案:

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 . 有关msbuild的完整文档,请参见此处

I would try running that and see if you're getting the exe size you expect. 我会尝试运行它,看看您是否正在获得所需的exe大小。

You can see full build log, including command line switches, in the project output directory, program_name.log file. 您可以在项目输出目录program_name.log文件中看到完整的构建日志,包括命令行开关。 You can also build the project from command line: http://msdn.microsoft.com/en-us/library/vstudio/xee0c8y7.aspx 您也可以从命令行构建项目: http : //msdn.microsoft.com/zh-cn/library/vstudio/xee0c8y7.aspx

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. 实际上,原始海报显然是由他们自己弄清楚的,但是从命令行运行Visual Studio工具的关键部分是运行以下批处理文件以正确设置环境变量。 Otherwise, you will get an error about missing DLLs, and so on. 否则,您将收到有关缺少DLL的错误,依此类推。 (This is Visual Studio 2008, Windows 7, but other combinations may be quite similar.) (这是Visual Studio 2008,Windows 7,但其他组合可能非常相似。)

C:\\Program Files\\Microsoft Visual Studio 9.0\\VC\\bin\\vcvars32.bat 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 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. 您可以从命令行或.bat文件编译C ++项目。 This works for VS2019: 这适用于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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM