将 llvm-11 与使用 -std=c++2a 编译的 gcc-11 中的标准库头结合使用时出错
我尝试将 clang 与 gcc 标准库标头一起使用,如下所示:
/opt/rh/llvm-toolset-11.0/root/usr/bin/clang -MD -MF bazel-out/k8-fastbuild/bin/external/com_google_googletest/_objs/gtest/gtest-typed-test.d '-frandom-seed=bazel-out/k8-fastbuild/bin/external/com_google_googletest/_objs/gtest/gtest-typed-test.o' -iquote external/com_google_googletest -iquote bazel-out/k8-fastbuild/bin/external/com_google_googletest -isystem external/com_google_googletest/googlemock -isystem bazel-out/k8-fastbuild/bin/external/com_google_googletest/googlemock -isystem external/com_google_googletest/googlemock/include -isystem bazel-out/k8-fastbuild/bin/external/com_google_googletest/googlemock/include -isystem external/com_google_googletest/googletest -isystem bazel-out/k8-fastbuild/bin/external/com_google_googletest/googletest -isystem external/com_google_googletest/googletest/include -isystem bazel-out/k8-fastbuild/bin/external/com_google_googletest/googletest/include -isystem /opt/rh/devtoolset-11/root/usr/include/c++/11 -isystem /opt/rh/devtoolset-11/root/usr/include/c++/11/bits -isystem /opt/rh/devtoolset-11/root/include/c++/11/x86_64-redhat-linux/bits -fdiagnostics-color -Wfatal-errors '-std=c++2a' -Wall -Wno-sign-compare '--gcc-toolchain=/opt/rh/devtoolset-11/root' -Wheader-guard -pthread -c external/com_google_googletest/googletest/src/gtest-typed-test.cc -o bazel-out/k8-fastbuild/bin/external/com_google_googletest/_objs/gtest/gtest-typed-test.o
然后我收到此错误:
In file included from external/com_google_googletest/googletest/include/gtest/gtest.h:62:
In file included from external/com_google_googletest/googletest/include/gtest/internal/gtest-internal.h:40:
In file included from external/com_google_googletest/googletest/include/gtest/internal/gtest-port.h:395:
/opt/rh/devtoolset-11/root/usr/include/c++/11/bits/regex.h:56:9: fatal error: use of undeclared identifier 'regex_constants'
regex_constants::match_flag_type __flags);
错误的原因可能是什么? gcc 和 clang 之间是否不兼容?我应该安装 clang 标头和 libc++ 吗?这是通过安装包 llvm-dev 制作的吗?
I am trying to use clang together with gcc standard library headers as follows:
/opt/rh/llvm-toolset-11.0/root/usr/bin/clang -MD -MF bazel-out/k8-fastbuild/bin/external/com_google_googletest/_objs/gtest/gtest-typed-test.d '-frandom-seed=bazel-out/k8-fastbuild/bin/external/com_google_googletest/_objs/gtest/gtest-typed-test.o' -iquote external/com_google_googletest -iquote bazel-out/k8-fastbuild/bin/external/com_google_googletest -isystem external/com_google_googletest/googlemock -isystem bazel-out/k8-fastbuild/bin/external/com_google_googletest/googlemock -isystem external/com_google_googletest/googlemock/include -isystem bazel-out/k8-fastbuild/bin/external/com_google_googletest/googlemock/include -isystem external/com_google_googletest/googletest -isystem bazel-out/k8-fastbuild/bin/external/com_google_googletest/googletest -isystem external/com_google_googletest/googletest/include -isystem bazel-out/k8-fastbuild/bin/external/com_google_googletest/googletest/include -isystem /opt/rh/devtoolset-11/root/usr/include/c++/11 -isystem /opt/rh/devtoolset-11/root/usr/include/c++/11/bits -isystem /opt/rh/devtoolset-11/root/include/c++/11/x86_64-redhat-linux/bits -fdiagnostics-color -Wfatal-errors '-std=c++2a' -Wall -Wno-sign-compare '--gcc-toolchain=/opt/rh/devtoolset-11/root' -Wheader-guard -pthread -c external/com_google_googletest/googletest/src/gtest-typed-test.cc -o bazel-out/k8-fastbuild/bin/external/com_google_googletest/_objs/gtest/gtest-typed-test.o
Then I get this error:
In file included from external/com_google_googletest/googletest/include/gtest/gtest.h:62:
In file included from external/com_google_googletest/googletest/include/gtest/internal/gtest-internal.h:40:
In file included from external/com_google_googletest/googletest/include/gtest/internal/gtest-port.h:395:
/opt/rh/devtoolset-11/root/usr/include/c++/11/bits/regex.h:56:9: fatal error: use of undeclared identifier 'regex_constants'
regex_constants::match_flag_type __flags);
What could be the reason for the error? Is there an incompatibility between gcc and clang? Should I instead install clang headers and libc++ and is that made by installing package llvm-dev?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
gtest-port.h
文件包含一个带有#include
的文件(请参阅 此处获取代码)。它期望该文件是 POSIXregex.h
,通常直接安装在前缀/usr/include
下。正如您在错误消息中看到的,编译器尝试包含/usr/include/c++/11/bits/regex.h
这是错误的文件。.../bits/
中的头文件并不意味着由用户代码直接包含。它们是标准库实现的内部。因此,直接包含它会失败(缺少的符号可能是在另一个内部头文件中定义的),这对我来说并不奇怪。为了解决您的问题,我建议您尝试在编译命令中省略
.../bits
目录*。我不知道谁告诉你要包含它们,但它们并不意味着要添加到编译器搜索路径中。* 从编译器命令行中删除这两个标志:
The
gtest-port.h
file includes a file with#include <regex.h>
(see here for the code). It expects the file to be the POSIXregex.h
which is normally installed directly under the prefix/usr/include
. As you can see in the error message, the compiler instead tries to include the/usr/include/c++/11/bits/regex.h
which is the wrong file.The header files in
.../bits/
are not meant to be included directly by user code. The are internal to the standard library implementation. Thus it is no surprise to me, that directly including it fails (the missing symbol is probably defined in another internal header file).To solve your problem I suggest you try to leave out the
.../bits
directories* in your compile command. I do not know who told you to include them, but they are not meant to be added to the compiler search path.* drop these two flags from the compiler command line: