问题
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?
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.
Thanks for your help.
回答1:
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.
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.
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.
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).
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).
Hope this helps & correct me if I'm wrong anywhere.
回答2:
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 Line It has an example cl /EHsc simple.cpp
回答3:
- 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.
You're set!
回答4:
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.
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.
回答5:
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
#include<iostream>
#define __HelloWorld__
HelloWorld.cpp
#include"header.h"
#ifdef __HelloWorld__
int main() {
std::cout << "Hello World" << std::endl;
}
#endif
program2.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.
回答6:
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. Here there is a reference for you.
来源:https://stackoverflow.com/questions/16626536/can-i-compile-and-debug-run-a-single-c-file-in-visual-studio-2012-how-to-a