Walt Mankowski on 26 Apr 2004 21:16:02 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] #define for 32 vs 64 bit C program?


On Mon, Apr 26, 2004 at 05:07:31PM -0400, Michael C. Toren wrote:
> On Mon, Apr 26, 2004 at 04:43:31PM -0400, Walt Mankowski wrote:
> > I have a C app that was developed on a 32 bit Linux box, and I'm
> > trying to get it to compile cleanly on a 64 bit opteron.  Does anyone
> > know of a gcc preprocessor directive that will tell whether a source
> > file is being compiled for 32 or 64 bits?
> 
> [..]
> 
> > But I'm pretty sure that "__32_BITS__" isn't the right directive.
> > Does anyone know what the recommended way to do this is?
> 
> Looking through /usr/include on a Debian system, it appears that if you
> include <bits/wordsize.h> you'll have access to __WORDSIZE.  I've never
> had a need to use it, though, and I have no idea how portable it is.

Yeah, I'm thinking I'm just going to go with __WORDSIZE.  In fact, if
I hadn't misspelled it as "__WORDSIZE__" I probably wouldn't have even
sent the original message. :)

I don't know how portable it is, either, but fortunately it doesn't
occur in all that many places.  I'd rather code around them than
litter my code with #if's.

For the ones I can't eliminate, there is what I'll likely change them
to:

#include <stdio.h>

int main () {
        long i = 42;
#if __WORDSIZE == 64
        printf("i = %ld\n", i);
#else
        printf("i = %d\n", i);
#endif
        return 0;
}

Thanks for your help.

Walt

Attachment: signature.asc
Description: Digital signature