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.
#include
DirectivesThe 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
third_party_function();
}
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() {
third_party_function();
}
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() {
third_party_function();
}
// main.cpp
import ThirdPartyWrapper;
int main() {
wrapped_function();
}
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() {
third_party_function();
}
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.
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)
project(MyProject)
# Add third-party library
add_library(
third_party_library STATIC third_party_library.cpp
)
# Create module
add_library(MyModule STATIC my_module.cppm)
target_link_libraries(
MyModule PRIVATE third_party_library
)
# Set target properties
set_target_properties(MyModule PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)
# 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 detailed overview of C++20 modules - the modern alternative to #include
directives. We cover import
and export
statements, partitions, submodules, how to integrate modules with legacy code, and more.