【发布时间】:2021-02-11 18:39:52
【问题描述】:
我是 C++ 编程的新手,所以请不要判断我在设置“Visual Studio Code”环境时遇到的问题。我正在尝试使用 blaze 数学包来解决二次规划问题。使用 MinGW GCC,我可以通过 cmd 成功编译 blaze 的测试文件,因此我想将 GCC 用于 VS Code。
操作系统:Windows 10.0.19041
GCC:gcc(x86_64-posix-seh-rev0,由 MinGW-W64 项目构建)7.3.0 || (cmd: gcc --version)
VS 代码扩展:C/C++ 0.27.1
首先我浏览了 MinGW 的 VS Code 教程: https://code.visualstudio.com/docs/cpp/config-mingw
这很好,所以我可以轻松地编译我的 helloworld.cpp。生成的 tasks.json 文件如下所示 tasks.json.
作为我的包管理器(用于 blaze 或其他包),我使用 vspkg-git: https://docs.microsoft.com/en-us/cpp/build/vcpkg?view=msvc-160 因此我在 Windows 上编程我不能使用“集成”命令将路径添加到包含路径。所以我必须手动完成。
我的包在绝对路径的文件夹中
C:\Users\Johannes\Desktop\Masterthesis\vcpkg\vcpkg\packages
所以我在“c_cpp_propertier.json”文件中添加了路径
{
"configurations": [
{
"name": "GCC",
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"includePath": [
"${workspaceFolder}",
"C:/Users/Johannes/Desktop/Masterthesis/vcpkg/vcpkg/packages/**"
],
"compilerPath": "C:/Program Files/mingw-w64/x86_64-7.3.0-posix-seh-rt_v5-rev0/mingw64/bin/g++.exe",
"browse": {
"path": []
}
}
],
"version": 4
}
文件夹中有几个包,因此我在路径末尾添加了“/**”以启用递归搜索头文件。
我的“helloworld.cpp”文件如下所示
#include <iostream>
#include <vector>
#include <string>
//#include <blaze/Math.h>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the
C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
我的问题是,当我尝试从此路径 for example Math.h 包含头文件时,Visual Studio 会抛出错误
blaze/Math.h: No such file or directory
但是当我右键单击包含并单击“转到定义”时,VS Code 会打开文件。 Optionbar 和 opened file。
日志诊断给出that information。
我猜有使用 MinGW 和 VS Code 中的其他软件包经验的人会非常简单地解决这个问题,但我几乎阅读了所有关于这些问题的帖子,但没有找到任何与我匹配的内容。
【问题讨论】:
-
疯狂猜测,我认为不是这样,但尝试使用带有反斜杠的
#include <blaze\Math.h>。 -
感谢回答,我试过了,但仍然出现错误
-
好的,我得到了答案。 “c_cpp_properties.json”文件的包含路径仅适用于 IntelliSense。这意味着 Visual Studio Code 将找到此包,并且 IntelliSense 将建议您从路径中获得可用的标头。这并不意味着编译器可以找到这些路径。因此,您必须将路径添加到“tasks.json”文件。正如您在上面的“tasks.json”文件照片中看到的那样,有一个名为“args”的字段,意思是“参数”。这些是编译器参数。您还必须以“-I”、“C:/PathYouWishToAdd”格式添加路径。这很好用!
-
太棒了!我建议你写一个答案。
标签: c++ visual-studio-code path include mingw