Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling programme linux 32 bit vs 64 bit

Tags:

c++

linux

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.

like image 652
Leslieg Avatar asked Dec 28 '22 05:12

Leslieg


1 Answers

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.

like image 174
Sven Marnach Avatar answered Jan 13 '23 10:01

Sven Marnach