Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I compile and debug (run) a single C++ file in Visual Studio 2012? (How to avoid creating too many projects)

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.

like image 951
Sunjay Varma Avatar asked May 18 '13 16:05

Sunjay Varma


People also ask

How do I debug a specific file in Visual Studio?

Try this: go to File->New->Project... and pick a win32 console application. In the next wizard, go to Application Settings , and check "Empty Project", and hit OK. Now you have empty project and solution files which you can copy/paste wherever you want them; to debug, just open the . sln file, drag in your single .


2 Answers

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.


Update: One other way is to keep one file with main(), create classes for the other project code, including any new file/code added to the project, and then call those classes from main(). This way, any piece of code other than main() stays wrapped in classes, and only the code in main() gets changed a bit every time new code has to be called, instead of renaming the functions to main().

like image 141
PalashV Avatar answered Sep 20 '22 08:09

PalashV


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

like image 37
Neel Basu Avatar answered Sep 20 '22 08:09

Neel Basu