M2 Mac下VSCode使用Boost Multiprecision遇头文件找不到问题求助
问题:clang++编译提示'cpp_int.hpp'文件未找到(M2 Mac + VSCode + vcpkg Boost)
在M2芯片Mac的VSCode环境中,使用clang++编译代码,已通过vcpkg安装并关联Boost Multiprecision库,但编译时始终提示“'cpp_int.hpp' file not found”错误。输入#include语句时IntelliSense可自动补全该文件,说明路径配置对IntelliSense生效,但编译器编译阶段报错。
测试代码
#include<iostream> #include<cpp_int.hpp> int main(){ using namespace std; boost::multiprecision::cpp_int test = 1; cout << test << endl; return 0; }
c_cpp_properties.json配置
{ "configurations": [ { "name": "Mac", "includePath": [ "${workspaceFolder}/**", "${vcpkgRoot}/arm64-osx/include/**", "${vcpkgRoot}/arm64-osx/include/boost/multiprecision/**" ], "defines": [], "macFrameworkPath": [ "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks" ], "compilerPath": "/usr/bin/clang", "cStandard": "c17", "cppStandard": "c++17", "intelliSenseMode": "macos-clang-arm64" } ], "version": 4 }
tasks.json配置
{ "version": "2.0.0", "tasks": [ { "type": "cppbuild", "label": "C/C++: clang++ build active file", "command": "/usr/bin/clang++", "args": [ "-I${vcpkgRoot}/arm64-osx/include/boost/multiprecision/**", "-std=c++17", "-stdlib=libc++", "-fcolor-diagnostics", "-fansi-escape-codes", "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "compiler: /usr/bin/clang++" } ] }
解决方法
1. 修正头文件引用方式
Boost库的头文件需要从boost根目录开始引用,原代码直接包含<cpp_int.hpp>是错误的,应修改为:
#include <boost/multiprecision/cpp_int.hpp>
2. 修正tasks.json中的编译路径参数
clang++的-I参数不支持/**通配符,只需要指定到包含boost目录的上级路径即可,也就是${vcpkgRoot}/arm64-osx/include。修改后的args配置如下:
"args": [ "-I${vcpkgRoot}/arm64-osx/include", "-std=c++17", "-stdlib=libc++", "-fcolor-diagnostics", "-fansi-escape-codes", "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ]
3. 简化c_cpp_properties.json的includePath
只保留${vcpkgRoot}/arm64-osx/include/**即可,通配符会自动遍历所有子目录,无需单独添加boost/multiprecision的路径:
"includePath": [ "${workspaceFolder}/**", "${vcpkgRoot}/arm64-osx/include/**" ]
完成以上修改后,重新执行编译任务即可解决文件未找到的错误。
内容的提问来源于stack exchange,提问作者dman
相关产品推荐
相关产品推荐

