Prevent console window from closing in Visual Studio 2017 cmake project - c++

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

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):
Press Ctrl+B (New Breakpoint)
Enter function name: exit
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.

Came across this situation lately, because we sometimes use stdout for debugging info in our UI applications, we needed to turn on the console window on our dev-machines, but turn it of in our CI build.
In your CMakeLists.txt must be a 'add_executable' statement, like this:
add_executable(project_name WIN32 ${your_source_files})
If you ommit the WIN32, CMake will change the subsystem to "Console", which leads Visual Studio to keep the console window open when your application exits.

Related

How to run C/C++ code using integrated terminal in Visual Studio

How to launch a C/C++ console application using the integrated terminal in Visual Studio instead of launching a separate terminal window?
To clarify more:
When I press the run button right now, this is what happens:
But what I want is something like this (but it must happen when I press the run button), where the output is directed to a terminal inside the visual studio window, without launching a separate window, this will make it easier when debugging because I won't have to switch between multiple windows:
There is a handy VsConsoleOutput extension for Visual Studio 2019 / 2022 that redirects program output into Output window inside of Visual Studio.
However when installing it i've got an exception complaining about incorrect value of InstalledByMsi value somewhere in manifest. The workaround is to manually open downloaded .vsix package (which seems to be a .zip archive) using WinRAR or something, adjust one line in extension.vsixmanifest file and save updated archive.
<Installation AllUsers="true" InstalledByMsi="false">

Visual Studio Code: Attach to Unity process for debugging C++ based dll

I have a Unity project that uses some C++ code via a DLL compiled in a separate project.
Can I attach the visual studio code debugger to my Unity project such that I can debug the DLL's source code using break points?
Here are some things I tried so far:
in Unity: Press "Pause", then press "Start" to immediately pause the game after starting it (in order to get time to attach vs code)
compile DLL using debug symbols
in VS Code: create a launch.json like this
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to process",
"type":"clr",
"request": "attach",
"processId": "${command:pickProcess}"
}]
}
--> this should allow me to pick the process I want to connect to interactively
VS Code: click on "Attach to process" -> search for my project name -> returns a process based on my/path/to/Unity.exe
--> attaching seems to work, but when I "unpause" my Unity game it never hits a break point.
Is my launch.json wrong?
Some additional info:
I'm using bazel to compile my c++ library project via command line (not sure if relevant?)
Usually when debugging C++ in VS code my launch.json has an entry sourceMap which directs the debugger to the root of my source files. Not sure if anything similar would be needed here as well?
Moving forward
Meanwhile I've refactored my launch.json a bit. Thanks to a comment I assume "type" : "clr" stand for Common Language Runtime which seems to be for debugging scripting languages but not C/C++. So I changed it to "type":"cppdbg". After installing gdb via Msys2, I'm referencing the path to that gdb in the launch.json. This is an updated version of my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to process",
"type":"cppdbg",
"request": "attach",
"processId": "${command:pickProcess}",
"program": "${workspaceRoot}/Packages/com.github.homuler.mediapipe/Runtime/Plugins/mediapipe_c.dll",
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
}]
}
Spoiler: It's still not working, but inside VS Code debug console / terminal I see some output when I start the game in Unity editor. So there seems to be some traffic between VS Code and Unity at least.
One new problem:
with second version of launch.json, C++ breakpoints are grey with the info message "Attempting to bind the breakpoint...."
While I couldn't figure out a solution for VS Code, I found a solution for
Microsoft Visual Studio Community 2022 RC (64-bit), installed with
Desktop development with C++
Universal Windows Platform development
note: this is not the standalone VS shipped with Unity (which is VS 2019)
the solution goes roughly like this
compile C++ DLL library with debug flags
since in my case the compilation was done using bazel, I cannot even say which compiler was used, but it doesn't seem to matter
Open VS -> File open -> Select folder that contains C++ source code (doesn't have to be the same folder as the Unity project)
Set breakpoint in any .cc file (that is accessed from Unity)
Launch Unity
In VS (top menu bar) click on Debug -> Attach to Process
in process list search for Unity.exe (there should only be one entry)
above that list is an option "Attach to:" -> Select -> "Native"
Launch Game inside Unity Editor
--> the game should now break when hitting the breakpoint
The C++ DLL library Project in VS be compiled debug first, also need add the /Zi parameter.
Follow steps:
Open the project's Property Pages dialog box.
Click the C/C++ folder.
Click the General property page.
Modify the Debug Information Format property.

How to attach Visual Studio to a C++ node module?

I wrote a node.js module in C++ with Visual Studio. I created the project with node-gyp configure --debug. This seems to work fine and I can compile my code and a loadable binary is created in the Debug folder. I am then able to require('./build/Debug/mymodule) this file from a server.js file and when I start the server from the command line (node server.js), the module runs. But I am struggling to figure out how to attach the Visual Studio Debugger.
According to this question it sounds like gdb is automatically attached (?), but how do I invoke my server.js with the Visual Studio debugger?
Edit: I run Visual Studio 2015 and node v4.1.2
Seems like I figured it out.
Goto: Project->Properties
Open "Debugging" options under "Configuration Properties"
Set path for your node.js installation and the path to your "server.js" (or whatever you called your file)
Start node with the green debug button directly from Visual Studio

Visual Studio 2015: Build successful but debugging never stops loading

I have this C++ project I am trying to debug in Visual Studio (Community) 2015. The build is always successful, but after that debugging never starts. All that shows up is the Windows loading icon, which just never goes away. Any ideas about what might be wrong?
The problem was that Avast was interfering with my Visual Studio Projects. I used the instructions at https://forum.avast.com/index.php?topic=139935.0 to get it to work.
Basically you need to exclude your VS projects from Avast's scan.
... but simply turning off DeepScreen in Avast 2014 is not enough. I
make multiple apps in Visual STudio 2012, and I can tell you that even
with Deep Screen off, Avast will still pop right up and destroy the
compiling process by saying the .exe is a virus (the crazy "evo-gen
[susp]"). The only way I can get Avast to stop doing that is to
manually enter-in exclusion directories for every directory where I
create apps! ...or make an exclusion for some high-level folder.
Try resetting your settings by going here
Tools >> Import and Export Settings Wizard >> Reset all settings
I had similar problems for Visual Studio 2015 Update 1 on Windows 10.
For me, the only solution, which helped is to install this package:
https://www.microsoft.com/en-us/download/confirmation.aspx?id=49029
"FIX: Devenv.com process freezes after you execute devenv.com /build command in Visual Studio 2015 " https://support.microsoft.com/en-us/kb/3092422

How do I make my out-of-date code automatically built before being run?

I'm using Visual Studio 2010.
When I change the code in my source file, then click the "Run" button on the debug tool-bar, Visual Studio does not automatically build the code, instead it runs the most recent manually built binaries.
I want to run my code just by simply clicking the "Run" button in the tool-bar; I don't want to press the "Build" button every time I want to run it, it becomes so annoying after a while. Currently, Visual Studio requires me to press the "Build" button before pressing the "Run" button. If I don't, it doesn't update my newest code changes in the exe file.
I opened the "Options" window above from Menu Bar > Debug > Options and Settings....
And, as you see in my settings, I told Visual Studio to always build my projects if they are out of date; but it doesn't listen to me.
How do I solve this problem?
Select your project and set it as startup project: "Project" -->
"Set as StartUp Project"
Check if your project is set at the configuration manager: "Build"
-->"Configuration Manager"