VS Code will not build c++ programs with multiple .ccp source files
in tasks.json:
"label": "g++.exe build active file",
"args": [
"-g",
"${fileDirname}\\**.cpp",
//"${fileDirname}\\**.h",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
],
in launch.json:
"preLaunchTask": "g++.exe build active file"
it will work if your sources are in separated folder
Compile multiple .cpp files in Visual Studio Code with clang++ on macOS
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
VS Code build c++ programs with multiple .ccp source files
First make the a.out
file first and change the /.vscode/lauch.json
file. In that file change the "program": "whatever"
to "program": "${workspaceFolder}/a.out"
and if there is a "preLaunchTask": "C/C++: g++ build active file",
then cut that line then press F5 and the debugger should work fine.
Check out here for more clearification.
Is there a way to build a project with multiple C++ files efficiently in Visual Studio Code?
Solution: Use CMake. It's less complicated and more reliable than Visual Studio build tasks. Comments on my original question clear up how that's done.
Related Topics
What Does the Operation C=A+++B Mean
Casting Between Void * and a Pointer to Member Function
How to 'Cout' the Correct Number of Decimal Places of a Double Value
Clarification Needed Regarding Getchar() and Newline
Does the 'Offsetof' MACro from <Stddef.H> Invoke Undefined Behaviour
Cpp - Valgrind - Invalid Read of Size 8
What Happens to Uninitialized Variables? C++
How to Know Which Parts in the Code Are Never Used
Use C++ with Cocoa Instead of Objective-C
Why Did Microsoft Abandon Long Double Data Type
Are Child Processes Created with Fork() Automatically Killed When the Parent Is Killed