我想要实现的是将多个.cpp
和.h
文件的编译和链接步骤分开。像这样:g++ -c *.cpp && g++ -o main *.o
。但是我想在vscode tasks.json
中这样做。我尝试了以下代码:
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"
}
收到的错误如下:
<<h2>控制台/h2>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
我该怎么办?
下面的注释建议和vscode复合任务文档。找到了一种编译和生成可执行文件的方法,代码如下:
{
"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"
}
即便如此,作为c++构建的一种方式,它也会很快变得复杂和/或未优化。从现在开始,我将按照建议使用cmake。