C++ Modules: Getting Started Today - Andreas Weis - CppCon 2023

Sdílet
Vložit
  • čas přidán 8. 06. 2024
  • cppcon.org/
    ---
    C++ Modules: Getting Started Today - Andreas Weis - CppCon 2023
    github.com/CppCon/CppCon2023
    Modules have been one of the most highly anticipated features of C++20. Unfortunately, it was also the language feature that took the longest to become widely available for developers to use. This year, for the first time, we see broad support for the feature in all major compilers and mainstream build system support through CMake. The goal of this talk is to provide you with all the basic knowledge to allow you getting started with C++20 modules today.
    We will take a look at how modules change the build process and why it took so long to implement them. We will take a tour of the essentials of the named modules mechanism and explore the new best practices for physical code structure in a modules-based code base, including how to set up a build with CMake. And last but not least, we will discuss different options for interacting with existing header-based code.
    The talk will focus above all else on practicality: We will only be covering features that are widely available for use today with the latest compilers and build tools. We will give special attention to the areas where the design practices for modules differ from the familiar header-based approach and address common misconceptions and pitfalls that are typical among developers first encountering the feature. No prior knowledge of modules is required.
    ---
    Andreas Weis
    Andreas Weis has been writing C++ code in many different domains, from real-time graphics, to distributed applications, to embedded systems. As a library writer by nature, he enjoys writing portable code and exposing complex functionalities through simple, richly-typed interfaces. Both of which C++ allows him to do extensively. Andreas is also one of the co-organizers of the Munich C++ User Group, which allows him to share this passion with others on a regular basis.
    He currently works for Woven by Toyota, where he focuses on building modern software for use in safety critical systems.
    ---
    Videos Filmed & Edited by Bash Films: www.BashFilms.com
    CZcams Channel Managed by Digital Medium Ltd: events.digital-medium.co.uk
    ---
    Registration for CppCon: cppcon.org/registration/
    #cppcon #cppprogramming #cpp
  • Věda a technologie

Komentáře • 60

  • @vesk4000
    @vesk4000 Před 6 měsíci +18

    Awesome! I've been looking for a good first talk on modules and in particular what I can do right now. The standard is very nice, but it seems a lot of module features haven't been implemented yet, which makes it quite annoying when I watch a lot of talks, they don't take that into account.

  • @davidsicilia5316
    @davidsicilia5316 Před 6 měsíci +7

    The best talk on modules thus far at CppCon

  • @ComicSansMS
    @ComicSansMS Před 5 měsíci +15

    A small error on the slides: The CMake FILE_SET for modules sources is called CXX_MODULES, not MODULES.
    Also, on slide #20 about interface partitions, the names of the module in the primary module interface and the partitions need to be consistent (either all 'm' or all 'mice', but not a mixture of the two).

  • @Zex-4729
    @Zex-4729 Před 5 měsíci +1

    Thanks for the talk! So far the best source of explanation for modules.

  • @lucasbraune
    @lucasbraune Před 5 měsíci

    Great talk!

  • @christiandaley120
    @christiandaley120 Před 3 měsíci

    Great talk, the include guard trick is genius!

  • @MathewBensonCodes
    @MathewBensonCodes Před 5 měsíci +1

    Happy to have learnt through this talk how to export templates. I was putting the export on the function "part" and couldn't figure out why it wasn't working.

  • @theo-dr2dz
    @theo-dr2dz Před 4 měsíci +1

    At this time (version 17.8.6) Visual Studio can with no problem have modules that wrap header files that include the C standard library. So, no problem if you write a module that uses a C library. That's already a big deal.
    It won't work if you include a header that includes the C++ standard library. So if you try to wrap a C++ library that won't work because the library will include stuff from the C++ standard library and you will get name clashes.
    So, the solution is to have an immediate wrapper module that just includes all standard library headers that are used by the wrapped library in the bit between module; and export module x;. and then exports the library. In most cases that will probably approach most of the C++ standard library. At least it's a trick that works (I verified with a minimal example). Hopefully this issue will be resolved and the trick will no longer be needed soon.

  • @zahash1045
    @zahash1045 Před 5 měsíci +13

    this reminds me of a helmet that i had that had a hammer attached to it. So everytime i want to nail something to the wall, i would wear that helmet and swing my head around to hit the nail.
    but sometimes i miss and i hit my head against the wall instead. So to solve that problem, i added some padding to my forehead.
    now everything works like a charm.

    • @kobe4460
      @kobe4460 Před 14 dny

      Another fellow jonathan blow watcher

  • @victoreijkhout7115
    @victoreijkhout7115 Před 5 měsíci +1

    Is 17:53 correct? My compiler thinks that the implementaiton module is trying to import itself. Oh wait, the code is correct but the compilation order is crucial, as was not mentioned. 21:29 should the "m:" twice be "mice:"?

    • @ComicSansMS
      @ComicSansMS Před 5 měsíci +4

      Yes, the 'm:' on slide 20 was a typo and should be 'mice:'. Thanks for catching that!

    • @abdulshabazz8597
      @abdulshabazz8597 Před 5 měsíci

      // File: my_module.p1.cpp
      export module mice:pinky;
      export void narf() {};
      // File: my_module.p2.cpp
      export module mice:the_brain;
      export void take_over_the_world() {};
      struct SecretMasterPlan {};
      .
      .

  • @Fetrovsky
    @Fetrovsky Před 3 měsíci +2

    In my_module_p{1,2}.cpp, you have "export module m:something;"... what is m? I don't see you declaring a module called m. Should it be mice instead?

    • @evgenii_g
      @evgenii_g Před 2 měsíci +1

      yep, should be `mice`, not `m`. It's a compile err with `m` otherwise

  • @marianwitek403
    @marianwitek403 Před 3 měsíci

    I'm using visual studio and start to learn modules. The thing is there is bad tutorial on the internet. I import std module to other module that is in project which compile to static library (.lib). In that project I'm only using print function from cpp23. The lib file is about 4mb of size when using imported std. If this is normal? All library contining cpp standard library is linked to my library? Second, if I will link my library with other project that make executable file (.exe) and in that project I'm using cpp standard library (import std) ofcourse I' have to use std.ifc interface again but do I have link c++ standard library (.lib) files again? This is very practical information..not only to show how "nice" is write "import std".

  • @olafschluter706
    @olafschluter706 Před 5 měsíci +2

    Aside of CMake (as of 3.28) modules support by toolchains is very lacking. Neither Apple's clang 15.0 nor gcc 13.2 provide a dependency graph for CMake. clang 17.0 works somewhat with CMake (i.e. it configures and builts everything), but then VS Code can't handle dependencies and code completion, neither with the C/C++ plugin nor the clangd plugin. So for the time being it seems we have to wait for the tools to adopt still. "Getting started today"? No way.

  • @kiddkai
    @kiddkai Před 5 měsíci +7

    It looks so much complex than the other languages module systems...

    • @GregWhiteley
      @GregWhiteley Před 5 měsíci +4

      Only if you compare it with single vendor programming languages - rust, swift,... - that mandate (and provide) the build system. For those of us in embedded and system programming world we can't rely on those languages. We need C/C++ because it integrates with our systems, not finding a way to get those single vendors to care enough to support our system.
      Complexity is the price we pay for flexibility, but in return we can ship product.

    • @jawad9757
      @jawad9757 Před 4 měsíci

      @@GregWhiteley you don't have to use cargo with rust

    • @_ironrose
      @_ironrose Před 3 měsíci

      c++ has to be complex i guess

    • @007LvB
      @007LvB Před měsícem

      @@_ironrose Unfortunately yes. But we must remember that C++ is the only language out there that does not force programmers to write their software in any specific way. All C++ does is give us a set of tools and some loose guidelines. Rust assumes programmers can't be trusted and so places as many restrictions as possible.
      C++ is getting better, sure, but due to legacy reasons it has a lot of "catching up" to do. Modules make C++ almost infinitely better.

    • @frenchmarty7446
      @frenchmarty7446 Před 18 dny

      Welcome to C++.

  • @tiagocerqueira9459
    @tiagocerqueira9459 Před 5 měsíci +68

    Only C++ can take a concept this simple and make it hard

    • @indiesigi7807
      @indiesigi7807 Před 5 měsíci +3

      Contrary to pop opinion the header system is extremely good and won't be replaced any time soon.

    • @wumi2419
      @wumi2419 Před 5 měsíci +4

      Does it mean only C++ has real world use then? Since that's one of properties of reality, it makes simple concepts hard by virtue of nuance.

    • @iliasalaur
      @iliasalaur Před 5 měsíci +5

      What is so hard about it?

    • @llothar68
      @llothar68 Před 5 měsíci

      @@indiesigi7807 I want to keep headers too, but make the classes open so i can extend them in private source files. That would be better and simpler.

    • @llothar68
      @llothar68 Před 5 měsíci

      @@iliasalaur They didn't try to work against the root of all this problems. Single translation units. A 1970s concept that is so wrong. We could have so much nice things if we could get of command line compilers and obj files, instead go with a server that get feed c++ source files and spits out executables. Thats the solution for the speed problem. The better abstraction could be solved by allowing open classes. Objective-C++ is doing it very well but only for the Objective-C.object model.

  • @abdulshabazz8597
    @abdulshabazz8597 Před 5 měsíci +3

    I don't like the fact imports are dumped into the current namespace. Perhaps if it was a bit more python-y in the way of : import B; B.my_import_func(); in lieu of a using declaration would provide a much cleaner namespace .

    • @tomleeyrc40
      @tomleeyrc40 Před 15 dny

      Namespaces help a little
      in foo.cpp:
      export module foo;
      namespace foo { export void fn(){} }
      in main.cpp
      import foo;
      int main() { foo::fn() }
      This interface only requires that all your modules have a namespace that is the same as the name it uses when calling export.

  • @sjswitzer1
    @sjswitzer1 Před 4 měsíci

    Shouldn’t CMake treat modules as libraries, or at least be able to do so? (Maybe it can and I missed it?)

    • @007LvB
      @007LvB Před měsícem

      Why would this be a good idea? A module is simply a translation unit. I think the approach of linking several module interface units together into a library is a much better solution. After all, a library may be formed from just a single TU.

  • @PaulMetalhero
    @PaulMetalhero Před 6 měsíci

    So .bmi files behave like small static libraries?

    • @szaszm_
      @szaszm_ Před 5 měsíci +5

      To me it sounds like the same thing as headers, but in binary format, for the compiler, so it's faster to parse and use.

    • @spaxio
      @spaxio Před 5 měsíci +5

      No, like small per-compiled headers

  • @LeeJordanMusic
    @LeeJordanMusic Před 5 měsíci

    Great talk, thanks! Some concepts here, like "export import", "export using", and the ability to export a function returning a non-exported type, make me a bit worried for the future of C++. A lot of people already get confused with headers, and this is IMO adding a whole lot of complexity. That doesn't matter for people who know what they're doing, but this just makes it harder to convince large teams / companies to use modern C++.

  • @Lalasoth
    @Lalasoth Před 5 měsíci +1

    Hmm you must be from the future.. I cannot find a visual studio release later than 17.8.3

    • @ComicSansMS
      @ComicSansMS Před 5 měsíci +2

      Sorry for being imprecise there. The 19.34 is the C++ toolchain version, the number you get when running cl.exe, not the version number of the Visual Studio IDE that you get in the About... window in Visual Studio.

    • @Lalasoth
      @Lalasoth Před 5 měsíci

      @@ComicSansMS thanks for clarifying that.

  • @007LvB
    @007LvB Před měsícem

    Exporting from module partitions is not a standard. GCC and Clang do not support it, and Clang developers have even said that it might never be supported! I would add a BIG NOTE at slide 20.

  • @lisinsignage
    @lisinsignage Před 5 měsíci

    Will C++ 2025 reinvent the concept of .def files fir export definitions 😂😂😂

  • @kl6995
    @kl6995 Před 4 měsíci +3

    It's too complicated. As a long term C++ dev, I really was hoping that C++ modules would ease my life and the one of my team. It's 2024 and I don't see the benefits of it so much: too verbose, error prone.. it's depressing because we had to set a new modern language in our stack because of my failure to predict what modules would end up like. My fault, but it kills me.

    • @007LvB
      @007LvB Před měsícem

      It will ease your life. At least I think so. Stick to module interface units for declarations and documentation, and module implementation units for definitions. You never need to worry about inclusion hell again. I would say that's a big win.

  • @Mempler
    @Mempler Před 3 měsíci

    3 years and still not properly usable. Good language 👍
    Gonna stick with C

    • @007LvB
      @007LvB Před měsícem

      Why on earth would you stick with C? That language is close to useless for practical purposes.

  • @KX36
    @KX36 Před 6 měsíci +20

    Modules were such a wasted opportunity to make something good, ended up ugly and overly verbose with so many rules to remember. Nobody will use, like coroutines.

    • @szaszm_
      @szaszm_ Před 5 měsíci +3

      Coroutines are already usable, although there are more libraries needed in the ecosystem. Asio already makes good use of them for async networking. Writing your own coroutine type is much harder, but most people don't need to do that.

    • @iliasalaur
      @iliasalaur Před 5 měsíci +2

      Coroutines is a very different story, because there is a very small number of scenarios, where they could be handy.
      Modules are exactly the opposite. This is the piece of standard, that must be used everywhere. I hope that in 2-3 years more compilers will add support for them, because you know, headers - suck

    • @matejplch7524
      @matejplch7524 Před 5 měsíci +3

      Coroutines are hard to implement, but easy to use. They are not meant to be implemented by everyone, only library implementers will need to do that, and then millions of C++ developers will be able to use them without knowing the implementation details.

    • @llothar68
      @llothar68 Před 5 měsíci +1

      @@iliasalaur I love headers. I love them much more then all the languages that mix the implementation with the declarations.The problem is not hte header but the single translation unit compilation speed (and some better implementation hidding). All of it could have been solved much better without this module system they came up with.

    • @KX36
      @KX36 Před 5 měsíci +1

      Coroutines have myriad possible uses. I have used coroutines in Unity for a variety of uses. Anywhere you have async code you can use coroutines and they have a simple intuitive syntax there. You shouldn't need a 3rd party library to use what was supposed to be a major feature of the C++20 standard library, but right now you do.
      C++20 modules tend to be discussed as a replacement for headers, but when you start using them you find that they don't fully replace headers. They seem to be more useful for joining together code at the library level, rather than at the file level. (And of course only your own new libraries because existing libraries probably don't support modules yet.) Within each library you'll probably still #include headers. I think this is hinted at around slide 29/50 when it talks about "regular files". You could use module partitions but partitions and modules in general are just a ton of header style boilerplate with no real benefit over traditional includes.

  • @GuillermoH77
    @GuillermoH77 Před 5 měsíci +2

    Besides complexity, nobody is using it because it is 2024… and still it is not production ready.
    IMHO, I think that standard body should deprecate this, and try again. Don’t force all C++ community to support yet another mistake.

    • @007LvB
      @007LvB Před měsícem

      Useless perspective! Modules were introduced with C++20, and the standard library is modularized with C++23. It takes time to do this right. The modules proposals have been underway since 2012, don't kid yourself thinking this is easy or simple!