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

I'm trying to build a simple project with multiple *.cpp files with clang++ on macOS.

The tasks.json:

    {
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-std=c++17",
                "-Wall",
                "-Wextra",
                "-Weffc++",
                "-Wconversion",
                "-pedantic-errors",
                "-stdlib=libc++",
                "-g",
                "${workspaceFolder}/*.cpp",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

The resulting command looks like this:

/usr/bin/clang++ -std=c++17 -Wall -Wextra -Weffc++ -Wconversion -pedantic-errors -stdlib=libc++ -g '/Users/USER/Developer/WORKINGDIR/Lesson 2/Lesson 2.8/*.cpp' -o '/Users/USER/Developer/WORKINGDIR/Lesson 2/Lesson 2.8/main' 

But the compiler throws an error:

clang: error: no such file or directory: '/Users/USER/Developer/WORKINGDIR/Lesson 2/Lesson 2.8/*.cpp'

But if i change the string which points to files from this:

'/Users/USER/Developer/WORKINGDIR/Lesson 2/Lesson 2.8/*.cpp'

to this (without quotes and added '\'):

/Users/USER/Developer/WORKINGDIR/Lesson\ 2/Lesson\ 2.8/*.cpp

everything compiles fine.

But how can I configure the same string in the tasks.json? Or what should I change there for it to work properly?



Solution 1:[1]

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.

Solution 2:[2]

Bro!

The most important advice is that you are advised not to use spaces in the directory or file names.

Is your multiple sources files architecture like this:

fitBodyBootCamp

|

|_.vscode

|     |__c_cpp_properties.json

|     |__launch.json

|     |__settings.json

|     |__tasks.json

|__test.cpp

|__test.h

|__testImp.cpp

If so, This is a project with multiple sources files under the same folder.

Your "tasks.json" should be:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "Compile With clang++",
      //"command": "clang++",/usr/bin/clang++
      "command": "/usr/bin/clang++",
      "args": [
        "-std=c++11",
        "-stdlib=libc++",
        // My project fitBodyBootCamp were under 
        // /Users/marryme/VSCode/CppProject/fitBodyBootCamp
        // So ${workspcaeFolder} also were
        // /Users/marryme/VSCode/CppProject/fitBodyBootCamp
        // all the *.cpp files were under
        // /Users/marryme/VSCode/CppProject/fitBodyBootCamp
        "${workspaceFolder}/*.cpp", // Have changed
        "-o",
        // Thanks those chiense website bloggers!
        // 1.mac vscode compile c++ multi-directory code demo
        // https://blog.csdn.net/fangfengzhen115/article/details/121496770?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-4.pc_relevant_default&spm=1001.2101.3001.4242.3&utm_relevant_index=7
        // 2.Compile and debug c++ multi-folder project under VSCode (non-makefile)
        // https://blog.csdn.net/BaiRuichang/article/details/106463035
        // I also put the main.o under my current workspace directory
        // This after "-o"  is the target file test.o or test.out or test
        "${workspaceFolder}/${fileBasenameNoExtension}", // Have changed
        "-I",
        // This after "-I" if the include files directory
        "${workspaceFolder}", // Have changed
        "-Wall",
        "-g"
      ],
      "options": {
        // "cwd" is the source files directory
        "cwd": "${workspaceFolder}" // Have changed
      },
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

If you use gcc/g++, just change the clang/clang++ to those.

If this line code needs to change or function, I add the comment

"// Have changed". Just notices that line code! Please!

"tasks.json": You know, that's "tasks.json"!!! NOT "task.json".

More about "launch.json" or "c_cpp_properties.json" or

"settings.json",

please make reference to Multiple CXX Same Folder Manually VSCode(mac) from GitHub Gist,

Or, make reference to Multiple CXX Same Folder Manually VSCode(mac) from Github.

Or your multiple sources files architecture like this:

fitBody

|

|_.vscode

|     |__c_cpp_properties.json

|     |__launch.json

|     |__settings.json

|     |__tasks.json

|__build

|__inc

|     |__fitbody.h

|__src

     |__fitbody.cpp
 
     |__main.cpp

If so, That is separated multiple sources files.

Your "tasks.json" is just like this:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "Compile With clang++",
      // Just use clang compiler
      //"command": "clang++",/usr/bin/clang++
      "command": "/usr/bin/clang++", // Have changed
      "args": [
        "-std=c++11",
        "-stdlib=libc++",
        // My project fitBodyBootCamp were under 
        // /Users/marryme/VSCode/CppProject/fitBody
        // So ${workspcaeFolder} also were
        // /Users/marryme/VSCode/CppProject/fitBody
        // all the *.cpp files were under
        // /Users/marryme/VSCode/CppProject/fitBody/src
        // this is the source files diretory
        "${workspaceFolder}/src/*.cpp", // Have changed
        "-o",
        // Thanks those chiense website bloggers!
        // 1.mac vscode compile c++ multi-directory code demo
        // https://blog.csdn.net/fangfengzhen115/article/details/121496770?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-4.pc_relevant_default&spm=1001.2101.3001.4242.3&utm_relevant_index=7
        // 2.Compile and debug c++ multi-folder project under VSCode (non-makefile)
        // https://blog.csdn.net/BaiRuichang/article/details/106463035
        // I also put the main.o under my build folder directory ../build
        // This after "-o"  is the target file main.o or main.out or main
        "${workspaceFolder}/build/${fileBasenameNoExtension}", // Have changed
        "-I",
        // This is include directory.
        "${workspaceFolder}/inc", // Have changed
        "-Wall",
        "-g"
      ],
      "options": {
        // "cwd" is the source files directory
        "cwd": "${workspaceFolder}/src" // Have changed
      },
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

More about "launch.json" or "c_cpp_properties.json" or

"settings.json",

please make reference to Separated Multiple CXX Manually VScode(mac) from GitHub Gist.

Or, make reference to Separated Multiple CXX Manually VScode(mac) from GitHub.

END!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 blastoise
Solution 2 Vittore Marcas