Skip to content

A tool to easily generate module library from any conventional C++ header library.

License

Unknown, GPL-3.0 licenses found

Licenses found

Unknown
LICENSE
GPL-3.0
COPYING
Notifications You must be signed in to change notification settings

RichardLuo0/cxx-module-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cxx-module-generator

A tool to easily convert traditional C++ header library to module library.

It converts a header file like

// lib.hpp
namespace A {
namespace B {
class C {
  int a, b;
};
}  // namespace B
}  // namespace A

into

// lib.cppm
module;
#include "lib.hpp"
export module lib;
namespace A {
namespace B {
export using A::B::C;
}
}

Then you can use clang++ -march=native -std=c++20 --precompile -c lib.cppm -o lib.pcm to convert the cppm into pcm.
After that use clang++ -march=native -std=c++20 -c lib.pcm to generate .obj file (contains only module initializer), link it with your obj files to create an executable. Finally, you can use the library as a C++20 module:

import lib;

int main() {
  A::B::C c;
  return 0;
}

By using modules, the build time can be reduced dramatically.

Notice

This tool is still in early development and doesn't always work.

  • Symbols with internal linkage can not be exported. However, a workaround is to include them in a header file. This can be enabled with --internal-linkage-as-header option.

Usage

For example, to convert boost/json.hpp, the module name is boost.json, use cmg boost/json.hpp --name=boost.json --namespace=boost::json --. Do not forget the -- at the end of command. It means ignore any compile_commands.json in current directory (which most probably you do not have one).

Most options are the same as any other clang tools. Use cmg --help for more help.

Use find path/in/subtree -name '*.hpp' | xargs cmg --namespace=a::b -- if you need to run on multiple files.

Install

Clang installation is mandatory

  • Download the file from release section
  • Put it next to clang executable (for msys2, it should be msys2/clang64/bin)

Tested library

  • boost.json
  • boost.program_options
  • boost.process: std_out and std_err have internal linkage. no matching function for call to boost::vector_detail::value_at_impl

About

A tool to easily generate module library from any conventional C++ header library.

Resources

License

Unknown, GPL-3.0 licenses found

Licenses found

Unknown
LICENSE
GPL-3.0
COPYING

Stars

Watchers

Forks

Languages