I’m writing a tiny kernel with c++11 and have two instances with the same type which have to be constructed before any other static objects are created.
The code I wrote is as follows:
// test.hpp
class test {
// blahblah...
};
// test.cpp
typedef char fake_inst[sizeof(test)] __attribute__((aligned(alignof(test))));
fake_inst inst1;
fake_inst inst2;
// main.cpp
extern test inst1;
extern test inst2;
int kmain() {
// copy data section
// initialize bss section
new (&inst1) test();
new (&inst2) test();
// call constructors in .init_array
// kernel stuffs
}
It builds and works as expected without no warning messages, but not with LTO.
I get tons of warning messages complaining the type matching and I wonder if there’s a workaround since it confuses me to find the other ‘real’ warning or error messages.
Any suggestion?
Perhaps like this?
Within
main, you can then saynew (&inst1) test;. It should not give warnings about type inconsistency violations anymore now, because unlike in your code, this code does not contain variables that have different types in different files.