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.
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 .
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()
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With