如下图文件夹
> include: *.h文件
> out: 编译后的输出文件夹
> source:*.cpp文件
那我们要怎样配置vscode里的一些环境呢?
identifier "test_add" is undefinedC/C++(20)
说白了,就是test_add这个标识符不能识别(没有定义),可我们从上面的文件里看到了,该函数都是有的。再往下看,我们一个一个来解决。
出现如下,选择Debug Anyway
完成这一步了,我们可以参文件下面,会有一个.vscode的文件夹,还有两个.json的文件。
默认的如下,主要要修改三个地方
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
修改如下
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${workspaceFolder}/source/*.cpp", //all cpp from source
"-I", //include
"${workspaceFolder}/include", //include path
"-o",
"${workspaceFolder}/out/${fileBasenameNoExtension}" //out path
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
默认的launch.json如下,
只需要修改2版主即可
{
// 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": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
修改如下,都已经加注释了
{
// 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": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/out/${fileBasenameNoExtension}", //program output name, same as tasks.json -o path
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/out", //out here is the folder named out
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
保存以上文件后,已经不报错了
/usr/bin/g++ -g /mnt/d/tmp/c++/source/main.cpp -I /mnt/d/tmp/c++/include -o /mnt/d/tmp/c++/out/main
/tmp/ccBwxewk.o: In function `main':
/mnt/d/tmp/c++/source/main.cpp:8: undefined reference to `test_add()'
collect2: error: ld returned 1 exit status
tasks.json -g 后面的参数改为 “${workspaceFolder}/source/*.cpp”
Starting build...
/usr/bin/g++ -g /mnt/d/tmp/c++/source/*.cpp -o /mnt/d/tmp/c++/out/main
/mnt/d/tmp/c++/source/add.cpp:3:10: fatal error: add.h: No such file or directory
#include "add.h"
^~~~~~~
compilation terminated.
/mnt/d/tmp/c++/source/main.cpp:2:10: fatal error: add.h: No such file or directory
#include
^~~~~~~
compilation terminated.
Build finished with error(s).
The terminal process failed to launch (exit code: -1).
tasks.json 加上"-I", “${workspaceFolder}/include”,
c_cpp_properties.json配置文件默认是不会产生的,ctrl+shift+p 再输入configuration选择后便会出现。
在这里我们可以设置 includePath
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/include/**" //your include path
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
注:请注意,这里使用是的WSL环境下的g++。