Conditional compilation depending on 32-bit or 64-bit architecture

Discussion in 'C++' started by Gowtham, Apr 14, 2008.

  1. Gowtham

    Gowtham Guest

    Hi, I am trying to write some code which acts differently when
    compiled on
    32 bit and 64 bit machines. To identify the machine type, I am trying
    to find
    the sizeof( int ) and comparing it with 32 and 64.

    But, the compiler is complaining about syntax errors in the #if lines.

    Errors:
    32-64.cpp:7:13: missing binary operator before '('
    32-64.cpp:12:13: missing binary operator before '('

    g++ version: 3.2.3

    sizeof() is evaluated at compile time, and hence I am not able to
    understand
    why this code is not working:

    code:

    #include <iostream>

    using namespace std;

    int main( int argc, char *argv[] )
    {
    #if ( sizeof( int ) == 32 )
    cout << "32 bit" << endl;
    return 0;
    #endif

    #if ( sizeof( int ) == 64 )
    cout << "64 bit" << endl;
    return 0;
    #endif

    cout << "Neither 32 bit nor 64 bit" << endl;
    }

    Can somebody please tell me what I have missed?

    Thanks
    Gowtham
    Gowtham, Apr 14, 2008
    #1
    1. Advertising

  2. Gowtham

    Gowtham Guest

    Re: Conditional compilation depending on 32-bit or 64-bitarchitecture

    On Apr 14, 3:05 pm, Gowtham <> wrote:
    > Hi, I am trying to write some code which acts differently when
    > compiled on
    > 32 bit and 64 bit machines. To identify the machine type, I am trying
    > to find
    > the sizeof( int ) and comparing it with 32 and 64.
    >
    > But, the compiler is complaining about syntax errors in the #if lines.
    >
    > Errors:
    > 32-64.cpp:7:13: missing binary operator before '('
    > 32-64.cpp:12:13: missing binary operator before '('
    >
    > g++ version: 3.2.3
    >
    > sizeof() is evaluated at compile time, and hence I am not able to
    > understand
    > why this code is not working:
    >
    > code:
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > int main( int argc, char *argv[] )
    > {
    > #if ( sizeof( int ) == 32 )
    > cout << "32 bit" << endl;
    > return 0;
    > #endif
    >
    > #if ( sizeof( int ) == 64 )
    > cout << "64 bit" << endl;
    > return 0;
    > #endif
    >
    > cout << "Neither 32 bit nor 64 bit" << endl;
    >
    > }
    >
    > Can somebody please tell me what I have missed?
    >
    > Thanks
    > Gowtham


    Oops, I should have compared sizeof(int) with 4 and 8.
    What is the best way of achieving this?
    Gowtham, Apr 14, 2008
    #2
    1. Advertising

  3. Sam a écrit :
    > Gowtham writes:
    >
    >> On Apr 14, 3:05 pm, Gowtham <> wrote:
    >>> Hi, I am trying to write some code which acts differently when
    >>> compiled on
    >>> 32 bit and 64 bit machines. To identify the machine type, I am trying
    >>> to find
    >>> the sizeof( int ) and comparing it with 32 and 64.
    >>>
    >>> But, the compiler is complaining about syntax errors in the #if lines.
    >>>
    >>> Errors:
    >>> 32-64.cpp:7:13: missing binary operator before '('
    >>> 32-64.cpp:12:13: missing binary operator before '('
    >>>
    >>> g++ version: 3.2.3
    >>>
    >>> sizeof() is evaluated at compile time, and hence I am not able to
    >>> understand
    >>> why this code is not working:

    [snuip]
    >>> #if ( sizeof( int ) == 32 )

    [snip]
    >>
    >> Oops, I should have compared sizeof(int) with 4 and 8.
    >> What is the best way of achieving this?

    >
    > The sizeof() operator gets evaluated at compile time, not in the
    > preprocessor phase, as such it cannot be used in preprocessor directives.
    >
    > Check the documentation for your compiler or C library. It's fairly
    > likely that there are some preprocessor macros #define-ed somewhere,
    > that give you the target platform's bitness.
    >


    For a more portable solution, you might look into the <climit> header.
    Something like:

    #if INT_MAX > ((int)(0x7FFFFFFF))
    //>32 bits version - verify 64 bits
    #if INT_MAX == ((int)(0x7FFFFFFFFFFFFFFF))
    //64 bits
    #define SIZEOF_INT 8
    #else
    //SIZEOF_INT == 0 means not 4 or 8
    #define SIZEOF_INT 0
    #elif INT_MAX == (int)(0x7FFFFFFF))
    #define SIZEOF_INT 4
    #else
    //SIZEOF_INT == 0 means not 4 or 8
    #define SIZEOF_INT 0
    #endif

    #if SIZEOF_INT == 8
    cout << "64 bit" << endl;
    #elif SIZEOF_INT == 4
    cout << "32 bit" << endl;
    #else
    cout << "Neither 32 bit nor 64 bit" << endl;
    #endif

    If you want to make the decision at compile time, with the actual
    sizeof(int) call, you could use templates.

    template<int SZ_INT>
    void foo()
    {
    cout << "Neither 32 bit nor 64 bit" << endl;
    }

    template<>
    void foo<4>()
    {
    cout << "32 bit" << endl;
    }

    template<>
    void foo<8>()
    {
    cout << "64 bit" << endl;
    }


    int main()
    {
    foo<sizeof(int)>();
    return 0;
    }


    Michael
    Michael DOUBEZ, Apr 14, 2008
    #3
  4. Gowtham

    Guest

    Re: Conditional compilation depending on 32-bit or 64-bitarchitecture

    Michael,

    The version with templates is more elegant, robust and easy to
    support.
    , Apr 14, 2008
    #4
  5. Gowtham

    Gowtham Guest

    Re: Conditional compilation depending on 32-bit or 64-bitarchitecture

    On Apr 14, 4:03 pm, Sam <> wrote:
    > Gowtham writes:
    > > On Apr 14, 3:05 pm, Gowtham <> wrote:
    > >> Hi, I am trying to write some code which acts differently when
    > >> compiled on
    > >> 32 bit and 64 bit machines. To identify the machine type, I am trying
    > >> to find
    > >> the sizeof( int ) and comparing it with 32 and 64.

    >
    > >> But, the compiler is complaining about syntax errors in the #if lines.

    >
    > >> Errors:
    > >> 32-64.cpp:7:13: missing binary operator before '('
    > >> 32-64.cpp:12:13: missing binary operator before '('

    >
    > >> g++ version: 3.2.3

    >
    > >> sizeof() is evaluated at compile time, and hence I am not able to
    > >> understand
    > >> why this code is not working:

    >
    > >> code:

    >
    > >> #include <iostream>

    >
    > >> using namespace std;

    >
    > >> int main( int argc, char *argv[] )
    > >> {
    > >> #if ( sizeof( int ) == 32 )
    > >> cout << "32 bit" << endl;
    > >> return 0;
    > >> #endif

    >
    > >> #if ( sizeof( int ) == 64 )
    > >> cout << "64 bit" << endl;
    > >> return 0;
    > >> #endif

    >
    > >> cout << "Neither 32 bit nor 64 bit" << endl;

    >
    > >> }

    >
    > >> Can somebody please tell me what I have missed?

    >
    > >> Thanks
    > >> Gowtham

    >
    > > Oops, I should have compared sizeof(int) with 4 and 8.
    > > What is the best way of achieving this?

    >
    > The sizeof() operator gets evaluated at compile time, not in the
    > preprocessor phase, as such it cannot be used in preprocessor directives.
    >
    > Check the documentation for your compiler or C library. It's fairly likely
    > that there are some preprocessor macros #define-ed somewhere, that give you
    > the target platform's bitness.
    >
    > application_pgp-signature_part
    > 1KDownload


    Thanks. There is an identifier __WORDSIZE defined in gcc environment.
    This will have
    values 32 and 64.

    I could handle this in my code. Thanks again.

    Wondering if this is truly portable?
    Gowtham, Apr 14, 2008
    #5
  6. On 2008-04-14 12:05, Gowtham wrote:
    > Hi, I am trying to write some code which acts differently when
    > compiled on
    > 32 bit and 64 bit machines. To identify the machine type, I am trying
    > to find
    > the sizeof( int ) and comparing it with 32 and 64.
    >
    > But, the compiler is complaining about syntax errors in the #if lines.
    >
    > Errors:
    > 32-64.cpp:7:13: missing binary operator before '('
    > 32-64.cpp:12:13: missing binary operator before '('
    >
    > g++ version: 3.2.3
    >
    > sizeof() is evaluated at compile time, and hence I am not able to
    > understand
    > why this code is not working:
    >
    > code:
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > int main( int argc, char *argv[] )
    > {
    > #if ( sizeof( int ) == 32 )
    > cout << "32 bit" << endl;
    > return 0;
    > #endif
    >
    > #if ( sizeof( int ) == 64 )
    > cout << "64 bit" << endl;
    > return 0;
    > #endif
    >
    > cout << "Neither 32 bit nor 64 bit" << endl;
    > }
    >
    > Can somebody please tell me what I have missed?


    In addition to what others have said I would like to point out that
    relying on sizeof(int) is doomed to fail. On many 64-bit platforms the
    int is still 32-bits.

    --
    Erik Wikström
    Erik Wikström, Apr 14, 2008
    #6
  7. Gowtham

    Ian Collins Guest

    Michael DOUBEZ wrote:
    >
    > int main()
    > {
    > foo<sizeof(int)>();
    > return 0;
    > }
    >

    sizeof(void*) would be a better choice. In the two most popular 64 bit
    memory models (LP64 and LLP64), sizeof(int) is 4.

    --
    Ian Collins.
    Ian Collins, Apr 15, 2008
    #7
  8. Ian Collins a écrit :
    > Michael DOUBEZ wrote:
    >> int main()
    >> {
    >> foo<sizeof(int)>();
    >> return 0;
    >> }
    >>

    > sizeof(void*) would be a better choice. In the two most popular 64 bit
    > memory models (LP64 and LLP64), sizeof(int) is 4.
    >


    True.
    Which might be a limit of the <climits>/INT_MAX solution.

    IIRC 'long' is not guaranteed to be large enough to hold a pointer so
    the template solution of the compiler defines are the only solution i
    could think of.

    Michael
    Michael DOUBEZ, Apr 15, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Muhammad Khan
    Replies:
    4
    Views:
    1,222
    Mike Treseler
    Jul 10, 2003
  2. bfische
    Replies:
    6
    Views:
    31,573
    jimmy
    Sep 18, 2008
  3. Pallav singh
    Replies:
    0
    Views:
    343
    Pallav singh
    Apr 1, 2009
  4. Pallav singh
    Replies:
    5
    Views:
    394
    Jorgen Grahn
    Apr 3, 2009
  5. Yohan N. Leder
    Replies:
    9
    Views:
    158
    John Bokma
    Aug 16, 2006
Loading...

Share This Page