Software & AppsOperating SystemLinux

How To Fix “cannot execute binary file: Exec format error” on Ubuntu 18.04 x86-64 Target

Ubuntu 18

In this article, we will delve into the error message “cannot execute binary file: Exec format error” that you might encounter on Ubuntu 18.04 x86-64 Target. We’ll take a close look at why it occurs, and how to fix it.

Quick Answer

To fix the "cannot execute binary file: Exec format error" on Ubuntu 18.04 x86-64 Target, you need to ensure that you compile your code into an executable file and that the file has the necessary permissions. This error typically occurs when you try to execute a non-executable file. By following the steps outlined in this article, you can resolve this issue and successfully run your executable file.

Understanding the Error

The “cannot execute binary file: Exec format error” typically appears when you attempt to execute a file that is not in a valid executable format. This could be due to a variety of reasons, such as the file being an object code file instead of an executable file, or the file being compiled for a different architecture.

Identifying the Problem

Let’s consider a scenario where you have a C program file test1.c and you compile it using the following command:

gcc -g -Wall -c test1.c -o test1

Here, gcc is the GNU Compiler Collection, -g is used to generate debug information to be used with GDB, -Wall enables all compiler’s warning messages, -c tells gcc to stop after the compilation stage and produce an object file, and -o specifies the output file.

After running the above command, when you try to execute the output file test1 using ./test1, you encounter the “Exec format error” message.

The Solution

The error occurs because the -c option in the command tells gcc to create an object file, which is not executable. To resolve this issue, you need to compile your code into an executable file.

Remove the -c option from the command:

gcc -g -Wall test1.c -o test1

This command will compile test1.c into an executable file named test1. Now, when you try to execute ./test1, it should run without the “Exec format error” message.

Checking File Permissions

Before you can execute a file in Unix-like systems, you need to ensure that it has execute permissions. You can check the permissions of a file using the ls -la command.

For example,

ls -la test1

The output will show the permissions of the file test1 in the format -rwxrwxrwx. Here, r stands for read, w for write, and x for execute. The first set of rwx is for the owner, the second for the group, and the third for others.

If your file does not have execute permissions, you can add them using the chmod command:

chmod +x test1

Conclusion

In conclusion, the “cannot execute binary file: Exec format error” message in Ubuntu 18.04 x86-64 Target is usually due to trying to execute a non-executable file. By ensuring that you compile your code into an executable file and that the file has the necessary permissions, you can avoid this error.

Remember, it’s always important to understand the commands and options you’re using in your terminal. If you’re unsure about a command or option, you can always check the man pages (e.g., man gcc) for more information.

For more detailed information about the gcc compiler, you can visit the official GCC documentation. For more information about file permissions in Unix-like systems, you can check out this comprehensive guide on file permissions.

What is the difference between an object file and an executable file?

An object file is generated by the compiler and contains machine code and data that is not yet in the final executable format. It needs to be linked with other object files and libraries to create an executable file. An executable file, on the other hand, is a file that can be directly executed by the operating system.

How do I check the architecture of a binary file?

You can use the file command followed by the path to the binary file to check its architecture. For example, file test1 will display information about the architecture of the test1 file.

Can I execute a binary file compiled for a different architecture?

No, you cannot execute a binary file compiled for a different architecture. The binary file is specific to the architecture it was compiled for and will not run on a different architecture.

How can I change the file permissions of a file?

You can use the chmod command followed by the desired permissions and the file name. For example, chmod +x test1 will add execute permissions to the file test1. The +x option adds execute permissions, +r adds read permissions, and +w adds write permissions.

What should I do if the file still doesn’t execute after granting execute permissions?

If the file still doesn’t execute after granting execute permissions, it’s possible that the file is not compatible with your system’s architecture. Double-check that you have compiled the file for the correct architecture and ensure that any dependencies are properly installed. You can also try running the file with the strace command to get more information about any errors that may be occurring.

Leave a Comment

Your email address will not be published. Required fields are marked *