skip to Main Content

How do I run a C++ program separately in Visual Studio 2022. I used to use python and Visual Studio Code, but because of my current project, I need to learn C++ and every time I organize all my C++ files in a folder, I get an error that says that I cannot have more than 1 main function in the same project. I understand that this is because the Editor is running all programs in a C++ project together, How do I make it run just the one that I am currently working on.

2

Answers


  1. The typical way would be to have one solution with one project containing one program (.exe) having a main() method.

    You can also have one solution with many projects and each of them is a program. You can then switch the startup project (the one which is marked bold, change it throug the context menu via right click).

    However, you can also workaround that and have one solution with one project only:

    Create one file called main.cpp. Inside the main() function, make a selection of your program based on the command line arguments or user input. You can then run your application and specify which part of the program you want to execute. For your own learning projects, that’s fine. Don’t do that for production code that ships to customers.

    When you do that, you probably want to learn about header files and how the compilation process in C++ works (i.e. what is compiled and what is linked), e.g. by watching Kate Gregory’s C++ 20 Fundamendatly course on Pluralsight.

    Login or Signup to reply.
  2. Others have explained what main() is and how project/solution works with VS. There is another thing you can do which is to use developer command prompt for VS to compile your .cpp files instead of creating a project for every main()/program you make, open it then move to the folder where the .cpp file you want to compile is in using "cd" command then u can issue "cl" command and filename afterwards to compile it .

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search