0

I’m using C++ in Visual Studio, and I need to be able to view multiple C++ files. I also need to be able to run those previous files as well as my current file.

I have tried Visual Studio Community 2022 as well as Insiders 2026, but I cannot find a way to reliably do it. I tried the CMakeLists.txt method, but then I have to define each new script within the txt and I cannot do that every time I need to run a program. I tried using the open folder option, but it asks for a directory file, and I cannot figure out how to do that and it seems like it would run into the same issue as CMake. I tried adding multiple programs to the same solution, but it just won’t let me only select the one I truly want to run, and going into the properties and turning of inclusions for each program again takes too long.

The main issue seems to be that I need multiple programs to have the main() designation, and Visual Studio doesn’t know what to do at that point.

3
  • 6
    You can have multiple projects in one solution. Each project can be an executable project and contain a main(). See: What are solutions and projects in Visual Studio?. Commented Nov 3 at 15:30
  • You can add multiple cpp files to the solution in Visual Studio. Commented Nov 3 at 16:10
  • Tests adapters are fine alternative to normal way (with one project by file), but requires to use the associated UT framework; then you might run specific test which might be in different translation unit (each file doesn't have a main but one or several test case). Commented Nov 3 at 17:44

1 Answer 1

0

I figured it out. For anyone else with the same / similar issue, unless you need CMake, use the Console App option when creating a new solution, and then whenever you create a new project either right click on the solution and use Add->New Project, or use the usual New option in the top left and then select Add to Solution rather than Create New Solution. You can even right click on the solution, go to Configure Startup Projects, and you can select Current Selection rather than Single Startup Project, and it will dynamically change the startup project, ie the project you can run, as you move through cpp files. BTW, this is as of Visual Studio 2022 Community Edition 17.14.19, and Visual Studio 2026 Insiders Edition 11123.170. Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Yep, this is the normal mode of operation for Visual Studio. Only use cmake if you need interoperability with some other environment/tool.
Yeah, for anyone actually reading the documentation of the tool they are using, this should be obvious. Since this is well documented I don't see why it requires a specific Q/A here - the answer should follow from reading the available documentation (which it should be assumed that people actually do).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.