我正在尝试使用一些新的 c++20 功能,但我什至无法编译一些简单的代码行.. 我正在 macOS 上工作,首先尝试使用 c++2a 语言方言在 Xcode 中编译它,但没有成功,然后尝试使用 g++ 从命令行编译代码,但我遇到了类似的错误。 一切都是最新的(Clang Vers 12,g++/gcc vers 10),我知道某些功能仍然存在一些问题。
这是一个例子:
import <iostream>;
#include <vector>
int main(){
std::vector<int> vec{1,2,3};
for (int e : vec) std::cout << e << std::endl;
}
编译:
g++ -std=c++2a -fmodules-ts test.cpp
抛出以下错误:
header file <iostream> (aka '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream') cannot be imported because it is not known to be a header
所以我认为工具链的头路径有问题,没有最新的更新,所以我尝试将路径更改为 g++ main 包含。 所以我尝试了:
g++ -std=c++2a -fmodules-ts -I/usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/ test.cpp
引发此错误的原因:
test.cpp:1:8: error: header file <iostream> (aka '/usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/iostream') cannot be imported because it is not known to be a header unit
import <iostream>;
^
In file included from test.cpp:2:
In file included from /usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/vector:60:
/usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/bits/stl_algobase.h:59:10: fatal error: 'bits/c++config.h' file not found
#include <bits/c++config.h>
^~~~~~~~~~~~~~~~~~
2 errors generated.
有人可以帮忙吗? 我知道我可以使用 #include 进行编译 - 这只是作为使用新功能的一个例子 - 我有同样的问题,例如进口 。 我真的在努力整理任何东西——哪个编译器是 c++20 的最佳用例,我如何以及在哪里可以包含/找到支持 c++20 的标准库? 以及如何在 Xcode 中一起使用所有内容?
答案 0 :(得分:0)
gcc 10 尚不支持此功能。您可以使用 gcc trunk 或 gcc modules 分支,并且需要在命令行中添加 -fmodules-ts
。