Thread: pragma pack in a preprocessing directive

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    16

    pragma pack in a preprocessing directive

    I want to put #pragma pack(n) as a preprocessor define.
    eg

    Code:
    #define PACK_ON(n)                  _Pragma("pack(n)")
    PACK_ON(2)
    I get the compile warning:
    Code:
    warning: unknown action 'n' for '#pragma pack' - ignored
    I've also tried this
    Code:
    DO_PRAGMA(x)                  _Pragma(#x)
    #define PACK_ON(n)                  DO_PRAGMA(pack(n))
    PACK_ON(2)
    but get error:
    Code:
    _Pragma takes a parenthesized string literal


    How do I get around this ?
    Last edited by adetheheat; 12-23-2019 at 11:08 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe try with concatenation to form the parenthesized string literal:
    Code:
    #define PACK_ON(n) _Pragma("pack(" #n ")")
    Or maybe it is necessary to be explicit about the concatenation:
    Code:
    #define PACK_ON(n) _Pragma("pack(" ## #n ## ")")
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    16
    Thanks for your help:
    Code:
    #define PACK_ON(n) _Pragma("pack(" #n ")")
    gave error: _Pragma takes a parenthesized string literal
    and
    Code:
    #define PACK_ON(n) _Pragma("pack(" ## #n ## ")")
    gave error: pasting ""pack("" and ""2"" does not give a valid preprocessing token,
    So still no luck

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like this?
    Code:
    #include <stdio.h>
    
    #define str(s) #s
    #define PACK(x) _Pragma(str(pack(x)))
    
    PACK(2)
    struct foo {
        char a;
        short b;
        int c;
    };
    
    
    int main()
    {
        printf("%zd\n",sizeof(struct foo));
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    16
    great that worked ! many thanks

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    I couldn't solve this one, either. But once you see the solution, it's "obvious". Macros are weird.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #pragma directive
    By Usha in forum C Programming
    Replies: 1
    Last Post: 07-13-2014, 12:57 AM
  2. When should I use pragma pack?
    By kuchiku in forum C Programming
    Replies: 2
    Last Post: 05-23-2011, 01:19 PM
  3. #pragma directive
    By roaan in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 04:17 AM
  4. #pragma pack(1) workaround?
    By l3iggs in forum C Programming
    Replies: 5
    Last Post: 06-02-2008, 11:17 AM

Tags for this Thread