0

I have a C file:

#include <stdint.h>

typedef struct {
  uint32_t m1;
  uint16_t m2;
} TStruct;

TStruct s = {
  1,
  2
};

How to compile this file with gcc to produce little endian binary file?

0x01 0x00 0x00 0x00 0x02 0x00
gaminn
  • 13
  • 3
  • compile it for an arch that uses little endian... and you also have to take care of alignment. – LPs Jul 06 '16 at 15:34

3 Answers3

0

Though, Little-Endian is the default for all standard configurations. A otherwise configured compiler toolkit can use this switch to fetch desired format.

    gcc file.c -mlittle-endian

There you go..

Amit Kumar
  • 59
  • 1
  • 8
0

I guess tying together the other answers. On a little endian machine (x86):

evaitl@evbb ~/se $ cat foo.c 
#include <stdint.h>

typedef struct {
  uint32_t m1;
  uint16_t m2;
} TStruct;

TStruct s = {
  1,
  2
};
evaitl@evbb ~/se $ gcc -c foo.c 
evaitl@evbb ~/se $ objcopy -O binary foo.o  foo
evaitl@evbb ~/se $ hexdump -C foo
00000000  01 00 00 00 02 00 00 00                           |........|
00000008
evaitl
  • 1,365
  • 8
  • 16
-1

Probably

objcopy -O binary

is what you are looking for.See [ this ] SO answer.

Community
  • 1
  • 1
Sergio
  • 8,099
  • 2
  • 26
  • 52