visual-studio 在 Visual Studio 中运行小型 C++ 程序而无需创建项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/880803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running small C++ programs in Visual Studio without creating projects
提问by user109134
Is there any way to build/run small C++ programs, in Visual Studio without creating projects?
有没有什么方法可以在不创建项目的情况下在 Visual Studio 中构建/运行小型 C++ 程序?
For example, if I have a file hello.cpp, can I compile it to hello.exewithout a project?
例如,如果我有一个文件hello.cpp,我可以在hello.exe没有项目的情况下编译它吗?
采纳答案by Michael Burr
GMan's ideaof having a 'sandbox' project is a good one, as it allows for easily trying out multi-file tests. I call mine "cppTest".
GMan拥有一个“沙箱”项目的想法很好,因为它允许轻松尝试多文件测试。我称我的为“cppTest”。
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.
但是,如果您只想编译您碰巧打开的任何 C 或 C++ 文件,只需创建一个简单的“外部工具”。实际上,这并不像它应该的那么简单。
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.0\Common7\Tools\vsvars32.bat"
echo Visual C/C++ 2008 (VC 9.0) Compile...
set CC="%ProgramFiles%\Microsoft Visual Studio 9.0\VC\bin\cl.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:
在“工具/外部工具...”对话框中,添加一个新项目并填写以下字段:
- Title:
&Compile File - Command:
c:\path\to\runcl.cmd - Arguments:
$(ItemPath) Initial Directory:
$(ItemDir)check the "Use Output Window" box
- 标题:
&Compile File - 命令:
c:\path\to\runcl.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.
现在您可以使用“工具”菜单中的“编译文件”项来编译当前打开的文件。您甚至可以双击输出窗口中的错误,将您带到有错误的行。
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).
这有一些限制,您可以通过使用批处理文件或使用 Visual Studio 宏(我对 VS 宏不太熟悉)来修复其中的一些限制。
- if you haven't saved the file, the compile will run against the most recent save. There's no option in the External Tools configuration to force a save.
- if you run the command and there's not a C or C++ file active, the batch file will fall over
- there are probably quite a few other areas where the batch file will get confused
- 如果您还没有保存文件,编译将针对最近保存的文件运行。外部工具配置中没有强制保存的选项。
- 如果您运行该命令并且没有处于活动状态的 C 或 C++ 文件,则批处理文件将失败
- 批处理文件可能还有很多其他地方会混淆
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.
像这样的批处理文件的好处在于,您还可以使用它来帮助将编译器集成到各种编辑器中,从而可以调用外部工具,例如 UltraEdit、Zeus Editor、EditPad Pro、PSPad、Programmer's Notepad 等。
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.
如果您愿意,我已经发布了一个批处理文件,用于将多个编译器集成到上述编辑器中。该批处理文件处理多个编译器,包括从第 6 版到第 9 版的各种 MSVC 编译器、Digital Mars、MinGW 和 Comeau 编译器(要使用的编译器由附加参数选择)。批处理文件非常复杂(不幸的是,这是具有任何复杂性的 Windows 批处理文件的性质)。但我发现它使得从各种编辑器运行这些东西变得非常简单。我可以快速按下我分配给编译器的几个键来针对 5 个不同的编译器编译单个文件,这样我就可以轻松测试兼容性。
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...
除了我发现它对我自己的目的有用之外,我不做任何承诺 - 如果你也这样做,那就太好了。否则,不要使用它...
回答by GManNickG
Within the actual IDE, I don't think it's possible to run a small program, you have to use the command line like Matt suggested.
在实际的 IDE 中,我认为不可能运行一个小程序,你必须像 Matt 建议的那样使用命令行。
The solution I have for this problem, is that I have one project on my computers called "sandbox", which just has a main.cpp, and it's where I can mess around.
我对这个问题的解决方案是,我的计算机上有一个名为“沙箱”的项目,它只有一个 main.cpp,我可以在那里乱七八糟。
It let's me try things out here and there, but I never have to keep starting a new project.
它让我在这里和那里尝试一些东西,但我永远不必继续开始一个新项目。
回答by Michael
One way to do this is to build a generic project such as sandbox, test, or even Hello World and then add and remove source files to change what you want to do. You can even remove the old source file and then add new to start over fresh. This is quick and easy while making it possible to return programs that are built.
一种方法是构建一个通用项目,例如沙箱、测试甚至 Hello World,然后添加和删除源文件以更改您想要执行的操作。您甚至可以删除旧的源文件,然后添加新的源文件以重新开始。这既快速又简单,同时可以返回已构建的程序。
回答by darclander
If you have a hello.cpp file and would like it to compile into a hello.exe file without creating a project in code::blocks or visual studio you could look into: https://courses.cs.washington.edu/courses/cse373/99au/unix/g++.html
如果您有一个 hello.cpp 文件并希望将其编译为 hello.exe 文件而不在 code::blocks 或 Visual Studio 中创建项目,您可以查看:https: //courses.cs.washington.edu/courses /cse373/99au/unix/g++.html
A guide on how to install g++ via MinGW on windows: http://www.codebind.com/cprogramming/install-mingw-windows-10-gcc/
如何在 Windows 上通过 MinGW 安装 g++ 的指南:http: //www.codebind.com/cprogramming/install-mingw-windows-10-gcc/
Then you would be able to compile a single hello.cpp file by opening your command prompt, navigating to your file (the hello.cpp file) wherever you have placed it and run the command g++ -o hello hello.cppwhich should result in a file with the name "hello.exe" in the same location as your hello.cpp.
然后,您将能够通过打开命令提示符,导航到您放置的文件(hello.cpp 文件)并运行命令来编译单个 hello.cpp 文件,该命令g++ -o hello hello.cpp应该会生成一个名为“hello”的文件.exe”与您的 hello.cpp 位于同一位置。
Note that this is not part of visual studio but is an easy way to just compile a single .cpp file if you would like to keep it simple.
请注意,这不是 Visual Studio 的一部分,但如果您想保持简单,则可以简单地编译单个 .cpp 文件。

