PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: CMake Compiling and Debugging  (Read 105 times)

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 9287
  • Windows 10
    • PlanetSquires Software
CMake Compiling and Debugging
« on: June 03, 2020, 10:40:48 PM »

We will be using a Make system to handle compiling our code. In simple terms, a Make system merely keeps track of the various source files in your application and only recompiles those that have been modified. This significantly speeds up compiling for larger projects as only the changed files need to be recompiled.

We are using CMake, website is located at:

Download the CMake package from get the “Binary distribution” and select the zip file download: Windows win64-x64 ZIP.

Extract that zip so that it is in a folder off of vscode\data\tools

Ensure that you have modified the code.cmd file and updated the CMake folder path in the PATH environment variable. For example:
set PATH=%~dp0data\tools\TDM-GCC-64\bin;%~dp0data\tools\cmake-3.17.2-win64-x64;%PATH%

Go to Settings (Ctrl+,) and select the Extensions branch. Click on the branch called CMake Tools Configuration.

Make these changes:
-   Cmake Path
   If your path is different because you downloaded a different version then update your path accordingly.

-   Mingw Search Dirs
   Click “add Item” and add “.\” (without the double quotes)

A lot of trial and error was involved in trying to find out how to actually compile and run code or how to compile and debug code. VSCode provides a general way through the use of two JSON files, launch.json (debugger instructions) and tasks.json (build instructions). I got these to work for compiling the current active file in the code editor, but once your project starts to grow to multiple files then that system quickly becomes cumbersome, hence the whole reason for CMAKE. That is a make system that controls the compiling and packaging of all necessary files into your final EXE. It only compiles that require re-compiling so subsequent compiles are pretty fast. We never use the launch.json and tasks.json approach.

With everything installed, here is how I start a new project:

Using windows file explorer, I navigate to where I want to store my new project. Every “project” in VSCode is related to an actual physical folder on the drive.
-   Create a new folder. For this example, I name it “MyProject”.
-   Using windows file explorer, I navigate to where I have the portable version of VSCode.
-   Double click the “start_vscode.cmd” file which essentially runs a powershell type of batch file (I listed the contents of that file earlier) that sets some environment variables and then starts up the actual VSCode editor.
-   In VSCode, select File / Open Folder, and navigate to the MyProject folder created in the first step.

At this point there is nothing special going on. Basically you have a blank open project with no files.
-   View / Command Palette (or shortcut, Ctrl+Shift+P) <---- you will use keyboard shortcuts a lot in VSCode.
-   Start typing: cmake into the search bar.
-   Find the entry called: CMake: Quick Start and click on it. This is when the magic starts.
-   CMake will start to configure itself and the first thing it will ask for is “Select a Kit for MyProject”. A ‘kit” is basically a compiler toolchain (remember the TDM-GCC-64 toolchain that we downloaded? We’ll use that one).
-   Select the toolchain. You will probably have many listed especially if you have used Visual Studio at some point in the past. We want to select “GCC for x86_64-w64-mingw32 9.2.0”.
-   If you don’t see the “GCC for x86_64-w64-mingw32 9.2.0” entry in the list, then click on the first option “[Scan for kits]”. This should find your toolchain located off of the app folder.

-   Enter a name for the new project. Just enter “MyProject”.
-   Select “Executable” rather than “Library”.
-   You will now see a lot of CMake configuring taking place. You may also get a popup asking if you wish to add Intellisense support for the folder (click yes of course).

-   You should now see a CMakeList.txt file open in the editor. This is the file that controls our build, run and debugging of our project. A sample main.cpp file will also be created in the folder and you will see it added to the CMakeList file in the add_executable line. Later, if we add more files to our project then we add them in that same line after the main.cpp entry. Basically, that means that it will take the compiled object files of those code files and link them to the main.cpp object to create the final exe.

-   In the Explorer treeview, click on the main.cpp file to show it in the editor. It is just a simple Hello World message that prints to the console.
-   Notice in the bottom statusbar the various options that CMake is giving us related to whether we want to create a debug or release build, toolchain, build, launch the program or debugger. Use those tools to launch our processes. Do not use the options found under the Run menu at the top of the editor.

-   Place your cursor on the first executable line int main(int, char**) In the left had margin click there to place a breakpoint. It should show as a small red circle.
-   Press Ctrl+F5 to build the file and start the debugger. The program should compile and the debug window open and execution stop at line 4. Under the Run menu you will see the debugger keyboard shortcuts: F10, F11, Shift+F11, F5. There should also now be a hovering panel at the top of the editor with several icons that do the same actions.
-   Play around with the debugger and then press F5 to continue executing and end the debugging process.
-   Go back to the Explorer (Ctrl+ShiftE) and expand the newly created folder called Build. In there you will find MyProject.exe


I modified the CMakeLists.txt file to include the follow entries (remove the # character to uncomment any lines that you want to activate). A CMakeLists.txt file basically contains all of the logic that controls how your program is compiled. It is a simple plain ascii text file.

This make file is setup for a simple project that I called win32hello.

Code: [Select]
cmake_minimum_required(VERSION 3.0.0)
project(win32hello VERSION 0.1.0)



target_link_libraries(win32hello -lcomctl32)

# Use -m32 to compile 32-bit exe, and use -m64 to compile 64 bit exe.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -municode")

# Also, if using -m32 then must use the following resource compiler flags
# otherwise the compiling of the resource.rc will fail.
set(CMAKE_RC_FLAGS "${CMAKE_RC_FLAGS} --output-format=coff --target=pe-i386")

# -s                   strip out all debug symbols
# -municode            allows call to the unicode wWinMain() entry point
# --subsystem,windows  subsystem turns on/off the console window
# -static-libstdc++
# -static-libgcc
# -std=gnu++17
# -fno-rtti
# -fno-exceptions

# The TDM-GCC-64 toolchain automatically statically links the C++
# standard library and libgcc in order to ensure that running the
# program will work on systems without those libraries.


« Last Edit: June 03, 2020, 11:19:38 PM by Paul Squires »
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 9287
  • Windows 10
    • PlanetSquires Software
Re: CMake Compiling and Debugging
« Reply #1 on: June 05, 2020, 07:59:40 PM »

I noticed that when I switch to compile/debug a 32 bit program then the debugger in Visual Studio Code will not work correctly. It fails in the debugger with "PAUSED AT EXCEPTION" in thread 1.

So, for now, I will debug only using 64 bit mode. My flags in CMakeLists.txt look like this:

# Use -m32 to compile 32-bit exe, and use -m64 to compile 64 bit exe.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -municode -m64 ")

Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer