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"
}
Related
I'm attemping to follow this article to be able to code in c++ on MacOS, but my code isn't running? Or the output isn't going into the terminal.
I've gotten to the "Run helloworld.cpp" step in the article and have chosen "C/C++: clang++ build and debug active file." When I run the code, it brings me to the debug console and says main is asking for permission to access files in my Desktop folder.
Here is my tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-fdiagnostics-color=always",
"-std=c++17",
"-stdlib=libc++",
"-g",
"-pedantic-errors",
"-Wall",
"-Weffc++",
"-Wextra",
"-Wsign-conversion",
"-Werror",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Build finished successfully.
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.
I'm new to using C++ and have been trying to figure this out for hours. I'm using windows. I installed VScode, the extension for C++, installed MSYS2, and set up clang++. Now I'm trying to run file hello.cpp in VScode using their terminal. It's just hello world. When using the below task, it executes but then throws "Unable to start debugging. The value of miDebuggerPath is invalid." The hello world does not output and I'm at a loss. Does anyone know why this is happening and have suggestions on how to fix? Thank you and much appreciated!
Here is a pic of the commands I've been trying to run in the terminal
Executing task: C/C++: clang++.exe build active file <
Starting build...
C:\msys64\mingw64\bin\clang++.exe -fdiagnostics-color=always -g C:\Users\Daniel\Desktop\test\hello.cpp -o C:\Users\Daniel\Desktop\test\hello.exe
Build finished successfully.
My code
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;}
My launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []}
My tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\clang++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: clang-cl.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\clang-cl.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: clang-cpp.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\clang-cpp.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"}
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!
I was tryna transition from seperate IDEs to a single one where I can use with everything (Python, C++ and Web). I chose VSCode, since it had all of the necessary stuff in it. I finished setting up Conda and Python, but when I got to C++ I had a problem compiling my task.json file. The error was that wchar.h couldn't be found. It compiles and works fine on XCode, and CLion, but the Clang just doesn't work on VSCode. Any ideas on how to fix this?
Thanks
HJ
Here is the error code for reference
In file included from /Users/kimh2/Desktop/Coding Stuff/C++/HelloWorld/main.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:215:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:96:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/wchar.h:119:15: fatal error:
'wchar.h' file not found
#include_next <wchar.h>
^~~~~~~~~
1 error generated.
The terminal process terminated with exit code: 1
The task.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": "clang++ build active file",
"command": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "shell",
"label": "clang++ build active file",
"command": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}