Alex Gartrell
Alex Gartrell

Reputation: 2564

How can I figure out which architecture I'm compiling for in GCC?

I want to write code that compiles easily for either 32 or 64 bit linux under gcc. I guess I'm looking for something like

#ifdef IA32
subl $0x4, %esp
#endif

#ifdef X86_64
subl $0x4, %rsp
#endif

Upvotes: 1

Views: 163

Answers (2)

Stephen Canon
Stephen Canon

Reputation: 106317

I believe the following should work:

#if defined __i386__
subl $0x4, %esp
#elif defined __x86_64__
subq $0x4, %rsp
#else
#error Unknown architecture!
#endif

Fixed the suffix on sub in the 64-bit code for you, too =)

Upvotes: 1

Rom
Rom

Reputation: 4199

Most likely what you're looking for is _LP64, which is defined when pointer size is 64-bit wide

Upvotes: 2

Related Questions