It's not about ARM only, it's about majority of architectures being currently in use - the exceptions are x86, PowerPC in big-endian mode, some newer ARMs, and pretty much nothing else.Solar wrote:I admit I never worked on the ARM, so I am ignorant of its alignment requirements.
Using compiler features vs "the hard way"
Re: Using compiler features vs "the hard way"
Re: Using compiler features vs "the hard way"
__attribute__((packed)) is supported by all mainstream compilers and is a de facto standard.The problem is that there's no portable way to do that, (even with defines thanks to pragma pack).
Re: Using compiler features vs "the hard way"
Ah?JamesM wrote:__attribute__((packed)) is supported by all mainstream compilers and is a de facto standard.
Which compilers other than GCC support that syntax? None that I know of...
Every good solution is obvious once you've found it.
Re: Using compiler features vs "the hard way"
Clang, the ARM Compiler, IIR...Solar wrote:Ah?JamesM wrote:__attribute__((packed)) is supported by all mainstream compilers and is a de facto standard.
Which compilers other than GCC support that syntax? None that I know of...
Re: Using compiler features vs "the hard way"
I could make the same claim for "#pragma pack(1)", which is supported by GCC and MSVC alike. Does that make it a "de facto standard" too?
Every good solution is obvious once you've found it.
- Brynet-Inc
- Member
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
Re: Using compiler features vs "the hard way"
PCC, Clang/LLVM..Solar wrote:Ah?JamesM wrote:__attribute__((packed)) is supported by all mainstream compilers and is a de facto standard.
Which compilers other than GCC support that syntax? None that I know of...
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: Using compiler features vs "the hard way"
Standard conforming:
Not portable, however (though it is posisble to define alignas in terms of MSVC __declspec(aligned) and GCC __attribute__((aligned)).) For C++, "alignas" is a native operator.
Code: Select all
#include <stdalign.h>
struct MyPackedStruct alignas(char)
{
int firstMember alignas(char);
short secondMember alignas(char);
....
};
Last edited by Owen on Thu Mar 15, 2012 9:26 am, edited 1 time in total.
Re: Using compiler features vs "the hard way"
OK, I admit that I heard of those compilers but didn't use them.
So, let's see... I couldn't find good docs on the Altera IIR, so I'll drop that one.
__attribute__((packed)) is supported by GCC, Clang, LLVM, xlC, and PCC.
#pragma pack(1) is supported by GCC, Clang, LLVM, xlC, PCC, and MSVC.
So much for "de facto standards".
So, let's see... I couldn't find good docs on the Altera IIR, so I'll drop that one.
__attribute__((packed)) is supported by GCC, Clang, LLVM, xlC, and PCC.
#pragma pack(1) is supported by GCC, Clang, LLVM, xlC, PCC, and MSVC.
So much for "de facto standards".
Every good solution is obvious once you've found it.
Re: Using compiler features vs "the hard way"
LLVM is not a C compiler - if anything, it's the compiler of its own assembly language. Therefore it doesn't support any C constructs, let alone non-standard ones.Solar wrote:__attribute__((packed)) is supported by [...] LLVM
#pragma pack(1) is supported by [...] LLVM
Sure, there are compilers that use LLVM as their backend, but you mentioned both of them already: llvm-gcc is simply GCC with the added ability of emitting LLVM IR and using LLVM pipeline (it doesn't change the frontend), and Clang is the LLVM's native compiler.
So what do you mean by "LLVM"?
Re: Using compiler features vs "the hard way"
After some googling I suspect that #pragma pack() is way more compatible across compilers. GCC, Turbo C, MSVC, Watcom and lots of others has support for it. It's much more a de facto standard than __attribute__((packed)). Since I had a hunch it would be supported on anything but GCC I was actually surprised to see this text in the GCC manual: "For compatibility with Microsoft Windows compilers, GCC supports a set of #pragma directives...". Who would have thought.
Re: Using compiler features vs "the hard way"
I am confused about #pragma pack().
Does it affect the compiler settings for each file, or just for the file where it was found?
Edit: I found this, according to which only the struct declaration following the pragma is affected.
Does it affect the compiler settings for each file, or just for the file where it was found?
Edit: I found this, according to which only the struct declaration following the pragma is affected.
Re: Using compiler features vs "the hard way"
Nononono...
As far as there can be a "specification" for anything following "#pragma" (which, by definition, is implementation-defined), "#pragma pack(1)" affects all structures following the directive in the current translation unit.
Check out the relevant part of the GCC docs. Especially the part about "#pragma pack()" resetting the alignment to whatever it was when translation began should clear things up.
As far as there can be a "specification" for anything following "#pragma" (which, by definition, is implementation-defined), "#pragma pack(1)" affects all structures following the directive in the current translation unit.
Check out the relevant part of the GCC docs. Especially the part about "#pragma pack()" resetting the alignment to whatever it was when translation began should clear things up.
Every good solution is obvious once you've found it.
Re: Using compiler features vs "the hard way"
Yep, and GCC doesn't really support #pragma push/pop, which would help here. In general I favour __attribute__ over #pragmas. Even though __attribute__ is a GNU extension, all compilers implement it for compatibility and dont complain about it in strict modes. #pragmas just scream nonportable to me.Solar wrote:Nononono...
As far as there can be a "specification" for anything following "#pragma" (which, by definition, is implementation-defined), "#pragma pack(1)" affects all structures following the directive in the current translation unit.
Check out the relevant part of the GCC docs. Especially the part about "#pragma pack()" resetting the alignment to whatever it was when translation began should clear things up.
You're being pretty pedantic here. Clang is the C language frontend for the LLVM compiler infrastructure and falls under the LLVM project umbrella.Sure, there are compilers that use LLVM as their backend, but you mentioned both of them already: llvm-gcc is simply GCC with the added ability of emitting LLVM IR and using LLVM pipeline (it doesn't change the frontend), and Clang is the LLVM's native compiler.
So what do you mean by "LLVM"?
Re: Using compiler features vs "the hard way"
Uh... the link I gave says it does?JamesM wrote:GCC doesn't really support #pragma push/pop, which would help here.
...with the exception of MSVC...Even though __attribute__ is a GNU extension, all compilers implement it...
No more or less than __attribute((pack))__.#pragmas just scream nonportable to me.
But I will shut up now. I don't have strong feelings one way or the other, I just try to drill a hole into GNU things wherever I can.
Every good solution is obvious once you've found it.
Re: Using compiler features vs "the hard way"
Yes, I know I'm pedantic and I know Clang is their own frontend. Still, Solar mentioned both Clang and LLVM as if they were distinct compilers. This can be pretty confusing to people who are not familiar with LLVM project family.JamesM wrote:You're being pretty pedantic here. Clang is the C language frontend for the LLVM compiler infrastructure and falls under the LLVM project umbrella.