Modules in C++ 20 - Thoughts on Compiler & CMake Support

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 ก.ค. 2024
  • ❗❗❗ UPDATE ❗❗❗
    Stream where we use the NEW CMake modules support: • C++ 20 Modules & CMake...
    Most modern languages use modules to separate code into logical components. In C++20, modules were introduces as an alternative to the traditional header/source combo, to offer many benefits such as reducing name clashes with functions and macros, speed up compilation times, and code organisation.
    In this video, we talk about the current state of the support for modules in CMake, MSVC, Clang, and GCC. As well as a quick example of how to use this feature with a modern compiler and a modern version of CMake.
    Github project with relevant links: github.com/matheusgomes28/cma...
    Contents of the video:
    00:00 - What is a C++ module?
    01:50 - Differences and benefits of C++20 modules
    03:36 - State of support for C++20 modules
    06:30 - Using modeules with CMake
    12:31 - Get the code and final thoughts
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 15

  • @codingwithmat
    @codingwithmat  6 หลายเดือนก่อน +1

    I've posted a new stream video where we play with the new CMake C++20 Modules support: th-cam.com/video/WdY_NkUQBOE/w-d-xo.html

  • @tomw1703
    @tomw1703 ปีที่แล้ว +1

    Thanks for making this video! Very interesting and helpful.

    • @codingwithmat
      @codingwithmat  11 หลายเดือนก่อน

      Thanks for watching my friend :)

  • @Aditya-ny8ur
    @Aditya-ny8ur 8 หลายเดือนก่อน +2

    You can import standard library headers like `import ;`. You need to pass `-std=c++20 -stdlib=libc++ -fimplicit-modules -fimplicit-module-maps -fprebuilt-module-path=.` to the clang compiler
    yes, by using llvm's library

    • @codingwithmat
      @codingwithmat  8 หลายเดือนก่อน +1

      Indeed, but not all compilers have "import std" yet :(

  • @IamusTheFox
    @IamusTheFox 9 หลายเดือนก่อน +4

    I mean to expand on what you said -- as bad as MSVC's library may be, compared to so many C libraries it's nothing. Imagine trying to use two C libraries that both try to redefine main using a macro that you don't realize, because you just wrote a regular main.

    • @codingwithmat
      @codingwithmat  9 หลายเดือนก่อน +1

      We can agree that there are many "badly written" libraries out there! But as you can tell, the MSVC's MIN and MAX definitions scarred me for life ;)
      Thanks for watching!

    • @IamusTheFox
      @IamusTheFox 9 หลายเดือนก่อน +1

      @@codingwithmat thanks for great video!
      I was scared from SDL2 and a unit testing library that both redefined main lol

  • @multiHappyHacker
    @multiHappyHacker 9 หลายเดือนก่อน +1

    Did they fix the problem with needing to export functions that should not need to be?

  • @theintjengineer
    @theintjengineer ปีที่แล้ว +6

    Nice video, Mat!
    I am on Windows, using the latest MSVC, because the other compilers still suck at supporting the newer features (too far behind), and I literally have:
    ```
    module;
    export module my_module;
    import std;
    // your lib
    ```
    And if I am at C++20 and don't have the modularised Standard Library, then
    ```
    module;
    export module my_module;
    import ;
    // import other libs you need, like `import ;` etc...
    // your lib
    ```
    I have no `#include`s in my code whatsoever.
    Even if I have to use a library that's not modularised, I `import` it, instead of `#include`ing it;
    But sure, if one isn't using *cl* with *Visual Studio 17 2022* Generator (e.g. *clang* + *ninja* ), then your way seems like the only one.
    And yeah, there's no way I am going back to header includes; and even if modules didn't provide any performance gain, I'd still use them just because of how they allow us to organise and structure our code.

    • @codingwithmat
      @codingwithmat  ปีที่แล้ว +2

      Thanks for watching my guy!
      Regarding the import std libraries situation, I found that the "import std;" or "import " works well with MSVC, but neither GCC or Clang seem to support that just yet :( Further reading online implies that the MSVC std imports aren't actually standard, so I thought it may be better to leave out that way if people are trying this code with Clang/Gcc.
      I'll make sure to do a nice video update when it all becomes well supported. It's a shame that, even in 2023, modules isn't finalised in all standard compilers...

    • @dibyojyotibhattacherjee4279
      @dibyojyotibhattacherjee4279 9 หลายเดือนก่อน

      ​@@codingwithmatyet it is one of the most awaited features in the C++ family..

  • @heavymetalmixer91
    @heavymetalmixer91 9 หลายเดือนก่อน +3

    Now that Clang, GCC and CMake have modules in their stable versions, did anything about modules usage get any better?

    • @codingwithmat
      @codingwithmat  9 หลายเดือนก่อน +2

      Not sure if the usage itself changed, after all, C++ 20 modules are a "standard" so the question is whether or not they all support every part of the modules standard!
      What has changed, of course, is the way we use modules in CMake and the support in GCC. I believe you no longer have to use all the "experimental" flags and functionality we set in this video on newer versions of CMake. There's an article on Kitware released earlier this month about it.
      I need to release a new video with these updates and a sample usage project, just haven't got around to it yet. Thank you for watching!

    • @adamodimattia
      @adamodimattia 3 หลายเดือนก่อน

      Cmake should be gone completely! Modules should have eliminated the need for cmake in my opinion