Many of us aware of the fact that the 64 bit version of machine architecture will have the backward compatibility to lower versions. An application built for 32 bit version of Linux machine can run seamlessly on 64 bit version also, but the vice versa is not true. So, if we compile a C code on 64 bit version of Linux, it cannot be executed on the 32 bit version of Linux.
By default, GCC builds an application for the native machine architecture. So, on 64-bit machine, the GCC builds an application for 64-bit architecture. If we want an application built on a 64-bit machine to run on both 32-bit and 64-bit machines, we can achieve it using the special compilation flag of GCC.
The -m Flag:
GCC provides a special compilation flag "-m" to specify the machine architecture for which the application needs to be built.
To build for 32-bit version, specify the compilation flag as "-m32", whereas for 64-bit version, use the flag "-m64".
Note: To execute a 32-bit application on a 64-bit machine, we need to have the 32-bit version of dynamic libraries required by that application on that 64-bit machine; otherwise the application cannot be executed.
Examples:
test.c
#include <stdio.h>
int main()
{
printf ("Welcome\r\n");
return 0;
}
32-bit Compilation:
gcc -m32 -o 32bitExe test.c
This Command will compile the "test.c" for 32-bit machine architecture and creates the executable with the name "32bitExe".
Running the executable, will give the below output.
./32bitExe
Welcome
64-bit Compilation:
gcc -m64 -o 64bitExe test.c
This command will compile the code "test.c" for 64-bit machine architecture and creates the executable with the name "64bitExe".
Running the executable, will give the below output.
./64bitExe
Welcome
How to identify the executable is 32-bit or 64-bit version:
The linux command "file" can be used to identify whether an executable is of type 32-bit or 64-bit.
Syntax: file <executable-name>
Example:
We wanted to know whether the executable name "32bitExe" is a 32-bit or 64-bit executable.
file 32bitExe
32bitExe: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.0.0, BuildID[sha1]=0xd4f96ae7862c856df70e8aa9fe54aeb1b41992df, not stripped
As highlighted in bold above, the output of the file command clearly specifies that the executable "32bitExe" is a 32-bit version of executable.
Similarly, for the executable "64bitExe",
file 64bitExe
64bitExe: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.4.0, BuildID[sha1]=0x14a6ae7113cedf4945c25ad5b599227f85cb46ca, not stripped
Important Note on running 32-bit executable on 64-bit machine:
Sometimes, when we try to execute a 32-bit application on a 64-bit linux machine we may get an error like "No such file or directory". For example, executing the "32bitExe" on 64-bit machine, may throw the error as shown below.
./32bitExe
bash: ./32bitExe: No such file or directory
The reason is that, on 64-bit machine, the dynamic libraries required by the 32-bit executable could be missing. Verify for the shared object "/lib/i386-linux-gnu/ld-2.15.so" in the 64-bit machine. If it present, then verify that the symbolic link "/lib/ld-linux.so.2" is present to point to the shared object "/lib/i386-linux-gnu/ld-2.15.so". At the run time, this symbolic link will be used to invoke the shared objects.
ls -l /lib/ld-linux.so.2
lrwxrwxrwx 1 root root 30 Sep 25 14:47 /lib/ld-linux.so.2 -> /lib/i386-linux-gnu/ld-2.15.so
References:
https://www.cyberciti.biz/tips/compile-32bit-application-using-gcc-64-bit-linux.html
https://askubuntu.com/questions/227671/does-a-file-reported-as-not-a-dynamic-executable-by-ldd-depend-on-other-libr
https://ubuntuforums.org/showthread.php?t=924914
https://askubuntu.com/questions/133389/no-such-file-or-directory-but-the-file-exists
https://stackoverflow.com/questions/8659694/how-can-i-tell-whether-my-gcc-is-compiling-64bit-by-default
By default, GCC builds an application for the native machine architecture. So, on 64-bit machine, the GCC builds an application for 64-bit architecture. If we want an application built on a 64-bit machine to run on both 32-bit and 64-bit machines, we can achieve it using the special compilation flag of GCC.
The -m Flag:
GCC provides a special compilation flag "-m" to specify the machine architecture for which the application needs to be built.
To build for 32-bit version, specify the compilation flag as "-m32", whereas for 64-bit version, use the flag "-m64".
Note: To execute a 32-bit application on a 64-bit machine, we need to have the 32-bit version of dynamic libraries required by that application on that 64-bit machine; otherwise the application cannot be executed.
Examples:
test.c
#include <stdio.h>
int main()
{
printf ("Welcome\r\n");
return 0;
}
32-bit Compilation:
gcc -m32 -o 32bitExe test.c
This Command will compile the "test.c" for 32-bit machine architecture and creates the executable with the name "32bitExe".
Running the executable, will give the below output.
./32bitExe
Welcome
64-bit Compilation:
gcc -m64 -o 64bitExe test.c
This command will compile the code "test.c" for 64-bit machine architecture and creates the executable with the name "64bitExe".
Running the executable, will give the below output.
./64bitExe
Welcome
How to identify the executable is 32-bit or 64-bit version:
The linux command "file" can be used to identify whether an executable is of type 32-bit or 64-bit.
Syntax: file <executable-name>
Example:
We wanted to know whether the executable name "32bitExe" is a 32-bit or 64-bit executable.
file 32bitExe
32bitExe: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.0.0, BuildID[sha1]=0xd4f96ae7862c856df70e8aa9fe54aeb1b41992df, not stripped
As highlighted in bold above, the output of the file command clearly specifies that the executable "32bitExe" is a 32-bit version of executable.
Similarly, for the executable "64bitExe",
file 64bitExe
64bitExe: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.4.0, BuildID[sha1]=0x14a6ae7113cedf4945c25ad5b599227f85cb46ca, not stripped
Important Note on running 32-bit executable on 64-bit machine:
Sometimes, when we try to execute a 32-bit application on a 64-bit linux machine we may get an error like "No such file or directory". For example, executing the "32bitExe" on 64-bit machine, may throw the error as shown below.
./32bitExe
bash: ./32bitExe: No such file or directory
The reason is that, on 64-bit machine, the dynamic libraries required by the 32-bit executable could be missing. Verify for the shared object "/lib/i386-linux-gnu/ld-2.15.so" in the 64-bit machine. If it present, then verify that the symbolic link "/lib/ld-linux.so.2" is present to point to the shared object "/lib/i386-linux-gnu/ld-2.15.so". At the run time, this symbolic link will be used to invoke the shared objects.
ls -l /lib/ld-linux.so.2
lrwxrwxrwx 1 root root 30 Sep 25 14:47 /lib/ld-linux.so.2 -> /lib/i386-linux-gnu/ld-2.15.so
References:
https://www.cyberciti.biz/tips/compile-32bit-application-using-gcc-64-bit-linux.html
https://askubuntu.com/questions/227671/does-a-file-reported-as-not-a-dynamic-executable-by-ldd-depend-on-other-libr
https://ubuntuforums.org/showthread.php?t=924914
https://askubuntu.com/questions/133389/no-such-file-or-directory-but-the-file-exists
https://stackoverflow.com/questions/8659694/how-can-i-tell-whether-my-gcc-is-compiling-64bit-by-default
No comments:
Post a Comment