I run VScode on ubuntu16, and test the helloworld.cpp.
At first, I walked through all, then I tried to edit the tasks.json.
I got the error. Ok, I cancel all the the change with rollback, it shows error aagin.
I remove the helloworld folder and try again the helloworld.
After Terminal > Configure Default Build Task and build, the error shows again.
What's the wrong? It is got from vscode.
The error message:
Executing task: /usr/bin/g++ -g /home/liwenz/helloworld/.vscode/tasks.json -o /home/liwenz/helloworld/.vscode/tasks <
/usr/bin/ld:/home/liwenz/helloworld/.vscode/tasks.json: file format not recognized; treating as linker script
/usr/bin/ld:/home/liwenz/helloworld/.vscode/tasks.json:1: syntax error
collect2: error: ld returned 1 exit status
The terminal process terminated with exit code: 1
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
the helloworld link is https://code.visualstudio.com/docs/cpp/config-linux
You have to switch from the tasks.json file to the actual code you want to compile or debug.
As I build, I open tasks.json. So ${File}=tasks.json
I should open helloworld.cpp, then ${File}=helloword.cpp, and build ok.
Related
I had a custom build system in sublime for C++ which takes input from one file and redirects the output to another.
My sublime build system is as follows :
{
"cmd": ["g++.exe","-std=c++14", "${file}", "-o", "${file_base_name}.exe", "&&" , "${file_base_name}.exe<inputf.in>outputf.in"],
"selector":"source.cpp",
"shell":true,
"working_dir":"$file_path"
}
this helped in taking input from "inputf.in" file and print the output in "outputf.in" whenever i ran my program.
I want similar functionality in vs code but dont know how to configure build-task.
Here is a build task for a Node addon, you have to put it in .vscode/tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "shell",
"command": "npx node-pre-gyp build -j max",
"problemMatcher": {
"base": "$gcc",
"fileLocation": [
"relative",
"${workspaceFolder}/build"
]
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
You can press Ctrl-Shift-P to bring the command palette and select configure build task.
The problem matcher allows VSCode to highlight the compiler errors in your editor.
The default build task is the one you launch with a hotkey.
The vscode report an error while building a c program. The error message is as below.
Cannot build and debug because the active file is not a C or C++ source file.
The terminal process failed to launch (exit code: -1).
The task config file is as below.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"/home/xxx/tmp/test/main.c"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
I have a c file named main.c under the folder /home/xxx/tmp/test which is the workspace folder. What might be the cause of the problem?
As seen in this reply from Sean McManus, you need to get rid of all ${file} references in your tasks.json, for example:
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
}
change to
"args": [
"-g",
"test.cpp",
"-o",
"libs/test.o"
],
"options": {
"cwd": "${fileDirname}"
}
And change type of build from cppBuild to shell.
Here's my tasks.json for reference:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "build active file with debug",
"command": "/usr/bin/g++",
"args": [
"${file}",
"-o",
"${workspaceFolder}/out/${relativeFileDirname}.out",
"-g"
],
"problemMatcher": ["$gcc"],
"group": "build",
"detail": "compiler: /usr/bin/g++"
},
]
}
Here's a video to demonstrate the output: https://imgur.com/a/tvurtX1
In the video I am compiling a file 12-how-to-debug-effectively/main.cpp, and according to my tasks.json, the output file should be out/12-how-to-debug-effectively.out, but for some reason the variable substitution doesn't work and I instead get out/.out.
Any pointers where I might be going wrong?
cppbuild is not a valid value for the type argument.
All the examples on the VSC doc site use "type": "shell"
When using "type": "shell" I can see the command to be executed and the variables are filled in correct. (I used a 1 word subdirectory mysite)
Using "type": "cppbuild" I can't see which command is executed.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: 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
},
"detail": "compiler: /usr/bin/g++"
}
]
}
Starting build...
Build finished with error:
/usr/bin/x86_64-linux-gnu-ld:/home/chenxin/Documents/projects/helloworld/.vscode/tasks.json: file format not recognized; treating as linker script
/usr/bin/x86_64-linux-gnu-ld:/home/chenxin/Documents/projects/helloworld/.vscode/tasks.json:1: syntax error
collect2: error: ld returned 1 exit status
The terminal process failed to launch (exit code: -1).
Terminal will be reused by tasks, press any key to close it.
You are actually trying to compile a JSON file other than the cpp file you are using. You should first switch to the tab of the cpp code and then run the task or run the build task
I would like to build my program using Git Bash or MinGW so I have this build task:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "gcc.exe build active file",
"command": "gcc",
"args": ["-g", "${relativeFile}", "-o", "${fileBasenameNoExtension}"],
"options": {
"shell": {
"executable": "C:\\Program Files\\Git\\bin\\bash.exe",
"args": ["-l"]
},
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
},
]
}
I have two issues:
The cwd command does not work because ${fileDirname} is in the Windows form C:\Foo\Bar and since I am on bash I would like the POSIX form /c/Foo/Bar.
My task is not executed in the current terminal, but in a weird Window that display no output:
/usr/bin/bash: gcc -g .vscode\tasks.json -o tasks: No such file or directory
The terminal process terminated with exit code: 127
Terminal will be reused by tasks, press any key to close it.
With the extension Command Variable (v0.2.0) you can add Posix versions of the directory variables.
"cwd": "${command:extension.commandvariable.file.fileDirnamePosix}"