官方教程:https://code.visualstudio.com/docs/cpp/config-mingw
tasks.json 负责编译生成可执行文件(.exe)
launch.json 进行代码调试
c_cpp_properties.json 拓展
1.这里用添加引用eigen类库举例:
首先添加eigen库的路径到IncludePath. (c_cpp_properties.json)
{
"configurations": [
{
"name": "GCC",
"includePath": [
"${workspaceFolder}/**",
"F:/ComputerGraphics/pa0/Data/eigen"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "E:\\Mingw\\bin\\gcc.exe",
"cStandard": "gnu11",
"cppStandard": "gnu++14",
"intelliSenseMode": "gcc-x86"
}
],
"version": 4
}
结果就是报错:No such file or directory
后来查阅资料得知vscode中c_cpp_properties.json的"includePath"只是告诉vscode插件在哪里找到头文件,便于进行源码查看和debug,并没有告诉gcc编译器这个路径。
所以编辑器看起来没有错误但是gcc编译器找不到新增的"includePath"
2. 添加eigen库的路径到编译参数"args".(tasks.json)
既然是编译器的问题,那么给它指定额外的参数就好了,在这里便是 “gcc -I ", 指定头文件路径
在"args"里加入"-I", F:/ComputerGraphics/pa0/Data/eigen/"
使用#include"file"的时候,gcc/g++会先在当前目录查找你所制定的头文件,如果没有找到会缺省的头文件目录找,如果使用-I制定了目录,会先在你所指定的目录查找,然后再按常规的顺序去找.
对于#include<file>,gcc/g++会到-指定的目录查找,查找不到,然后将到系统的缺省的头文件目录查找
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "E:\\Mingw\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"F:/ComputerGraphics/pa0/Data/eigen/"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: E:\\Mingw\\bin\\g++.exe"
}
]
}
运行task编译,大功告成
注意点:
c_cpp_properties.json:"includePath"负责文本编辑器代码查找等功能
tasks.json:"args"负责gcc等编译器的编译指令。
另外,更简单粗暴的办法就是把Eigen整个拷贝到项目文件夹,"${workspaceFolder}",这样用默认的task编译也能成功
参考:
https://code.visualstudio.com/docs/editor/variables-reference
https://blog.csdn.net/qq_29695701/article/details/95593003