编译c++的问题



我正在尝试使用一些新的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 includes。所以我试了:

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进行编译——这只是一个使用新功能的例子——我在例如import中遇到了同样的问题。我真的在努力获得任何排序-哪个编译器是c++20的最佳用例,以及我如何以及在哪里可以包括/定位具有c++20支持的标准库?如何在Xcode中使用所有东西?

gcc 10还不支持此功能。您可以使用gcc trunk或gcc模块分支,并且您需要将-fmodules-ts添加到命令行。

最新更新