How to solve the "undefined reference to `xxx`" error that occurs during compilation

When compiling, I occasionally encounter the error "undefined reference to `xxx_function'". How to solve this problem?

How to deal with it?

First of all, we need to know that when such a phenomenon occurs, it is always in the link stage of compilation. When such a compilation error occurs and is printed, you can see the message "ld returned 1 exit status". At this time, you can know that a link is A compilation error caused by the corresponding library not being found when using the function.

So, how do we solve problems like this?

Since compilation means that a certain function is not defined, let's first see which library implements this function. Directly search the include directory of the compilation environment to see which header file the xxx_function function is defined in, and then see which source file this function is implemented in and compiled into a library. Assume that the xxx_function function is implemented by xxx.c and is finally compiled and output to xxx.so. Then use it to readelf -d xxx.socheck the "Library soname:" information output by the command. For example, if "libxxx.so" is output, we then add -lxxx to the LDFLAGS of the Makefile. That's it. When compiling, the link tool will link to libxxx.so and find the symbolic link of the xxx_function function successfully.
Sometimes it is caused by not linking certain libraries of the system, and sometimes it is caused by not linking third-party libraries. Specifically, according to the problem, find the library that implements the function, and then link the library accordingly.

Why does such an error occur?

When we run a program on the machine, the program is composed of source code through four stages: precompilation , compilation , assembly , and linking .

Precompiled

Assume that ours is a .c source file and related header files, which will be precompiled into an .i file by the compiler gcc. Just use the -E parameter for precompilation. Precompilation can be simply understood as processing precompilation instructions starting with "#" in the source code, such as "#include", "#define", etc. When we cannot determine whether the macro definition is correct or whether the header file is included correctly, we can check the precompiled file to determine the problem.

compile

The compilation process is to perform a series of lexical analysis, syntax analysis, semantic analysis and optimization on the preprocessed files to generate corresponding assembly code files. Use the -S parameter to output to an assembly file.

compilation

The assembler converts assembly code into instructions that the machine can execute. Almost every assembly statement corresponds to a machine instruction. Just use the -c parameter, and the target file will be output after precompilation, compilation and assembly.

Link

When we look at the assembly code, we often see jmp aaa_function. We know that this is a jump instruction, which will jump to the aaa_function function for execution. aaa_function is a symbol at this time. When we compile, it will eventually be executable. Each symbol used in the program is linked, and you know where to find the symbol when running, and you know what the jump address is. The linking process mainly includes steps such as address and space allocation, symbol resolution and relocation.

Therefore, when the above "undefined reference to `xxx_function'" error occurs, it means that the definition of the xxx_function symbol is not found in the current symbol table, and the linker ld reports an error. We will need to pass the -l command to inform the linker that the link needs to be linked. Which libraries enable it to find the corresponding symbol definition.

at last

Many aspects above are not described clearly, and many steps are not well understood. I strongly recommend that friends in need read the book " Programmer's Self-cultivation------Links, Loading and Libraries ". This book is very detailed. It introduces the various operations of linking an executable program to loading and running. I believe that after reading it carefully, your skills will be greatly improved.

Guess you like

Origin blog.csdn.net/weixin_41944449/article/details/120586638