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.exe
without a project?
例如,如果我有一个文件hello.cpp,我可以在没有项目的情况下将其编译为hello.exe吗?
4 个解决方案
#1
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".
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)
初始目录:$(ItemDir)
-
check the "Use Output Window" box
选中“使用输出窗口”框
标题:&编译文件
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...
除了我发现它对我自己的目的有用之外,我没有做出任何承诺 - 如果你也这样做,那很好。否则,不要使用它......
#2
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.
我遇到这个问题的解决方案是,我的计算机上有一个名为“sandbox”的项目,它只有一个main.cpp,它就是我可以乱搞的地方。
It let's me try things out here and there, but I never have to keep starting a new project.
让我在这里和那里尝试一下,但我永远不必继续开始一个新项目。
#3
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/
关于如何通过MinGW在Windows上安装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.cpp
which 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文件即可。
#4
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.
一种方法是构建一个通用项目,如sandbox,test,甚至Hello World,然后添加和删除源文件以更改您想要执行的操作。您甚至可以删除旧的源文件,然后添加新的以重新开始。这样可以快速轻松地返回构建的程序。
#1
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".
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)
初始目录:$(ItemDir)
-
check the "Use Output Window" box
选中“使用输出窗口”框
标题:&编译文件
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...
除了我发现它对我自己的目的有用之外,我没有做出任何承诺 - 如果你也这样做,那很好。否则,不要使用它......
#2
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.
我遇到这个问题的解决方案是,我的计算机上有一个名为“sandbox”的项目,它只有一个main.cpp,它就是我可以乱搞的地方。
It let's me try things out here and there, but I never have to keep starting a new project.
让我在这里和那里尝试一下,但我永远不必继续开始一个新项目。
#3
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/
关于如何通过MinGW在Windows上安装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.cpp
which 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文件即可。
#4
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.
一种方法是构建一个通用项目,如sandbox,test,甚至Hello World,然后添加和删除源文件以更改您想要执行的操作。您甚至可以删除旧的源文件,然后添加新的以重新开始。这样可以快速轻松地返回构建的程序。