ipaddress 1.1.0
Loading...
Searching...
No Matches
Cpp Module (C++20 and newer)

This library can be integrated as a C++ module. C++ modules, introduced in the C++20 standard, not only expedite the compilation process but also make the use of C++ more natural.

To leverage these benefits, ensure you have the right toolset, which includes a modern compiler, a compatible build system, and CMake version 3.28 or later (or 3.26 with extensions).

Using the Module

By default, the target assembly for the C++ module is not enabled. To activate it, you need to set the -DIPADDRESS_BUILD_MODULE=ON flag in your configuration parameters or explicitly specify it in your CMake file.

If your compiler is compatible with the C++20 standard or newer, you can import the ipaddress module as shown below (there are many restrictions for using C++ modules, read about them in the next section):

cmake_minimum_required(VERSION 3.28.0)
project(my-project LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 23) # or: set(CMAKE_CXX_STANDARD 20)
option(IPADDRESS_BUILD_MODULE "Build Cpp module" ON)
add_subdirectory(third-party/ipaddress)
add_executable(my-project main.cpp)
target_link_libraries(my-project PRIVATE ipaddress::ipaddress-module) # add the module to your target
set_target_properties(my-project PROPERTIES CXX_SCAN_FOR_MODULES ON)
set_target_properties(my-project PROPERTIES CXX_EXTENSIONS OFF)

After which you can import the module in your source files:

#include <iostream>
import ipaddress;
int main() {
auto ip = ipaddress::ipv4_address::parse<"127.0.0.1">();
std::cout << ip << std::endl;
return 0;
}
static consteval inline ip_address_base< ipv4_address_base > parse() noexcept
Static method template to parse an IP address from a fixed string at compile time.
Definition ip-address-base.hpp:123
Note
Currently, the ipaddress::ipaddress-module is not available via package managers such as Vcpkg or Conan. This is primarily due to the absence of established best practices for integrating C++ modules into package distributions.

Restrictions on the use of modules

Considerations for the use of C++ modules include compiler support and build system requirements. The following compilers are known to support C++ modules:

  • Clang 16 or later;
  • MSVC 17.4 or later;
  • GCC 14 or later.

CMake version 3.28 or higher is required, although version 3.26 may suffice if extensions are enabled. Currently, CMake offers support for C++ modules with these specific generators:

  • Ninja and Ninja Multi-Config;
  • Visual Studio.

Additionally, there are notable considerations:

  • CMake is presently capable of exporting targets with C++ modules for subsequent imports, but only with the Ninja and Ninja Multi-Config generators;
  • On Windows, certain issues have been observed when utilizing modules with Clang;
  • It is generally acknowledged that most editors lack comprehensive support for modules. Consequently, when using such editors, functionalities like IntelliSense may not perform reliably.
Note
In essence, it is crucial to recognize that the ecosystem for C++ modules and build systems is in a state of ongoing development. The structuring for their support is only beginning to take shape. Furthermore, the best practices for integrating these modules into distributable packages managed by package managers are still being formulating.
Warning
The current landscape of C++ modules presents several challenges. It's crucial to assess their suitability for your project. If your product requires compilation across a broad range of platforms and toolsets, including older versions, modules might not be the optimal choice. Conversely, if your product is tailored for a specific platform and you have a consistent toolset, utilizing C++ modules, including those from the standard library, could greatly enhance compilation efficiency.

Additional links

For more insights into integrating modules in your C++ projects, consider exploring these articles: