[Linux operating system] Problems encountered during compilation – why add -c? Executing the file prompts that the binary file cannot be executed? What is the function and understanding of main function parameters argc and *argv[]?

When using the GCC compiler for program development, we often encounter some problems in the compilation process, such as why the “-c” option is added, and why the generated executable file cannot be executed.
This blog will introduce these problems in detail, and give corresponding codes and explanations to help readers better understand the “-c” option in the GCC compilation process and the parameters argc and *argv[] of the main function.

Article directory

    • 1. Why add -c, -o? What is the relationship between the elements
      • 1.1 Explanation of each element of a complete compilation command
      • 1.2 Other options of gcc
    • 2. Why is the error “Unable to execute the binary file, the executable file format error”
      • 2.1 Common causes of errors
      • 2.2 The role of -c when compiling
    • 3. The function of the main function with two parameters argc and *argv[]
    • in conclusion:

1. Why add -c, -o? What relationship does each element represent?

1.1 Explanation of each element of a complete compilation command

We use an example to illustrate:

gcc -c hello.c -o hello

The meaning of each element in it:

  • "gcc": is the command line tool of the GNU Compiler Suite (GCC).
  • "-c": It is a compilation option of GCC, which means only compiling without linking. Using this option produces an object file (.o file) rather than an executable.
  • "hello.c": is the name of the source file to be compiled. Here “hello.c” is an example file name that can be replaced with the actual source file name.
  • "-o": is an option of GCC, which is used to specify the name of the output file.
  • "hello": is the name of the output file. Here “hello” is an example filename that can be replaced with the actual output filename.

Taken together, the meaning of the command “gcc -c hello.c -o hello” is:

Use the GCC compiler to compile the source file named “hello.c”, generate an object file, and name the object file “hello”.
Note that this command only compiles and does not link, so the generated file is not an executable file, but an object file. If you want to generate an executable file, you can remove the "-c" option, that is, use the command "gcc hello.c -o hello". This is why your program is clearly correct, but reports an error when it runs.
GCC (GNU Compiler Collection) is a set of compiler tools developed by GNU for compiling C, C++, Objective-C, Fortran, Ada and other programming languages.

From this we can learn

g

c

c

Other options:

\color{red}{According to this we can learn other options of gcc:}

According to this, we can learn other options of gcc:

1.2 gcc other options

  1. -c: Only compile source files, generate object files, and do not link.
  2. -o : Specifies the name of the output file.
  3. -g: Generate debug information in the object file for debugging.
  4. -O: Specifies the optimization level. For example, -O0 means no optimization, -O1 means basic optimization, and -O2 means higher level optimization.
  5. -Wall: Display all warning messages.
  6. -Werror: Treat all warnings as errors.
  7. -I : Specifies the search path for header files.
  8. -L : Specifies the search path for library files.
  9. -l : The library file to use when linking.
  10. -D : Define macros.
  11. -E: Only preprocess and generate preprocessed source code.
  12. -S: Only compile and generate assembly code.
  13. -shared: Generate shared library files.
  14. -static: Produce statically linked executables.
  15. -pthread: Link multithreading library.

2. Why is the error “Unable to execute the binary file, the executable file format error”

2.1 Common causes of errors

There may be several reasons:

\color{red}{There are several possible reasons for this:}

There may be several reasons:

  • Missing executable permissions: use the "chmod + x " command to add executable permissions to the file.
  • The "-o" option was not added to specify the output file name when compiling: Make sure to use the "-o" option and specify the output file when compiling name.
  • The "-c" option was not added when compiling: If only compiling but not linking is performed, the generated object file cannot be directly executed. You need to add the "-c" option when compiling to generate only the object file, and then perform the link operation to generate an executable file.

Generally, the -c parameter is used when compiling, resulting in no link and execution, so why do you need to bring the -c option?

2.2 Compile with -c function

The GCC compiler provides the "-c" option for compiling without linking. The advantage of this is that it can improve compilation speed and flexibility. In large projects, when we only make changes to some of the source files, only those source files need to be recompiled without relinking the entire project. Here is a sample code:

// main.c
#include <stdio.h>

int add(int a, int b) {<!-- -->
    return a + b;
}

int main() {<!-- -->
    int result = add(10, 20);
    printf("Result: %d\\
", result);
    return 0;
}

Suppose we modify the add function, we only need to recompile the main.c file:

gcc -c main.c

This produces an object file called main.o which we can then link with other object files to produce the final executable.

3. The function of the main function with two parameters argc and *argv[]

In C language, the main function is the entry function of the program, which can accept the parameters passed by the command line. The main function has two parameters: argc and *argv[].

  • argc(argument count) is an integer representing the number of command line arguments. It includes the name of the program itself as the first argument.
  • *argv[](argument vector) is a pointer to an array of pointers, where each pointer points to a string of command-line arguments. argv[0] points to the name of the program, argv[1] points to the first command-line argument, and so on.

By using these two parameters, we can obtain and process the parameters passed by the command line in the program. Here is a sample code:

// main.c
#include <stdio.h>

int main(int argc, char *argv[]) {<!-- -->
    printf("Number of arguments: %d\\
", argc);
    printf("Program name: %s\\
", argv[0]);
    
    for (int i = 1; i < argc; i ++ ) {<!-- -->
        printf("Argument %d: %s\\
", i, argv[i]);
    }
    
    return 0;
}

Suppose we execute the following command on the command line:

./main arg1 arg2 arg3

The output will be:

Number of arguments: 4
Program name: ./main
Argument 1: arg1
Argument 2: arg2
Argument 3: arg3

By using argc and *argv[], we can perform different operations according to different command line parameters, thus realizing the flexibility of the program.

Conclusion:

This blog introduces in detail the "-c" option in the GCC compilation process and the parameters argc and * of the main function The role and understanding of argv[]. By adding the "-c" option, we can only compile without linking, improving compilation speed and flexibility. At the same time, we also learned about the functions of main function parameters argc and *argv[], and how to obtain and process command line transfers in the program parameters.