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

7

34

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.

Alight answered 18/5, 2013 at 16:28 Comment(0)
B
23

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().

Beestings answered 20/5, 2015 at 11:33 Comment(0)
M
11
  1. Right click the File.
  2. Go to Properties of the particular file which you don't want to run.
  3. In Configuration Properties, go to General.
  4. Set "Excluded from Build" to YES.
  5. Click Apply.
  6. And then Load the Windows Debugger.

You're set!

Mukund answered 22/4, 2015 at 0:46 Comment(0)
O
10

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

Odericus answered 18/5, 2013 at 16:31 Comment(1)
And how to tell it to still maintain context of my project? Eg. I wrote some data interface in a GUI project, can I test it in standalone file, instead of running the huge GUI app?Conceptualism
T
8

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.

Traverse answered 18/5, 2013 at 16:52 Comment(1)
This is the first time that I'm able to see a very real and life-saving use of macros.Deflocculate
S
5

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.

Surber answered 18/5, 2013 at 16:35 Comment(2)
It's ridiculous needing to create multiple files just to test a few lines of code.Tungusic
@KyleDelaney Exactly. Not mentioning some project where main() is generated by some library.Conceptualism
T
0

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.

Tuber answered 18/5, 2013 at 16:32 Comment(0)
N
0

I can only find the solution for Visual Studio 2019 so it may not work in VS 2012. You can go to Solution Explorer, remove all other files (just remove, not delete) then add the new file. Then you'll be able to compile it.

Nunes answered 27/5, 2021 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.