0

I am trying to change the structure packing to 1 byte. I want to do it using a C compiler flag in makefile that should take care for all the structures in the code by defining them to be 1 byte aligned.

I have tried this, and I can do the thing using this in code

#if (PRAGMA_PACK)
#pragma pack (1)
#endif
typedef PACKED struct _stsomefn
{

}stsomefn;

PRAGMA_PACK is set in makefile using

CDEFS += -DPRAGMA_PACK=1

This has worked, of course it will. The problem here is I do not want to change this code. So that is why I am asking a MACRO which will be defined in makefile to do the same thing.

Ishmeet
  • 1,540
  • 4
  • 17
  • 34
  • Please clarify what you want, just saying you want a macro is too vague. Where do you want to use the macro? Why does it have to be a macro, not an attribute, or command-line flag? Explain what you want to achieve, not how you think you need to do it. – Jonathan Wakely Sep 23 '13 at 08:42
  • Do you need this macro to switch the packing in & out at build time? – Joe Sep 23 '13 at 08:58
  • @joe Yes if it possible – Ishmeet Sep 23 '13 at 09:20
  • @JonathanWakely I have updated the question, I am not sure I am asking the right thing, but I think I have explained it clearly now – Ishmeet Sep 23 '13 at 09:21
  • 1
    And not for all structs, just some specific structs? This seems pretty dodgy, so maybe you could give us the context of your problem? – Joe Sep 23 '13 at 09:28
  • Then you don't want a macro. You can't have a macro that magically affects source code without changing the source code. The macro definitely can't know which structs you want to be affected and which you want to be unaffected, just think about it. – Jonathan Wakely Sep 23 '13 at 09:35

3 Answers3

4

With GCC, the way to make a structure packed is by adding the GCC-specific __attribute__ ((packed)) variable attribute.

If you want this as a macro, just #define a suitable one:

#define PACKED __attribute__ ((packed))

Then use it:

struct my_packed_struct {
  int x;
  char y;
  float z;
} PACKED;

See also this question for more details about the packed attribute.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
2

You can easily issue pragmas from macros with the _Pragma keyword of C99, gcc implements this without problems.

#define PACK1 _Pragma("pack(1)")
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
2

I want to do it using a MACRO in makefile that should take care for all the structures in the code.

I don't know what this means, how would a macro in a makefile affect your code? You'd need to use the macro in the actual code, so you'd need to change the code, which you say you don't want to do.

With GCC you can control packing via:

  • #pragma pack(1) in the source code
  • __attribute__((packed)) on struct and variable definitions
  • -fpack-struct on the command-line

The last one is suitable for use in makefiles, without changing the code.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • Ok! I will try -fpack-struct in Makefile, will this convert all my structures to 1 bytes aligned or some functions? – Ishmeet Sep 23 '13 at 10:53
  • Yes, all structures, which means you need to recompile all libraries, as it alters the ABI of the compiler. It's probably a bad idea (but then IMHO packing structs is almost always a bad idea anyway, let the compiler do it naturally.) – Jonathan Wakely Sep 23 '13 at 11:51