c++ - 编译时vscode找不到 header ,而Intellisense可以

标签 c++ boost visual-studio-code

问题已更新: 我正在尝试使用 C/C++ 扩展在 vscode 上构建一个 C++ 项目。编译器提示找不到头文件(实际上是 boost 头文件)。我已经包含了 boost 根文件夹的路径,Intellisense 也能够解析标题路径,但不能解析编译器。我检查了我的源代码中包含的 header 是否在我的文件系统中的相应路径中。是否有任何解决方案可以让编译器看到包含 header ?

这是我的c_cpp_properties.json 文件:

{
    "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
}

这是我的helloworld.cpp 文件:

#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;
}

这是编译器的输出:

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

如果我将 tasks.json

{
    // 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
            }
        }
    ]
}

{
    // 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
            }
        }
    ]
}

只需手动将包含路径作为参数传递给 g++.exe 即可完成编译。令我困惑的是,在教程(vscode tutorial)中没有提到通过命令行参数手动插入 g++.exe 的包含路径,所有这些都应该通过修改 includePath 来完成c_cpp_property.json 中的变量。是我误解了教程还是没有正确设置 includePath 值?

最佳答案

c_cpp 属性用于智能感知。它不用于编译。编译器存在只是因为它被查询以找到它用于系统头文件的默认包含路径。

任务定义构建任务的运行方式。

Vscode 不连接两者。

关于c++ - 编译时vscode找不到 header ,而Intellisense可以,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56096045/

相关文章:

c++ - CMake:为我的项目设置警告标志,但不包含 header

C++双链表反向打印

c++ - 线程函数

python - Boost Python 重写相等运算符

visual-studio-code - 覆盖扩展中现有的 Visual Studio Code 命令

java - 如何在VSCODE中运行两个Java文件?

c++ - 从 C++ 中的任意类继承?

c++ - 如何/可能有一个 vector<unique_ptr<ABC>>?

c++ - lambda 绑定(bind)问题?

php - VSCode调试器未在Docker上为Laravel运行