Remember the gcc compilation command

Simple compilation process:
gcc
-c compiles, assembles, but does not link. Generate *.o file
-o output into file

gcc main.c -o main //main is an executable file
    //gcc mian.c can also generate an executable file, but it is a.out; -o can be renamed
    

-I The first directory to look for header files Specify the directory -->/usr/include-->/usr/local/include
-L The first directory to look for library files Specify the directory -->/usr/include--> /usr/local/include
-l means to look for library files in the above lib path. ### (-L and -l generally appear in pairs)

gcc -o hello hello.c -I /home/hello/include -L /home/hello/lib -lworld


?????????????????????????
Why is there an undefined reference to 'xxxxx' error?
First of all, this is a link error, not a compilation error. That is to say, if there is only this error, it means that there is nothing wrong with your program source code itself. It is because you used the wrong parameters when compiling with the compiler, and you did not specify the library to be used by the link program. For example, if some math functions are used in your program, then you need to
specify in the compilation parameters that the program should be linked with the math library. The method is to add -lm to the compilation command line.


#################################################################################################################################### #######
-shared gcc is used when compiling dynamic libraries
-shared test.c -o libtest.so

Compile to a static library
gcc -c main.c //generate main.o
ar -crv libfunc.a func.o //generate a static library libfunc.a (Note that lib is the prefix, the static library is suffixed with .a, and the middle part is func For the name of the library)
 
call the dynamic and static library
gcc -o test test.c -L/usr/lib -ltest -lfunc

Guess you like

Origin blog.csdn.net/lr94V587/article/details/125225373
Recommended