Skip to content

Instantly share code, notes, and snippets.

@sftblw
Last active June 7, 2017 14:44
Show Gist options
  • Select an option

  • Save sftblw/266969f4a5f359b346ee2a4400447f2b to your computer and use it in GitHub Desktop.

Select an option

Save sftblw/266969f4a5f359b346ee2a4400447f2b to your computer and use it in GitHub Desktop.
PACK() macro for packing structures
#ifndef _PACK_H_
#define _PACK_H_
// 자료구조 packing을 위해 컴파일러별 macro 사용
// 컴파일러 판별 매크로: https://sourceforge.net/p/predef/wiki/Compilers/
// #pragma pack(): https://msdn.microsoft.com/ko-kr/library/2e70t5y1.aspx
// PACK 매크로: http://stackoverflow.com/a/3312896/4394750
#if defined(NO_PACK)
#define PACK( __Declaration__ ) __Declaration__
#elif defined(_MSC_VER)
#define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop) )
#elif defined(__GNUC__)
#define PACK( __Declaration__ ) __Declaration__ __attribute__((__packed__))
#else
#define PACK( __Declaration__ ) __Declaration__
#warning ("Packing of this compiler is not implemented. see: pack.h")
#endif
// struct와 typedef를 나눠서 선언해야 함. 그렇지 않으면 GCC에서 에러.
// s/o 답변이 있는데 주소는 모름.
// PACK( struct _MyStruct {
// char a;
// int b;
// } ) ;
// typedef _MyStruct MyStruct;
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment