element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Blog modern C++ modules
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 2 Dec 2024 9:12 PM Date Created
  • Views 1090 views
  • Likes 7 likes
  • Comments 4 comments
  • Modern C++
  • c++23
  • c++20
Related
Recommended

modern C++ modules

Jan Cumps
Jan Cumps
2 Dec 2024

Since C++20, a new feature is available: modules.

We're all used to C++ header and source files. And we all know their pros and cons. Because I'm reviewing a new C++ alternative for them, I'll focus on the cons of header files:

  • header files are sensitive to the order they are included, and are usually included multiple times in a project. (usual mitigation: a guard #IFNDEF / #ENDIF construct in each header file).
  • changes usually require that a significant amount of the project needs recompilation
  • header files don't know the C++ language. They are handled by the precompiler (before C++ analysis starts)

Modules address these 3.

  • You don't include module headers in your code, but import modules. No matter how many times a module is imported, it's a single cost in the build phase. And the order of import in different source files does not matter.
  • changes in a module (either interface or implementation) doesn't proliferate throughout the whole build
  • modules are parsed by the compiler. They are fully aware (and can take full advantage) of the language and compiler.

Example project

image

The example is a simple module called matlib. It exports a function plus(). 

Example interface: matlib_interface.cpp

/*
 * matlib_interface.cpp
 *
 *  Created on: 2 dec. 2024
 *      Author: jancu
 */

module;

export module matlib;

export namespace matlib {

    int plus(int a, int b);

}

Example implementation: matlib_implementation.cpp

/*
 * matlib_implementation.cpp
 *
 *  Created on: 2 dec. 2024
 *      Author: jancu
 */


module;

module matlib;

namespace matlib {

    int plus(int a, int b) {
    	return a + b;
    }

}

Example of a program that uses the module: matlib_client.cpp

/*
 * matlib_main.cpp
 *
 *  Created on: 2 dec. 2024
 *      Author: jancu
 */

import matlib;

int main() {

    matlib::plus(1, 2);

    return 0;
}

Ready for production?

The language feature (and GCC's implementation) is currently a minimal viable product. It works, and starts offering the promised advantages. You enable them by flagging that you use C++20 or higher. And provide the -fmodules-ts flag.

image

What's holding me from using it in anger at the moment, is the limited IDE support (syntax highlighting, drill to declarations). And that it currently requires that the module files are compiled before the files that import the module. But it's interesting, and a preparation for more to come.

edit 20250208: I used it for an existing code base:  modern C++ modules: convert existing example from .h to module 

  • Sign in to reply
  • Jan Cumps
    Jan Cumps 1 month ago

    ARM and RISCV developers: the latest ARM GCC release 14.2, and the latest Pico RISC release RISCV_ZCB_RPI_2_1_1_3 have full support for this.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 9 months ago in reply to vmate

    I checked CLion IDE support. Looks like they have real good coverage. Code completion, syntx highlighting, support with finding modules...

    https://www.jetbrains.com/help/clion/support-for-c-20-modules.html

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 9 months ago in reply to vmate

    Are you sure about this? Header files aren't special in any way, we just call them that to signify their purpose. You can put your whole implementation in the header if you want to, which is quite common for simple libraries ("header only library").

    content in header files becomes C code once the preprocessor is done with it and includes it in a source file. (or if you pass it directly to the compiler, but then it's a source file with .h extension).

    They work great, and we have built constructs to work with some of the inherent effects of the pre-processor / includes approach (like putting the IFNDEF guides around the content).

    One of the places where you can see that headers are not source files, but inserted into source files, is that you can not define non-inline functions and variables in header files (source: https://build2.org/article/cxx-modules-misconceptions.xhtml#single-file).

    A post that reviews what's different between including a header and importing a module: https://medium.com/@guilhermeprogrammer/a-gentle-introduction-to-c-modules-29bbcf7ac86f

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • vmate
    vmate 9 months ago

    header files don't know the C++ language. They are handled by the precompiler (before C++ analysis starts) 

    Are you sure about this? Header files aren't special in any way, we just call them that to signify their purpose. You can put your whole implementation in the header if you want to, which is quite common for simple libraries ("header only library").

    Header inclusion order shouldn't be an issue either with properly written code.

    The advantages seem to be about ease of use(not having to write separate headers), compilation speed, and proper isolation of modules.

    Also, CLion seems to have proper support for C++20 features, including modules, so try that out.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube