Catégories

How to compile C++ code with VS Code and Clang

Bonjour. Today, under Windows, we will use VS Code to compile and link C++ code with Clang.

  • Windows 10 is running
  • Install VS Code. In April 2018, I use 1.21.1.
  • Launch VS Code and then install the C/C++ for Visual Studio Code extension. Use the extension icon on the left or press CTRL+SHIFT+X

C/C++ extension for VS Code

  • Install LLVM. In April 2018 you should install LLVM 6.0 or higher. During installation make sure to check the box saying « Make LLVM Tools available in current user ath » or similar. By default LLVM tools are NOT in your path.
  • Install Visual Studio Build Tools. Scroll down the page, the link to the Build Tools is very close to the bottom of the page. Again, this is not a Visual Studio Express 2017 installer. Instead this is a package which provides all the tools and libs you need to compile and link code under Windows. Among other things it provides the libs that are not coming with the LLVM installer. It does not provide Visual Studio IDE.

From the tooling stand point we are ready. Let’s move on :

  • With file Explorer create a directory where the files of the « project » will take place. Don’t worry, nothing will be created outside this directory. If at one point you get lost, delete everything except the source code (main.cpp, see below) and start again the procedure.
  • Once the directory is created in file Explorer, open it, right click then select « Open with Code » option.

Open with VS Code

  • Once in VS Code create a new file and copy the code below
#include <iostream>
#include <string_view>

int main(){
    std::string_view s = "Greetings professor FALKEN, how about a nice game of chess?\n";
    std::cout << s;
    getchar();
    return 0;
}

VS Code should look like this :
main.cpp in VS Code

  • Save the file as main.cpp in the current workspace (the directory from where you launch VSCode)

Configuration of the C++ context

  • Now, press F1 and then type « cpp » in the bar
  • Select the « C/Cpp : edit configuration » option

C/Cpp Edit Configuration in VS Code

  • An auto-generated c_cpp_properties.json file should appear. Save it pressing CTRL+S
  • At this point a « .vscode » subdirectory should be created. We can see this directory in the file tree on the left of VS Code as well as in file Explorer. Here is how it looks in VS Code

c_cpp-properties.json in VS Code

  • Here is what happen in file Explorer.

.vscode directory created by VS Code

Compilation with VS Code

  • Press ALT+T then select the »Configure Default Build Task » option
  • Select the « Create tasks.json file from a template » option
  • Select « Other » since we don’t want to use MSBuild, maven etc. to build our application

Configure default build task in VS Code

  • Modify the generated file as follow
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "clang-cl Debug",
      "type": "shell",
      "command": "clang-cl",
      "args": [
        "main.cpp",
        "/Z7 /MDd /W4 /EHsc /std:c++17 /Od",
        "/o Debug/Test.exe",
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    },
    {
      "label": "clang-cl Release",
      "type": "shell",
      "command": "clang-cl",
      "args": [
        "main.cpp",
        "/MD /W4 /EHsc /std:c++17 /O2",
        "/o Release/Test.exe",
      ],
    }
  ]
}

Note

With VS Code 1.22.1 and higher you should set one arg per line. Pay attention on how the spaces are escaped on the « /o » and « /link » line. At the end, tasks.json should be written as follow :

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "clang-cl Debug",
      "type": "shell",
      "command": "clang-cl",
      "args": [
        "main.cpp",
        "/Z7",
        "/MDd",
        "/W4",
        "/EHsc",
        "/std:c++17",
        "/Od",
        "/o' 'Debug/Test.exe",
        "/link' 'User32.lib" 
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    },
    {
      "label": "clang-cl Release",
      "type": "shell",
      "command": "clang-cl",
      "args": [
        "main.cpp",
        "/MD",
        "/W4",
        "/EHsc",
        "/std:c++17",
        "/O2",
        "/o' 'Release/Test.exe",
        "/link' 'User32.lib" 
      ],
    }
  ]
}
  • Save the tasks.json file with CTRL+S. The file will be saved in the .vscode subdirectory

tasks.json created by VS Code

  • Create a « Debug » and a « Release » subdirectories in the initial directory. This can be done with VS Code or with file Explorer. Just for the fun create both directories with VS Code.

Debug and Release directories for VS Code

  • Press CTRL+SHIFT+B to compile and link the debug version of the code
  • 3 new files should appear in the Debug subdirectory (Test.exe, .ilk and .pdb)

Comilation with VS Code generates code in Debug directory

Debugging with VS Code

  • In order to debug the code, press F5
  • Select « C++ (Windows) » among the different options

Debug with VS Code

  • VS Code will generate a default launch.json file. Modify the file as follow
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(Windows) Launch",
      "type": "cppvsdbg",
      "request": "launch",
      "program": "${workspaceFolder}/Debug/Test.exe",  
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": true
    }
  ]
}
  • Save the file with CTRL+S. A launch.json file is created in the .vscode subdirectory
  • In main.cpp source code set a breakpoint on line 5 for example (click in the left margin)

Set a breakpoint within VS Code

  • Press F5
  • The code should start and stop on the breakpoint
  • Strike F10 to go one step further. The string_view variable s should be initialized.

Debug session in VS Code

  • At this point you can either press F5 to continue or to press SHIFT+F5 to stop the debug session.

Compile a release version with VS Code

  • Press Alt+t
  • Select Execute the task
  • Select « clang-cl Release » in the list
  • Press Enter on the next option
  • Test.exe should be generated in the Release subdirectory

Compile a release version with VS Code and Clang

Linking with Windows libraries

  • Modify main.cpp as follow
#include <iostream>
#include <string_view>
#include <windows.h>

int main(){
    std::string_view s = "Greetings professor FALKEN, how about a nice game of chess?\n";
    std::cout << s;
    MessageBoxW(NULL, L"My message", L"My title", MB_OK);
    getchar();
    return 0;
}
  • If you press CTRL+SHIFT+B you can’t link the code because some library are missing

Linking error with Clang under Windows

  • Edit the tasks.json file and modify it as follow
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "clang-cl Debug",
      "type": "shell",
      "command": "clang-cl",
      "args": [
        "main.cpp",
        "/Z7 /MDd /W4 /EHsc /std:c++17 /Od",
        "/o Debug/Test.exe",
        "/link User32.lib" 
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    },
    {
      "label": "clang-cl Release",
      "type": "shell",
      "command": "clang-cl",
      "args": [
        "main.cpp",
        "/MD /W4 /EHsc /std:c++17 /O2",
        "/o Release/Test.exe",
        "/link User32.lib"
      ],
    }
  ]
}
  • Save the file with CTRL+S
  • Press F5 to run a debug session
  • A message box should appear on the screen when you execute line 8.

Successful linking with VS Code and Clang

  • In order to create a Rlease version of the code with the message box you follow exactly the same steps as before : ALT+T, select « Execute the task… », select « clang-cl Release », press ENTER

You are all set. Enjoy 🙂 !

3 comments to How to compile C++ code with VS Code and Clang

  • Paul

    Thanks for the tutorial. The only problem I had was spaces in tasks.json. I just could get them to work so I ended up using « /oDebug/Test.exe » and « /linkUser32.lib »

  • Norton

    I had exacly the same problem as Paul. Thanks for the solution.

  • Vadim

    Thanks for the tutorial! I have one more problem. When there is a build error and I click on the line that contains the error (in terminal) it doesn’t jump to the corresponding line in the code. Ctrl+Click doesnt’t work too. Is there a way to make VSC to jump to the source code?

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.