How to compile c++20 module header unit with clang++
Asked Answered
S

1

1

When compiling this simple program: (with clang++-11 -fmodules main.cpp -o main.o -std=c++2a)

// main.cpp
import "pch1.h";

int main() {
    std::cout << "Hello" << std::endl;
}

I get the following error message:

main.cpp:3:8: error: header file "pch1.h" (aka './pch1.h') cannot be imported because it is not known to be a header unit

Obviously I need to do something with the header file before using it, but what flags does clang expect?

The question is twofold:

  • What flags do clang want to compile/precompile the header unit
  • What flags do clang want to feed the header unit into main.cpp
Shock answered 11/3, 2021 at 7:8 Comment(0)
B
0

After spending many hours digging in CLang's code and options I didn't find a way how to create and use header units.

But I found a fully compatible way of importing headers as modules. I describe it here, and here is description of STD headers case.

So according to linked answers import <iostream>; is totally equivalent to doing import std_mod;. In both cases all definitions are exported and all macros are preserved. Also in both cases you don't need any modifications at all to your (or std) headers.

Bidarka answered 30/4, 2021 at 8:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.