I ran into an error in our codebase when mixing compilers. I have simplified the error to the reproducible example that follows.
The following code produces incompatible structures when compiled with MSVC vs GCC/clang for Windows x64 target.
#include <iostream>
#include <xmmintrin.h>
typedef unsigned char u8;
struct __attribute__ ((ms_struct)) A
{
u8 u8_1;
int i_1;
__m128 vector_1;
protected:
bool b_1;
};
struct B : public A
{
void* pv_1;
};
struct C
{
__m128 vector_1;
};
static_assert(std::is_pod<C>(), "__m128 is NOT POD");
static_assert(!std::is_pod<A>(), "A is POD");
int main()
{
std::cout << "Size of A: " << sizeof(A) << "n";
std::cout << "Size of B: " << sizeof(B) << "n";
return 0;
}
The MSVC program has this output:
Size of A: 48
Size of B: 64
While GCC/clang programs have the following output:
Size of A: 48
Size of B: 48
According to LLVM/clang documentation, they aim to be ABI compatible with MSVC on Windows but this doesn’t appear to be the case. Is this a bug in LLVM/clang?
Also when I compile with GCC, even using the ms_struct attribute just gives me this error:
error : ms_struct may not produce Microsoft-compatible layouts for classes with base classes or virtual functions [-Wincompatible-ms-struct]
GCC/clang both seem to ignore ms_struct for the layout of struct B
.
Is there a way to make GCC/clang and MSVC generate the same layout for these structures?