[cfe-users] Clang Sizeof give diff value for Microsoft and Linux

Vivek Pandey via cfe-users cfe-users at lists.llvm.org
Wed Feb 17 00:08:12 PST 2021

I think Clang, on/for windows, should give a compile time flag/option that can be used to control it (A flag when set make compile-time operator like sizeoff to behave like MSVC or non-MSVC)  As in the current form it is breaking building of cross-platform application.

With C++11 onward many things are incorporated in standard so that one code base can be used across platform. Now if compiler is blocking the flow then it seems moving back to pre C++11.

From: David Blaikie <dblaikie at gmail.com>
Sent: Friday, January 29, 2021 3:23 AM
To: Vivek Pandey <vivek.pandey at tallysolutions.com>
Cc: cfe-users at lists.llvm.org; Manu Agarwal <manu.agarwal at tallysolutions.com>; Tuhin Sengupta <tuhin.sengupta at tallysolutions.com>
Subject: Re: [cfe-users] Clang Sizeof give diff value for Microsoft and Linux

Clang on Windows is designed to be compatible with MSVC - which has different layout requirements than the Itanium ABI/GCC on Linux. I don't think there's a way to use the same ABI on both platforms - especially not if you are interacting with any code compiled by another compiler on both platforms (existing/foregin C++ precompiled libraries).

On Thu, Jan 28, 2021 at 1:45 PM Vivek Pandey via cfe-users <cfe-users at lists.llvm.org<mailto:cfe-users at lists.llvm.org>> wrote:
Hi Team,

We are using Clang 11 for our product that has common C++ code base for Windows, Linux, Macintosh, ….

We observed that sizeof operator gives different value on Windows and Linux/OSX, when the inheritance is from a common base class:

Example Sample:

#include <cassert>

struct Base {}; // empty class

struct Derived1 : Base {
    int i;

struct Derived2 : Base {
    Base c; // Base, occupies 1 byte, followed by padding for i
    int i;

struct Derived3 : Base {
    Derived1 c; // Derived1 is too derived from same Base class
    int i;

int main()
    assert<https://ind01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fen.cppreference.com%2Fw%2Fcpp%2Ferror%2Fassert&data=04%7C01%7Cvivek.pandey%40tallysolutions.com%7C1a2156b9e1d146b7360608d8c3d71ae2%7C66bcd9b727254893bb969ae424774af6%7C0%7C0%7C637474675935486094%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000&sdata=NYeOWjC%2B0xhCxaFWPvJTpcN9xvz8uCZDV5gAKEan8J0%3D&reserved=0>(sizeof(Derived2) == 2*sizeof(int));

    assert<https://ind01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fen.cppreference.com%2Fw%2Fcpp%2Ferror%2Fassert&data=04%7C01%7Cvivek.pandey%40tallysolutions.com%7C1a2156b9e1d146b7360608d8c3d71ae2%7C66bcd9b727254893bb969ae424774af6%7C0%7C0%7C637474675935496090%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000&sdata=ymboJjAMIu8lm26ZdA7L%2FArWNyxIUUu96c89R5f4oR8%3D&reserved=0>(sizeof(Derived3) == 3*sizeof(int));

              When we compile above program using Clang 11 and run it on windows and Linux  sizeof(Derived3) give different value.

We don’t want a work-around via making first data member of derived class of type different from class that also is derived from same empty base class.
Is there any flag that we can use so that it gives same result on all platform (Windows/Linux/OSX/Android/iOS/….)
Or another way to solve this gracefully.

Thank you!

Best Regards,

cfe-users mailing list
cfe-users at lists.llvm.org<mailto:cfe-users at lists.llvm.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20210217/0e5977d0/attachment-0001.html>

More information about the cfe-users mailing list