I can not stress enough how important having a good environment setup is for me. Traditionally, I’m a Go developer so getting a similar setup working in C++ will help ease me in. I also use VSCode, so having something working there where I can have my tests and the ability to debug is key. Luckily these days VSCode has pretty good C++ and CMake support.
The environment
I’ve used CMake in the past for various projects but never took the time to really learn how to structure / use it properly. I came across a great article on modern cmake that really cleared up a lot of things and gave me a good starting point for structuring this project. I highly recommend reading that page if you are diving in to c++ with CMake.
So the way it’s structured is:
- apps/: where you put your code to compile binaries
- src/: where you put your library code
- tests/: yehp, tests
I deviated a bit from their example project by not having a separate include
directory. I did this because I’m not going to be building a library to distribute and I’d rather have header files alongside source files as that’s traditionally how I’ve done C++.
Getting VSCode Setup
I actually run linux via windows WSL2, so I want to make sure tests work as well as debugging. I installed the following VSCode extensions:
- C/C++ Obviously
- CMake tools For CMake integration
- C++ TestMate For testing (supports Catch2 and GoogleTest)
The modern cmake article suggested Catch2 for testing, so I’m giving that a shot, seems to work nicely and I have a lovely testing UI now!
![](https://wirepair.org/wp-content/uploads/2023/07/tests.png?w=257)
I can also run tests by going into the build directory and running ctest
.
$ ctest
Test project /home/wirepair/netcode/pmo/build
Start 1: testlibtest
1/1 Test #1: testlibtest ...................... Passed 0.00 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 0.00 sec
Getting debugging working was a bit more work. Here’s the following configs:
launch.json
{
"configurations": [
{
"name": "C/C++: cpp build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: cpp build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
],
"version": "2.0.0"
}
settings.json
{
"cmake.configureOnOpen": true
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cpp build active file",
"command": "/usr/bin/cpp",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
So now to debug I just have to do ctrl + p > CMake: Debug
and it launches the integrated gdb debugger for me.
So yeah, that’s the environment, feel free to follow along I’m going to do all this open source style.