element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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
  • About Us
  • 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 1196 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
Parents
  • Jan Cumps
    Jan Cumps 2 months 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
Comment
  • Jan Cumps
    Jan Cumps 2 months 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
Children
No Data
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