在 Visual Studio 中运行小型 C++ 程序而无需创建项目
有没有什么方法可以在不创建项目的情况下在 Visual Studio 中构建/运行小型 C++ 程序?
Is there any way to build/run small C++ programs, in Visual Studio without creating projects?
例如,如果我有一个文件hello.cpp
,我可以在没有项目的情况下将其编译为hello.exe
吗?
For example, if I have a file hello.cpp
, can I compile it to hello.exe
without a project?
推荐答案
GMan 的想法 拥有一个沙箱"项目是一个很好的想法,因为它允许轻松尝试多文件测试.我称我的为cppTest".
GMan's idea of having a 'sandbox' project is a good one, as it allows for easily trying out multi-file tests. I call mine "cppTest".
但是,如果您只想编译您碰巧打开的任何 C 或 C++ 文件,只需创建一个简单的外部工具"即可.实际上,事情并没有想象中的那么简单.
However, if you just want to be able to compile whatever C or C++ file you happen to have open, just create a simple "External Tool". Actually, it's not as simple as it probably should be.
首先,创建一个批处理文件,用于设置编译器环境并运行编译器.类似于以下内容:
First, create a batch file that will set up the compiler environment and run the compiler. Something like the following:
@rem - runcl.cmd
@rem a batch file to drive simple VC9 compiles
#rem
@echo off
set LIBRARIES=kernel32.lib user32.lib advapi32.lib shlwapi.lib oleaut32.lib
set WIN32_WINNT=0x0500
call "%ProgramFiles%Microsoft Visual Studio 9.0Common7Toolsvsvars32.bat"
echo Visual C/C++ 2008 (VC 9.0) Compile...
set CC="%ProgramFiles%Microsoft Visual Studio 9.0VCincl.exe" /Zi /EHsc -D_WIN32_WINNT=%WIN32_WINNT% %1 /link /incremental:no %LIBRARIES%
echo %CC%
%CC%
在工具/外部工具..."对话框中,添加一个新项目并填写以下字段:
In the "Tools/External Tools..." dialog, add a new item and fill in the following fields:
- 标题:
&编译文件
- 命令:
c:path ouncl.cmd
- 参数:
$(ItemPath)
初始目录:
$(ItemDir)
选中使用输出窗口"框
现在您可以使用工具"菜单中的编译文件"项来编译当前打开的文件.您甚至可以双击输出窗口中的错误,将您带到有错误的行.
Now you can use the "Compile File" item in the Tools menu to compile whatever is the currently open file. You can even double click on the errors in the output window to take you to the lines with errors.
这有一些限制,您可以通过创建批处理文件或使用 Visual Studio 宏来修复其中一些限制(我对 VS 宏不是很熟悉).
There are some limitations with this some of which you can fix by fancying up the batch file or maybe with a Visual Studio macro (I'm not very familiar with VS macros).
- 如果您还没有保存文件,编译将针对最近保存的文件运行.外部工具配置中没有强制保存的选项.
- 如果您运行该命令并且没有处于活动状态的 C 或 C++ 文件,则批处理文件将失败
- 批处理文件可能还有很多其他地方会混淆
像这样的批处理文件的好处在于,您还可以使用它来帮助将编译器集成到各种编辑器中,以便您调用外部工具,例如 UltraEdit、Zeus Editor、EditPad Pro、PSPad、Programmer's Notepad 等.等
The nice thing about a batch file like this is that you can also use it to help integrate the compiler into various editors that let you call external tools, like UltraEdit, Zeus Editor, EditPad Pro, PSPad, Programmer's Notepad, etc. etc.
如果您愿意,我已经发布了一个批处理文件,我用来将多个编译器集成到上述编辑器中.该批处理文件处理多个编译器,包括从第 6 版到第 9 版的各种 MSVC 编译器、Digital Mars、MinGW 和 Comeau 编译器(要使用的编译器由附加参数选择).批处理文件非常复杂(不幸的是,这是具有任何复杂性的 Windows 批处理文件的性质).但我发现它使得从各种编辑器运行这些东西变得非常简单.我可以快速按下分配给编译器的几个键来针对 5 个不同的编译器编译单个文件,这样我就可以轻松测试兼容性.
If you like, I've posted a batch file that I use to integrate several compilers into editors like the above. This batch file handles several compilers including various MSVC compilers from version 6 through version 9, Digital Mars, MinGW and Comeau compilers (the compiler to use is selected by an additional parameter). The batch file is pretty convoluted (unfortunately, that's the nature of Windows batch files that have any kind of complexity). But I find it makes running these things from various editors pretty simple. I can quickly hit a few keys that I've assigned to the compilers to compile a single file against 5 different compilers so I can test for compatibility easily.
除了我觉得它对我自己的目的有用之外,我不做任何承诺 - 如果你也这样做,那就太好了.否则,不要使用它...
I make no promises about it other than I find it useful for my own purposes - if you do too, great. Otherwise, don't use it...
- https://gist.github.com/mburr/3308168
相关文章