Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making 'long' 4 bytes in gcc on a 64-bit Linux machine

I am working on porting an application to 64-bit on Linux platform. The application is currently supported on Linux, Windows, Mac 32-bit and Windows 64-bit. One of the issues we are frequently encountering is the usage of long for int and vice versa. This wasn't a problem till now since long and int are interchangeable (both are 4 bytes) in the platforms the application is currently supported on. The codebase being a huge one, with lots of legacy code with #defines for many data types, makes it cumbersome to search all usage of long and replace appropriately with int.

  1. As a short term solution, is there a way to make GCC use 4 bytes instead of 8 for 'long'?
  2. If it has, what are issues that we might face? If not, is there an easier way to fix the long and int problem?
like image 289
Chethan Ravindranath Avatar asked Oct 09 '12 07:10

Chethan Ravindranath


People also ask

How do I compile 64-bit gcc?

Just type the following command on Linux terminal. Command: gcc -v Output Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper Target: x86_64-linux-gnu ...................... ......................

What is the size of an int data type in gcc 64 bits ENV?

int is 32 bits in size. long , ptr , and off_t are all 64 bits (8 bytes) in size.

What is the size of a long in gcc?

With GCC, long integers are normally 32 bits long and long long integers are 64 bits long, but it varies with the computer hardware and implementation of GCC, so check your system's documentation. These integer types differ in the size of the integer they can hold and the amount of storage required for them.

Is a long 4 or 8 bytes in C?

The size of the “int” integer type is 4 bytes and the size of the “long long” integer type is 8 bytes for all the above combinations of operating system, architecture and compiler.


2 Answers

-m32 generates 32-bit code.

-mx32 generates 64-bit code but uses 32-bit longs and pointers.

Intel 386 and AMD x86-64 Options

like image 131
Windows programmer Avatar answered Oct 13 '22 18:10

Windows programmer


  1. No. On Linux x86_64 the ABI specifies that long is a 8 byte type (LP64). In fact, most if not all 64-bit Unix systems (including 64-bit OS X, AFAIK) are LP64 so this is nothing specific to Linux.

  2. Beyond fixing your code, no.

If you need a portable integer type which is large enough to store a pointer value, use intptr_t or uintptr_t (but usually wanting to store a pointer value into an integer means that you're doing something wrong, so think twice!). For an integer type which is capable of representing the difference between two pointers, use ptrdiff_t. For sizes of objects, use size_t.

like image 26
janneb Avatar answered Oct 13 '22 18:10

janneb