Skip to content

YexuanXiao/convert-cpp-std-headers-to-std-module

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

To quickly convert C++ headers into a module, follow the method below:

// lib.hpp
#ifndef LIB_HPP
#define LIB_HPP
#include <system_headers>
#include <cppstd_headers>

#include "impl1.h"
#include "impl2.h"

namespace lib
{
  struct foo {};
  void bar() {};
}
#endif
// lib_module.cppm
module;
#include "lib.hpp"
export module lib;

export namespace lib
{
  using lib::foo;
  using lib::bar;
}

However, this is not ideal because the std module is not used. C++ standard library headers are often the biggest factor affecting compilation speed, so if we could remove the includes and use import std; instead, that would be optimal.

At present, the solution to achieve this is:

// lib_module.cppm
module;

#include <system_headers>
#include <cassert> // NB
#include "clear_all_cpp_std_headers.h"

export module lib;
import std;

extern "C++"
{
#include "lib.hpp"
}
export namespace lib
{
  using lib::foo;
  using lib::bar;
}

Here are several articles introducing C++ modules that are well worth reading, listed in order from simpler to more advanced:

  • C++ Modules: en zh
  • C++ Modules In Depth: en zh
  • C++20 Modules: Practical Insights, Status and TODOs: en zh
  • C++20 Modules: Best Practices from a User's Perspective: en zh

About

A library for helping to convert #include standard library headers to import std;

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages