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?
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:
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
}
}
]
}
./[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
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.