本文讲述使用VS code编写C++程序,使用非标准库的其他路径下的头文件时编译错误的问题解决办法。
官网路径已详细介绍了VS Code如何安装C++扩展以及配置编译环境:
https://code.visualstudio.com/docs/cpp/config-mingw
整个文章大致流程:
这里主要解决的问题是关于 使用非标准库的其他路径下的头文件时编译错误的问题解决办法:
fatal error: *.h: No such file or directory
相信大家都知道ctrl + shift + p 进入C/C++:编辑配置里有一个includePath的配置,但是我们把头文件路径配置进去后,编写代码的时候能智能提示了,但是编译却总是不能通过。
为什么呢?因为我们配置的只是智能提示(IntelliSense)的配置,而非编译生成的配置。
因此,我们需要把关注放到tasks.json上来。
我们知道默认情况下,该配置的内容大致如下
{
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
此刻我们增加如下:
{
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"-I",
"D:/thirdlib/rapidxml/",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
即:我们增加了一个 -I 选项,并指定了路径,知道g++编译的都知道,这是指定头文件目录。
当我们加完之后,再次编译,通过了。
那么,你现在也明白了吗?