compile single c++ source file in 1 project in visual studio

3.4k Views Asked by At

i know a lot of people asked this question, but i can't find how to do it. Is there
a way to build only one source file in visual studio 2017? without new project, i'm learning c++, so i can't make huge thing now, just focus to code(now i'm learn data structure and algorithm),most of my exercise is about <200 code lines, so it great to compile new file without whole project, sometimes i need a few lines of code to test my algorithm,please help me, thanks all you guy, because v.s is very good ide so i want to stick with it.

3

There are 3 best solutions below

0
On

On the source file you don't want to be included in the project, simply right click, select Properties. There you will find in General a field 'Excluded From Build'. Type true/yes there and the source file will be deactivated.

0
On

If you don't know how to create a new project and a new solution, it will be good to learn those basic concepts and use them to write, test, and debug your code.

You can use one Visual Studio project to do all the learning.

Let's say you want to test "algorithm 1". Then,

  1. Create a header file for it and a source file for it -- call them "test-algorithm-1.hpp" and "test-algorithm-1.cpp".

  2. Add them to the project.

  3. #include the header file in the main .cpp file of the project.

  4. Call the function to test "algorithm 1" from main.


#include "test-algorithm-1.hpp"

int main()
{
   test_algorithm_1();
}

When you are ready for testing "algorithm 2", repeat the above steps. The main .cpp file can now be.

#include "test-algorithm-1.hpp"
#include "test-algorithm-2.hpp"

int main()
{
   test_algorithm_1();
   test_algorithm_2();
}

If you want to avoid testing "algorithm 1" while testing "algorithm 2", simply comment out the corresponding line in main.

int main()
{
   // test_algorithm_1();
   test_algorithm_2();
}
0
On

If you just have one file and want to build it without waiting 1-2 minutes for the IDE to pop up,

  1. Find the Developer Command Prompt in your list of applications - it is under the Visual Studio directory in the Application menu.
  2. cd /d to your directory. cd will take you here if you are on the same drive as visual studio. If you are on a different drive, use cd /d.
  3. Use your favourite editor (notepad, vim, geany, notepad++, nano, microemacs etc) to create the file.
  4. cl sourcefile
  5. Run the excutable.

Unlike what visual studio does, you executable will now be in the same directory as your source. Editors like geany have a build button (the brick icon). All you need to do is fill in how to build: in this case, the cl command.

If you want a one file project, just follow these steps.

  1. Create New Project - File -> New -> Project
  2. Fill in filename, select Win32 Console Application. Note the directory - if it is not where you want it, change it. Click OK
  3. Application Wizard pops up, click Next
  4. Application settings - select Empty project, click Finish
  5. Open Solution Explorer. Right click Source Files. Menu pops up, select Add -> New Item
  6. Add new item dialog pops up, fill in your filename.