Create task that builds cpp and c files in Visual Studio Code - c++

I have task in my task.json file in Visual Studio Code that builds project :
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: build",
"command": "/usr/bin/g++",
"args": [
"-g",
"${workspaceFolder}/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
},
Task is called from launch.json. Does line "${workspaceFolder}/*.cpp" means that task builds only cpp files? How to deal if project has both - cpp and c files?

G++ can compile any .c or .cpp files but they will be treated as C++ files only.
GCC can compile any .c or .cpp files but they will be treated as C and C++ respectively.
compilerArgs (optional) Compiler arguments to modify the includes or defines used, for example -nostdinc++, -m32, etc.
You could try using GCC.
Else if you have a main.cpp the uses
#include "example.h"
#include "example2.h"
compiling only the main file should work.
Perhaps this might bring some more clarification : https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference

"tasks": [
{
"type": "cppbuild",
"label": "C/C++: build",
"command": "/usr/bin/gcc",
"args": [
"-g",
"${workspaceFolder}/*.cpp",
"${workspaceFolder}/*.c", // this should build c and cpp at the same time.
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/gcc"
},

Related

In vscode tasks.json how to use the g++ to compile-only multiple files and apply the output to make an executable?

What I do want to achieve is separate the compilation and linking steps for multiple .cpp and .h files. Like this: g++ -c *.cpp && g++ -o main *.o. But I would like to do that inside the vscode tasks.json. I tried the following code:
Tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-c",
"${workspaceFolder}/*.cpp",
"&& /usr/bin/g++ -o",
"${fileDirname}/${fileBasenameNoExtension} *.o"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
The error I did receive below:
Console
Starting build...
/usr/bin/g++ -fdiagnostics-color=always -c /home/raijin/Documents/Code/Cpp/test/*.cpp "&& /usr/bin/g++ -o" "/home/raijin/Documents/Code/Cpp/test/main *.o"
g++: warning: && /usr/bin/g++ -o: linker input file unused because linking not done
g++: error: && /usr/bin/g++ -o: linker input file not found: No such file or directory
g++: warning: /home/raijin/Documents/Code/Cpp/test/main *.o: linker input file unused because linking not done
g++: error: /home/raijin/Documents/Code/Cpp/test/main *.o: linker input file not found: No such file or directory
What should I do?
Following the comments suggestion and vscode compound tasks doc. Found a way to compile and produce a executable with the code below:
{
"tasks": [
{
"type": "cppbuild",
"label": "c++",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-c",
"${workspaceFolder}/*.cpp",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": false
},
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "custom cpp build",
"command": "g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"*.o"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [
"$gcc"
],
"options": {
"cwd": "${fileDirname}"
},
"dependsOn":["c++"]
}
],
"version": "2.0.0"
}
{
"tasks": [
{
"type": "cppbuild",
"label": "c++",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-c",
"${workspaceFolder}/*.cpp",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": false
},
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "custom cpp build",
"command": "g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"*.o"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [
"$gcc"
],
"options": {
"cwd": "${fileDirname}"
},
"dependsOn":["c++"]
}
],
"version": "2.0.0"
}
Even so it works, it gets quickly complicated and/or unoptimized as a way to build in c++. I'm going to use cmake as suggested from now on.

C++ compiler optimisations (MSYS2 MinGW-64 bit GCC compiler used with VSCode)

I'm trying to apply the different optimisation levels (-O0, -O1, -O2, -O3 and so on) to the compilation and execution of a .cpp file.
However, I can't figure out the exact syntax I need to use and where to write the instructions (i.e. in which terminal, the VSCode terminal or the MinGW terminal)?
Any help would be much appreciated.
tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: cpp.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\cpp.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: C:\\msys64\\mingw64\\bin\\cpp.exe"
}
],
"version": "2.0.0"
}
In this field:
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
add the optimization option:
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-O2"
],
Consider that you have two tasks (I am not sure why you have the second one) and you need to set it in the correct task. If I were you I would remove the unused one and instead create a task for debug and release build: Release build in Visual Studio Code
Be careful that this is about c# and cannot be copy pasted!

VS Code: How to set up task.json for building all .cpp and .h files

I started coding C++ and wanted to try in VS Code. I know it isn't the best program for that, I use VS 2019 but I wanted to try Code. I came to conclusion that VS Code builds only on .cpp file but my programs has 2 .cpp and 1 .h files. I followed Microsoft's tutorial on their site and installed all necessary programs and tools.
This is mine task.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${workspaceFolder}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
This gives me an error undefined reference to `myFunction()'
I believe that task.json is the problem here but I don't understand why isn't it working?

Pass CMake flags to VS Codes "tasks"?

So I have a CMakeLists.txt that works well and the solution runs in debug.
The tasks associated with VS Code (build active file, etc) call GCC ignoring the CMakeLists.txt. This is undesired because CMake sets required flags like the C++ standard version.
Anybody know how to pipe CMake into tasks.json?
For the default tasks look like:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-9 build active file",
"command": "/usr/bin/g++-9",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Generated task by Debugger"
},
{
"type": "cppbuild",
"label": "C/C++: cpp build active file",
"command": "/usr/bin/cpp",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: /usr/bin/cpp"
}
],
"version": "2.0.0"
}

How to work with multiple C++ file in VS Code?

In Visual studio code, I can compile and debug one C++ file easily. But when it comes to multiple C++ file with some header file, it does not work.
Here is the default tasks.json file
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
What changes do I have to make?