萌新求助,请问在 Clion(2023.3.3)中使用 C++Modules 的正确方式是什么?

95 天前
 autumnshine

各位大佬好,我最近在学习 C++,使用的是《 C++20 入门实践》这本书,里面第一个 Demo 就是使用 C++Modules 编写成的,但是我发现书中的代码我在 Clion 中编译报错。

代码如下:

import <iostream>;

int main() {
    int answer{42};
    std::cout << "The answer to life, the universe, and everything is "
              << answer
              << std::endl;
}

相关图片及说明: https://imgur.com/a/MGRU5qD

另外我在 Clion 的官方网站上 https://www.jetbrains.com/help/clion/support-for-c-20-modules.html 找到关于 C++ Modules 的支持情况, 里面有说到

For now, CLion does not consider .cpp files to be modules, so it's recommended that you use other extensions (for example, .cppm).

我把文件的扩展名改为 .cppm 后,也是报错的。 最后,我想请问下如何优雅的在 Clion 中使用 C++ Modules ?

1380 次点击
所在节点    C++
13 条回复
littlewing
95 天前
g++ -fmodules-ts --std=c++20 test1.cpp -x c++-system-header iostream

```
g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.4.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
```
littlewing
95 天前
@littlewing fix
g++ -fmodules-ts --std=c++20 test1.cpp
littlewing
95 天前
@littlewing 不加 -fmodules-ts 这参数就会报和你一样的错
autumnshine
95 天前
@littlewing 谢谢,使用这条命令可以进行编译。
kirory
95 天前
用 gcc-14, clang-17 ,等 Clion 更新
工具链普及前还是不方便,建议等
kirory
95 天前
https://www.kitware.com/import-cmake-the-experiment-is-over/ 虽然这些工具都已经 Release 了,但随系统分发的不一定是
autumnshine
94 天前
@kirory 是的,我也感觉现在工具链还不太普及,所以我现在还是用#include 指令 引入库文件使用了😂,这篇文章我参考下,谢谢你😊
tyzandhr
94 天前
在 Windows 上使用 msvc 工具链,无需额外配置
autumnshine
94 天前
@tyzandhr 是指直接用 VS 吗?我用 MSVC 编译器 + Clion 是报错的。VS 太大了,我不太想安装😂。所以现在还是暂时不使用 Modules 了,先学习其他语法。
zeal7s
94 天前
看我之前发的帖子,虽然会有 warning ,但是能够正常使用

https://v2ex.com/t/1007511
autumnshine
94 天前
@zeal7s 谢谢,其实你的这篇帖子我很早就看到了,但是因为我目前对 C++上面的一些工具链(如 CMake 等工具)的用法并不熟悉,所以看不太懂为什么要这么写。这个提问的初衷是想问有没有一个开箱即用的,对萌新友好的使用方式😂。不过现在看来这里水有点深,目前我还 hold 不住😂,所以现在我就先使用#include 的方式,先学习语法了。总之,谢谢你的解答。
tyzandhr
93 天前
@autumnshine 可以只安装 msbuild+msvc 的。vs 都嫌大,碰到 boost 和 qt 怎么办啊
autumnshine
93 天前
@tyzandhr 哈哈哈哈,还有一个原因是因为用惯 Jetbrains 家 IDE ,就懒得换了😂,后面学到一些框架的时候再说就好了,目前先不折腾 Modules 了,谢谢你。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1014408

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX