V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
autumnshine
V2EX  ›  C++

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

  •  
  •   autumnshine · 82 天前 · 1319 次点击
    这是一个创建于 82 天前的主题,其中的信息可能已经有所发展或是发生改变。

    各位大佬好,我最近在学习 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 ?

    13 条回复    2024-02-07 09:43:41 +08:00
    littlewing
        1
    littlewing  
       82 天前
    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
        2
    littlewing  
       82 天前   ❤️ 1
    @littlewing fix
    g++ -fmodules-ts --std=c++20 test1.cpp
    littlewing
        3
    littlewing  
       82 天前
    @littlewing 不加 -fmodules-ts 这参数就会报和你一样的错
    autumnshine
        4
    autumnshine  
    OP
       82 天前
    @littlewing 谢谢,使用这条命令可以进行编译。
    kirory
        5
    kirory  
       82 天前   ❤️ 1
    用 gcc-14, clang-17 ,等 Clion 更新
    工具链普及前还是不方便,建议等
    kirory
        6
    kirory  
       82 天前   ❤️ 1
    https://www.kitware.com/import-cmake-the-experiment-is-over/ 虽然这些工具都已经 Release 了,但随系统分发的不一定是
    autumnshine
        7
    autumnshine  
    OP
       82 天前
    @kirory 是的,我也感觉现在工具链还不太普及,所以我现在还是用#include 指令 引入库文件使用了😂,这篇文章我参考下,谢谢你😊
    tyzandhr
        8
    tyzandhr  
       81 天前 via Android
    在 Windows 上使用 msvc 工具链,无需额外配置
    autumnshine
        9
    autumnshine  
    OP
       81 天前
    @tyzandhr 是指直接用 VS 吗?我用 MSVC 编译器 + Clion 是报错的。VS 太大了,我不太想安装😂。所以现在还是暂时不使用 Modules 了,先学习其他语法。
    zeal7s
        10
    zeal7s  
       81 天前   ❤️ 1
    看我之前发的帖子,虽然会有 warning ,但是能够正常使用

    https://v2ex.com/t/1007511
    autumnshine
        11
    autumnshine  
    OP
       81 天前
    @zeal7s 谢谢,其实你的这篇帖子我很早就看到了,但是因为我目前对 C++上面的一些工具链(如 CMake 等工具)的用法并不熟悉,所以看不太懂为什么要这么写。这个提问的初衷是想问有没有一个开箱即用的,对萌新友好的使用方式😂。不过现在看来这里水有点深,目前我还 hold 不住😂,所以现在我就先使用#include 的方式,先学习语法了。总之,谢谢你的解答。
    tyzandhr
        12
    tyzandhr  
       80 天前 via Android   ❤️ 1
    @autumnshine 可以只安装 msbuild+msvc 的。vs 都嫌大,碰到 boost 和 qt 怎么办啊
    autumnshine
        13
    autumnshine  
    OP
       80 天前
    @tyzandhr 哈哈哈哈,还有一个原因是因为用惯 Jetbrains 家 IDE ,就懒得换了😂,后面学到一些框架的时候再说就好了,目前先不折腾 Modules 了,谢谢你。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2390 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 16:08 · PVG 00:08 · LAX 09:08 · JFK 12:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.