Compile multiple .cpp files in Visual Studio Code with clang++ on macOS

To compile multiple cpp files with clang++ in VS Code on macOS, you'll need to configure your tasks.json file and make sure that the args has the correct file path for your project. Here is what worked for me:

  1. I created a new project in Visual Studio and placed the .cpp and .h file in the immediate folder.
  2. Then, I updated my tasks.json, using VS Code variables to point to the correct location within the project. Example:

Note, using either ${fileDirname} or ${workspaceFolder} as variables worked based on my project's structure.

{       
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                // "${fileDirname}/*.cpp",
                "${workspaceFolder}/*.cpp", 
                "-o",
                //"${fileDirname}/${fileBasenameNoExtension}"               
                "${workspaceFolder}/${fileBasenameNoExtension}"             
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
  }
  1. From VS Code, I selected Terminal > Run Build Task... which output a file
  2. I opened a Terminal window within VS Code and ran ./[filename] (where filename is the name of the outputted file) and the code executed.

Don't complicate tasks.json. It is better to use something such as CMake that takes care of all of this configuration (C++ version, compilation flags and warnings, etc) and just call make (or ninja or any other generator that you choose with CMake).

Then in your tasks.json the value of "cwd" in "options" would point to the build folder (the folder where you run the CMake command) instead of the source folder, "args" would be the name of a target in CMake (or empty if you want to build all targets), and "command" would be something such as "/usr/bin/ninja" or "/usr/bin/make", depending on your chosen generator in CMake.

This has also the advantage that other people would benefit from this (or would create this for you), regardless of their editor.

A possible CMakeLists.txt file for the flags you are using would be something similar to

project(TheProjectName)
cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED YES)

add_compile_options(-Wall)
add_compile_options(-Wextra)
add_compile_options(-pedantic-errors)
add_compile_options(-Weffc++)
add_compile_options(-Wconversion)
add_compile_options(-stdlib=libc++)

# This is the executable target "main" is the name of the executable
# and you can change to something else
add_executable(main main.cpp
  other_file.h
  other_file.cpp
  some_other_file.h
  some_other_file.cpp
  )

The -g option to specify a debug build is not set in CMakeLists.txt. Instead, when you run cmake you set its build type to Debug.

To use this CMakeLists.txt file, create a build folder inside your workspace folder and go to it in a terminal. Then run the command cmake .. -DCMAKE_BUILD_TYPE=Debug once. After that you can compile the project with just make from the build folder. Setting this in tasks.json is just seting "cwd" to point to the ${workspaceFolder}/build and the command to be /usr/bin/make.


Just a final note, if you have both clang and gcc installed then when you run cmake it will probably use gcc instead of clang. To specifically as for clang run the cmake command specifying the CXX and CC environment variables to point to clang. That is, the cmake command should be run (once from the build folder) as

CXX=clang++ CC=clang cmake .. -DCMAKE_BUILD_TYPE=Debug