简体   繁体   中英

Compiling programme linux 32 bit vs 64 bit

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.

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.

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.

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM