Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent console window from closing in Visual Studio 2017 cmake project

Visual Studio 2017 has built-in support for cmake projects, meaning you can just open a folder containing a CMakeLists.txt and use it. However, there doesn't seem to be a way to prevent the console window from closing after running an executable.

With a normal Visual Studio project, you can use Ctrl+F5 to run without the debugger attached. However, Ctrl+F5 did exactly the same thing as F5, that is, it ran the executable and closed the console window immediately.

Another suggestion was to set the subsystem to "console" for the application, but the cmake project has no Visual Studio project that I can set settings for.

I figured maybe I could go to the Debug and Launch Settings for my CMakeLists.txt (right click > Debug and Launch Settings > target.exe), which opened launch.vs.json. Unfortunately, I was unable to find documentation on this. By looking through the schema, though, it seemed as if I could set "noDebug": true, but this just turned off the debugger and did nothing to stop the console from closing:

{
  "version": "0.2.1",
  "defaults": {},
  "configurations": [
    {
      "type": "default",
      "project": "CMakeLists.txt",
      "projectTarget": "target.exe",
      "name": "target.exe",
      "noDebug": true
    }
  ]
}

This is driving my crazy. I can't just add a system("pause") to the main function, as I'm using a main function provided by a test framework. Furthermore, that should be completely unnecessary; Visual Studio should handle it for me.

How can I make the Visual Studio console not close after my executable finished, when my executable is from a cmake project?

I'm using Microsoft Visual Studio Community 2017, Version 15.2 (26430.16) Release

like image 975
Justin Avatar asked Jul 26 '17 04:07

Justin


People also ask

How do I keep the console window open in Visual Studio?

To keep the console window open in Visual Studio without using the Console. ReadLine() method, you should run the application without debug mode by pressing Ctrl+F5 or by clicking on the menu Debug > Start without Debugging option.

How do I stop the console window from closing in C++?

Before the end of your code, insert this line: system("pause"); This will keep the console until you hit a key.

Why is the console window closing immediately once displayed my output?

Because it's finished. When console applications have completed executing and return from their main method, the associated console window automatically closes. This is expected behavior.


1 Answers

It's a bug in Visual Studio 2017 CMake support. It is resolved in VS 2019.

As a temporary workaround, add a breakpoint on application exit and run with debugging on (F5):

  1. Press Ctrl+B (New Breakpoint)
  2. Enter function name: exit
  3. Press OK

Now if you run your project (F5) the debugger will stop after main() returns.

To remove the breakpoint, go to the Breakpoints View (Ctrl+Alt+B) and delete it from there.

like image 124
rustyx Avatar answered Oct 13 '22 18:10

rustyx