我可以在 Visual Studio 2012 中编译和调试(运行)单个 C++ 文件吗?(如何避免创建过多的项目)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16626536/
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
Can I compile and debug (run) a single C++ file in Visual Studio 2012? (How to avoid creating too many projects)
提问by Sunjay Varma
I'm learning C++ out of a book and using Visual Studio 2012. In order to follow the book's exercises, I need to make multiple .cpp files with the main() function inside them. Is there any way I can compile/debug my programs without making a new project every single time?
我正在从一本书中学习 C++ 并使用 Visual Studio 2012。为了遵循本书的练习,我需要制作多个包含 main() 函数的 .cpp 文件。有什么方法可以编译/调试我的程序而不必每次都创建一个新项目?
For example, if I write a simple "Hello, World!" file and then decide to make something else that is really simple, can I avoid making a new project for each simple program? Is there any way to use Visual Studio 2012 just as a compiler? I would love it if I could just have everything inside a single project where I could compile whichever individual file I wanted and see it run.
例如,如果我写一个简单的“Hello, World!” 文件然后决定做一些非常简单的东西,我可以避免为每个简单的程序创建一个新项目吗?有没有办法将 Visual Studio 2012 用作编译器?如果我可以将所有内容都放在一个项目中,我会喜欢它,我可以在其中编译我想要的任何单个文件并查看它运行。
Thanks for your help.
谢谢你的帮助。
回答by PalashV
Although it's too late to add this answer, but it might be useful to the future-viewers. This is what I did -
虽然添加这个答案为时已晚,但它可能对未来的观众有用。这就是我所做的-
While trying to figure out how to use Visual Studio for the very same purpose you asked, I observed and found that for a C++ project, there should be only one starting point, that is, only one main()
function.
在试图弄清楚如何将 Visual Studio 用于您所问的相同目的时,我观察并发现对于 C++ 项目,应该只有一个起点,即只有一个main()
函数。
So, instead of creating a new project every time, just change the name of (main()
) functions in the unused C++ files to something else, like the filename or anything.
因此,不必每次都创建一个新项目,只需main()
将未使用的 C++ 文件中的 ( ) 函数名称更改为其他名称,例如文件名或任何其他内容。
For example, I first created my very first program hello_world.cpp
with a main()
function, and then compiled it, ran it & learned everything I could using it.
例如,我首先hello_world.cpp
用一个main()
函数创建了我的第一个程序,然后编译它,运行它并学习了我可以使用它的所有东西。
But now I want to create a new file for trying some other new thing (a new file learn_operators.cpp
with a main()
function of its own).
但是现在我想创建一个新文件来尝试其他一些新事物(一个learn_operators.cpp
具有main()
自己功能的新文件)。
So, before trying to compile & run learn_operators.cpp
, I'll change the name of main()
in hello_world.cpp
to, say, hello_world()
, and then build & run the project in the same manner as before, but this time only this new file will run as this is the (new) starting point for the project (that is, it includes main()
function).
因此,在尝试编译和运行之前learn_operators.cpp
,我会将main()
in的名称更改hello_world.cpp
为,例如,hello_world()
然后以与以前相同的方式构建和运行项目,但这次只有这个新文件会运行,因为这是 ( new) 项目的起点(即,它包括main()
功能)。
Hope this helps & correct me if I'm wrong anywhere.
如果我在任何地方错了,希望这可以帮助并纠正我。
回答by Neel Basu
To compile just make a cpp file. and use the cl
command line tool, Check the MSDN Link: Compile a Native C++ Program from the Command LineIt has an example cl /EHsc simple.cpp
编译只需制作一个cpp文件。并使用cl
命令行工具,检查MSDN 链接:从命令行编译本机 C++ 程序它有一个例子cl /EHsc simple.cpp
回答by Desai
- Right click the File.
- Go to Properties of the particular file which you don't want to run.
- In Configuration Properties, go to General.
- Set "Excluded from Build" to YES.
- Click Apply.
- And then Load the Windows Debugger.
- 右键单击文件。
- 转到您不想运行的特定文件的属性。
- 在配置属性中,转到常规。
- 将“从构建中排除”设置为是。
- 单击应用。
- 然后加载 Windows 调试器。
You're set!
你准备好了!
回答by Bar?? U?akl?
You can add all your cpp files to the same project with different file names then you can right click each file and exclude the files you don't want to get build.
您可以使用不同的文件名将所有 cpp 文件添加到同一个项目中,然后您可以右键单击每个文件并排除您不想构建的文件。
It is much better to have a project per application though.
不过,每个应用程序都有一个项目要好得多。
Alternatively you can have a single main file that calls your other functions in other files where you implement your exercises then you don't have to deal with anything, just implement new exercises in a new file and call it from main.
或者,您可以有一个主文件,它在您实现练习的其他文件中调用您的其他函数,然后您不必处理任何事情,只需在新文件中实现新练习并从主文件中调用它。
回答by tzwickl
You could also use conditional compilation to solve this problem. But I would really recommend you to make the effort to create a new project for each program.
你也可以使用条件编译来解决这个问题。但我真的建议您努力为每个程序创建一个新项目。
header.h
头文件.h
#include<iostream>
#define __HelloWorld__
HelloWorld.cpp
HelloWorld.cpp
#include"header.h"
#ifdef __HelloWorld__
int main() {
std::cout << "Hello World" << std::endl;
}
#endif
program2.cpp
程序2.cpp
#include"header.h"
#ifdef __program2__
int main() {
std::cout << "Program 2" << std::endl;
}
#endif
Now you can choose via #define
the program you want to run.
现在您可以通过#define
要运行的程序进行选择。
回答by FeliceM
If you right click on the solution name in the project browser window in the right pan, you should be able to add projects under your existing one. However, it is much better to start a new project for each exercise. Herethere is a reference for you.
如果您在右侧窗格的项目浏览器窗口中右键单击解决方案名称,您应该能够在现有项目下添加项目。但是,最好为每个练习开始一个新项目。这里有一个供您参考。