我目前正在尝试新的C++20的特性,如使用GCC 10.1.0描述的模块,但是如果我试图构建下面的代码片段,编译器会抛出一大堆错误。
这就是我到目前为止写的片段:
// helloworld.cpp
export module helloworld; // module declaration
import <iostream>; // import declaration
export void hello() { // export declaration
std::cout << "Hello world!\n";
}// main.cpp
import helloworld; // import declaration
int main() {
hello();
}我正在使用g++ helloworld.cpp main.cpp -std=c++20编译它。
编译器给了我这个错误:
helloworld.cpp:2:1: warning: keyword ‘export’ not implemented, and will be ignored
2 | export module helloworld; // module declaration
| ^~~~~~
helloworld.cpp:2:8: error: ‘module’ does not name a type
2 | export module helloworld; // module declaration
| ^~~~~~
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
3 | import <iostream>; // import declaration
| ^~~~~~~~
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
helloworld.cpp:3:1: error: ‘import’ does not name a type
3 | import <iostream>; // import declaration
| ^~~~~~
helloworld.cpp:5:1: warning: keyword ‘export’ not implemented, and will be ignored
5 | export void hello() { // export declaration
| ^~~~~~
helloworld.cpp: In function ‘void hello()’:
helloworld.cpp:6:10: error: ‘cout’ is not a member of ‘std’
6 | std::cout << "Hello world!\n";
| ^~~~
helloworld.cpp:1:1: note: ‘std::cout’ is defined in header ‘<iostream>’; did you forget to ‘#include <iostream>’?
+++ |+#include <iostream>
1 | // helloworld.cpp
main.cpp:2:1: error: ‘import’ does not name a type
2 | import helloworld; // import declaration
| ^~~~~~
main.cpp: In function ‘int main()’:
main.cpp:5:5: error: ‘hello’ was not declared in this scope
5 | hello();
| ^~~~~我做错什么了?
发布于 2020-07-06 22:53:24
GCC的语言状态页面说它还不支持模块。
C++20支持是不完整的(考虑到我们在2020年,这是足够公平的!)C++20技术上还不存在…)。
但是,使用一些标志和一个开发分支,您可以使用正在进行中的实现--在GCC模Wiki上阅读更多有关它的信息。
GCC 10的默认语言版本是C++14;GCC 11 up to C++17。
发布于 2021-01-23 04:14:45
我在23.1.21的时候看到了GNU gcc的网站,它说你必须包括一个名为-fmodules ts的旗帜。以下是该网站的链接,以获取其他信息https://gcc.gnu.org/wiki/cxx-modules
发布于 2022-09-08 04:02:16
现在,如果你用GCC的话,你就可以完成:
# g++ -std=c++20 -fmodules-ts hello.cpp -o hello.exe -x c++-system-header iostream来自:https://stackoverflow.com/a/73643558/19946116
如果您更喜欢Visual 2022,则需要配置它:https://stackoverflow.com/a/73643523/19946116。
https://stackoverflow.com/questions/62765630
复制相似问题