vscode cannot find header when compile while Intellisense can

橙三吉。 提交于 2020-06-23 13:36:06

问题


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?


回答1:


c_cpp properties is for intellisense. It is not used for compiling. The compiler is there only because it is queried to find the default include paths it uses for system headers.

The tasks defines how the build task runs.

Vscode doesn't connect the two.



来源:https://stackoverflow.com/questions/56096045/vscode-cannot-find-header-when-compile-while-intellisense-can

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!