00. 目录
01. 问题描述
Visual Studio Code在添加自定义头文件的时候遇到错误“fatal error: xxx.h: No such file or directory”。
02. 问题分析
我用 VSCode 来 Coding,这个编辑器需要自己配置头文件路径,就是自动建立一个 c_cpp_properties.json 文件来管理头文件路径,然后需要用哪些库就手动加上即可。
03. 问题解决
3.1 按F1启动指令输入框,输入 C/C++,选择第一项 Edit Configuration:
3.2 会自动生成一个 Json 文件
3.3 添加头文件路径
我们只需要再红框的 IncludePath 内加上需要的头文件路径即可,比如我的工程:
Windows平台如下
Linux平台如下
所以使用#include "xxx.h"
的時候Visual studio code不会出现红线警示,因为这个时候intellisense已经知道这个路径了,但是编译器还不知道,所以编译的时候还是会报错。
04. 问题验证
无
05. 其它
Question Updated: I’m trying to build a c++ project on vscode using the C/C++ extension. The compiler complains about not finding header files (actually boost headers). I have included path to the root folder of boost, and Intellisense is also able to parse the header paths, but not the compiler. I checked the included header in my source is in the corresponding path in my filesystem. Is there any solution to make the compiler see the include headers?
This is my c_cpp_properties.json
file:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/Users/zz_ro/Documents/source/boost_1_70_0"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/mingw/bin/g++.exe",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
and this is my helloworld.cpp
file:
#include "boost/math/constants/constants.hpp"
#include "boost/multiprecision/cpp_dec_float.hpp"
#include <iostream>
#include <limits>
int main()
{
using boost::multiprecision::cpp_dec_float_50;
cpp_dec_float_50 seventh = cpp_dec_float_50(1) / 7;
std::cout.precision(std::numeric_limits<cpp_dec_float_50>::digits10);
std::cout << seventh << std::endl;
}
And here is the compiler output:
helloworld.cpp:1:46: fatal error: boost/math/constants/constants.hpp: No such file or directory
#include "boost/math/constants/constants.hpp"
^
compilation terminated.
The terminal process terminated with exit code: 1
If I change my tasks.json
from
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build hello world",
"type": "shell",
"command": "g++",
"args": [
"-g",
"helloworld.cpp",
"-o",
"helloworld"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
to
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build hello world",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-IC:\\Users\\zz_ro\\Documents\\source\\boost_1_70_0",
"helloworld.cpp",
"-o",
"helloworld"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
by just manually passing the include path as an argument to g++.exe
and the compilation will go through. It confuses me that in the tutorial (vscode tutorial) there is no mention about manually inserting include path to g++.exe
via command line parameters, where all of these are supposed to be done by modifying the includePath variable in c_cpp_property.json
. Did I misunderstand the tutorial or I didn’t set the includePath value properly?
05. 附录
参考:c_cpp_properties.json和task.json的作用对象文章来源:https://www.toymoban.com/news/detail-628854.html
参考:Visual studio code 添加自定义头文件路径文章来源地址https://www.toymoban.com/news/detail-628854.html
到了这里,关于【Tools】VSCode软件设置头文件路径的方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!