Skip to content

Instantly share code, notes, and snippets.

@Mohido
Last active December 27, 2022 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mohido/cee4f626d09e43eee00e7e4c756a1424 to your computer and use it in GitHub Desktop.
Save Mohido/cee4f626d09e43eee00e7e4c756a1424 to your computer and use it in GitHub Desktop.
Setting Up Visual Code for C/C++ Development With CLANG

Pre-Requisits:

Setting Up VSCode:

  1. After Downloading VScode, install the foollowing VSCode extensions:
  1. Craete a folder called .vscode in your working directory.
  2. The C/C++ IntelliSense Debugger settings can be set based on the c_cpp_propoerties.json and launch.json files which the developer must create in the ".vscode" folder. The files content are explained below.

c_cpp_propoerties.json:

This file contains the compiler/autocomplete specific information for C/C++ files and it is used by the IntelliSense (the C/C++ extension). The structure of the file and its content can be found at VScode documentation. However, a simple example is given below for the linux environment.

"configurations": [
        {
            "name": "Linux",                   // Operating System
            "includePath": [                   // Paths used for AutoCompletion (where the code is actually written)
                "${workspaceFolder}/src/**"
            ],
            "defines": [                       // Define macros (this is defined even before the macros in the code)
                "MOHIDO_MOHIDO"
            ],
            "compilerPath": "/usr/bin/clang",  // Where to find the compiler
            "cStandard": "c17",                // C 17 support
            "cppStandard": "c++17",            // C++ 17 support
            "intelliSenseMode": "clang-x64"    // What is the compiler type
        }

launch.json

This file contains the necessary information for the debugger. For instance, where to find the program to debug and the type of the debugger. To read more about the file, please visit the launch.json page. However, again, an example is provided below:

"configurations": [
        {
            "name": "(Windows) Launch",                         // The name of the debugger (how you want it to be viewed in VSCode
            "type": "cppvsdbg",                                 // Debugger type (cppvsdbg, cppdbg ... etc)
            "request": "launch",                                // must be "launch" or "attach". 
            "program": "${workspaceFolder}/apptodebug.exe",     // Where to find the desired application
            "args": [],                                         // Arguments to pass to the application
            "stopAtEntry": false,                               // Breaks when the program starts
            "cwd": "${workspaceFolder}/bin/",                   // Where to find the debug files (output by the compiler)
            "environment": [],                                  // Any specific environment variables for the run of the application
            "console":"newExternalWindow",                      // Only for windows (create a new external console window)
        }

Building and Running the Application

There are two ways to compile the project and build the application files (.exe, .so, .dll ...etc):

  1. To us the terminal and compile every time using the clang command and link the necessary libraries, code files and flags. Then run the application from VScode by either pressing "F5" or through the menu (Run -> Start Debugging).
  2. We can automate the process by creating a build.sh or build.bat files and a VSCode task to run them from the VSCode editor instead of typing the steps manually mentioned previously in step (1).

Automating the Build for VSCode

VScode editor can run bash scripts by the press of a button. The process can be done through the menu (Terminal -> Run Task...). However, currently, there is no vscode task for running our custom .sh or .bat files. Thus, the need to define that task emerges.

To define a VScode task, create a file in the .vscode folder called tasks.json. An example of the content for shell scripts are given below:

{
  "tasks": {
            "type": "shell",    // Task type (shell)
            "label": "The Cool Task Name",  // Task name (what do you want it to be called)
            "windows": {                    // Where to find the shell file on windows
                "command": "${workspaceFolder}/buildscripts/build.bat"
            },
            "linux": {                      // Where to find the shell file on Linux
                "command": "${workspaceFolder}/buildscripts/build.sh"
            },
            "options": { 
                "cwd": "${workspaceFolder}/buildscripts"    // Where are the scripts
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",                             // So the developer can run it with "ctrl+shift+b"
            "detail": "Task generated by Debugger."       // Additional description for other developers to understand the task
        }
    }
  }

Now, the setting up is complete and the developers can build then run their debugger through vscode on multiple platforms.

  1. Click on the menu: Terminal -> Run Task... then choose the task you want to perform. This is only done if you have tasks.json file in the .vscode folder.
  2. Click on the menu: Run -> Start Debugging or press F5 to start the application on debugging mode.

Notes

  1. In the configuration, the developer can add predifined macros. However, bear in mind, when using the _DEBUG macro, some C++ libraries, such as iostream, will not work. The simplest solution is to remove that macro since it will not impact the debugger nor intelliSense in any means. An example of shown linking errors:
libcpmtd.lib(xlocale.obj) : error LNK2001: unresolved external ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment