C++20 Modules

Handling Third-Party Libraries

How do you handle third-party libraries that do not use C++20 modules?

Abstract art representing computer programming

Handling third-party libraries that do not use C++20 modules involves a few strategies to ensure compatibility and maintainability. Here's how you can integrate these libraries into your C++20 project effectively.

Using Traditional #include Directives

The simplest approach is to continue using traditional #include directives for third-party libraries. This method requires no modification to the libraries and ensures they work as intended. Here’s an example:

#include <iostream>

// Traditional include
#include "third_party_library.h"

int main() {
  // Function from third-party library

Creating Header Units

You can convert third-party headers into header units using the import statement. This allows you to gradually transition to modules while still using existing headers. Here’s an example:

// third_party_library.h
#pragma once
void third_party_function();
// main.cpp
// Creating a header unit
import "third_party_library.h";

int main() {

Wrapping Third-Party Libraries in Modules

Another approach is to create a module that wraps the third-party library. This module can expose the necessary functionality while hiding the implementation details. Here’s an example:

// third_party_wrapper.cppm
export module ThirdPartyWrapper;
#include "third_party_library.h"

export void wrapped_function() {
// main.cpp
import ThirdPartyWrapper;

int main() {

Using Global Module Fragment

For headers that need to be included before the module declaration, use the global module fragment. This ensures preprocessor directives and macros are handled correctly. For example:

module; // Global module fragment
#include "third_party_library.h"

export module MyModule;
import <iostream>;

export void myFunction() {

Managing Dependencies

When integrating third-party libraries, ensure your build system correctly handles the dependencies. Use package managers or build systems like CMake to manage and configure the inclusion of these libraries.

Example: CMake Integration

Here's an example of how you might configure a CMake project to use a third-party library with modules.

cmake_minimum_required(VERSION 3.20)

# Add third-party library
  third_party_library STATIC third_party_library.cpp

# Create module
add_library(MyModule STATIC my_module.cppm)
  MyModule PRIVATE third_party_library

# Set target properties
set_target_properties(MyModule PROPERTIES

# Main executable
add_executable(MyApp main.cpp)
target_link_libraries(MyApp PRIVATE MyModule)


Integrating third-party libraries that do not use C++20 modules can be done using traditional #include directives, creating header units, or wrapping them in modules.

Each method has its benefits and can be chosen based on the specific needs and constraints of your project.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved