Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#Pragma pack giving warning when compiling

I'm creating the following structures in C and only the Packet structure will be sent over a UDP socket. I want to pack the structure to avoid different alignments on different computers (Not sure if I need to pack both or only the one I'm sending). When I compile this, I get the following warning for both:

warning: ignoring #pragma ( [-Wunknown-pragmas] #pragma(pack);`

Why is it that my pragmas are being ignored? And how can this be fixed.

#pragma push(pack, 1);
struct packet{ // the actual packet within the node
    uint32_t seqnum;
    uint32_t checkSum;
    uint32_t numPackets; // number of packets to send
    char data[1024]; // the data in the packet
};
struct packetNode{ //for the linked list
    struct packet p;
    struct packetNode *next;
};
#pragma pop(pack)

gcc version:

gcc --version 
gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
like image 427
coder4lyf Avatar asked Feb 12 '26 16:02

coder4lyf


1 Answers

Your push and pack syntax is backwards compared to what the GCC manual says it supports. The semicolon after #pragma is superfluous.

#include <inttypes.h>

#pragma pack(push, 1)
struct packet{ // the actual packet within the node
    uint32_t seqnum;
    uint32_t checkSum;
    uint32_t numPackets; // number of packets to send
    char data[1024]; // the data in the packet
};
struct packetNode{ //for the linked list
    struct packet p;
    struct packetNode *next;
};
#pragma pack(pop)

Although the link is to the GCC 5.2.0 documentation, it is the same if you change the 5.2.0 in the URL to 4.8.4.

Incidentally, for the Packet data structure shown, the direction to pack the structure is superfluous on any plausible machine architecture. There are plenty of structures where that would not be the case (indeed, the PacketNode structure is one such, though I see no benefit to packing it as it is not sent over the wire), but the Packet structure will be identical in layout in both packed and unpacked forms.

like image 77
Jonathan Leffler Avatar answered Feb 14 '26 07:02

Jonathan Leffler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!