tags:

views:

88

answers:

4

I am new to this 32 bit vs 64 bit thing. I have written programme in C++ in linux. I am wondering what determine the programme is 32 bit or 64 bit? This is because I compile the programme from a makefile written by others.

How could I check it and how could I modify it to 64 bit?

Thanks.

A: 

Change the compilation option for gcc (I think is -m64 instead of -m32) where the compilation parameters are defined.

You should also check that the external dependencies (libraries) are also 64 bit or at least are still usable from your 64 bit code.

Cătălin Pitiș
A: 

64 bit means that there are 64 bits (instead of 32) used to represent an integer. Since memory is accessed using integers, this means that (since your integer can be larger), you can index more memory.

The easiest way to compile a program for 64 bit is to compile it on a 64 bit computer, but compilers also have flags (as mentioned in the other answer), but they can be problematic (if your dependencies aren't 64 bit as well).

SubSevn
64-bit refers to the size of a pointer, not the size of an integer. Although many compiler makers do choose to use the same size for integers and pointers, they are not required to do so and in fact, many other compilers choose not to do so.
Mr Fooz
So in a 64 bit environment, compilers might choose to use a 32 bit pointer to access memory?
SubSevn
@Mr Fooz: Not true. Probably the best way to classify a processor's "bitness" is by the width of the data bus. e.g. 8-bit 6502: 8 bit data bus, 16 bit address bus. 16 bit 68000: 16 bit data, 24 bit address. 16 bit 8086: 16 bit data, 20 bit address.
JeremyP
@SubSevn: no, as Mr Fooz explained, pointers must be 64-bit (assuming "flat", non-segmented memory, which is the standard nowadays). 64-bit-producing GCC on my Linux system uses 64-bit pointers, 32-bit int, 64-bit long.
larsmans
I was joking with my first comment but I appreciate JeremyP's and larsmans' comments; I learned something.
SubSevn
+5  A: 

To check if the program is 64 bit, you can compile it and run

file <name-of-the-binary>

Example:

~> file /bin/ls
/bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

So /bin/ls is 32 bit on my system.

Whether the code is compiled for 32 bits or 64 bits depends on your environment and your compiler settings. To compile 64 bit programs, you need a 64 bit kernel and a 64 bit "userland" -- in particular a 64 bit version of libc6 and the compiler libraries. Usually, your compiler will just choose the appropriate mode for your environment.

Some Linux distributions offer "mixed" environments: a 64 bit kernel with both 32 bit and 64 bit libraries. If your environment is like this, your compiler might offer compiling both types of binaries. How to choose between them depends on your hardware platform and your compiler. For gcc on the x86-64 platform, the compiler switches would be -m32 and -m64 -- just have a look at the gcc man page.

Sven Marnach
For the record, you don't need a 64 bit kernel to compile 64 bit binaries. That's only necessary if you want to run the compiled program.
Magnus Hoff
@Magnus Hoff: For the record you don't need a 64 bit kernel to run 64 bit applications. Well, OK, you do on Linux but that is a restriction of the design of Linux. By contrast the 32 bit XNU kernel (the Mac OS X kernel) is quite capable of running 64 bit userland applications.
JeremyP
@Magnus Hoff: Of course you are right. Considering the first sentence of the OP, I tried to keep things simple. And usually, you want to run a program after compiling.
Sven Marnach
@Sven Marnach: not always, you might want to cross-compile and transfer it to another machine before running.
larsmans
@larsmans: That's why I wrote "usually". The next corner case to be pointed out here probably is that you also do not need 64 bit libraries for compiling 64 bit object files -- you only need them for linking! But I'm quite sure that's not what the OP is interested in :)
Sven Marnach
+3  A: 

You can check if the produced executable is 32 or 64 bits with the file command.

Then, as already answered, the -m64 and -m32 options can be used to instruct the compiler. You'll need to have all the dependencies (libraries) available.

philippe