其实与 Makefile 打交道已经很多年了,然而一直都只是在别人搭好框架的基础上修改,近来看 github 上的源码越来越多,导致对 Makefile 和 cmake 的理解要求有提升,于是乎再回来看看 Makefile。
本篇记录基本的编译:二进制、静态库、动态库。
先上参考链接:Shared libraries with GCC on Linux
编译为可执行文件
gcc -o <output-target> <source-file1> <source-file2> ….
explain:
-o -- specify output file
e.g.
gcc -o hello hello.c
编译为静态库
gcc -c -o <output-target> <source-file1> <source-file2> ….
explain:
-c -- only compile, do not link
e.g.
gcc -c -o static_lib.a num.c wavelet.c
reference:
gcc -o <exe-name> -L<lib-path> -l<lib-name>
e.g.
gcc -o hello -L./lib -L/usr/lib -ltest -l:libtest.o
note:
-L -- specify library search path
-l -- specify library, support two kinds of names
lib<lib-name>.a
lib<lib-name>.so
-l: -- specify library with full name
with -l: , we can specify full name, lib should not obey the rules above
(which means lib<lib-name>.a or lib<lib-name>.so),
we can directly specify names such as "libtest.o" or "test.a" or whatever
编译为动态库
gcc -c -fpic -o <temp-object> <source-file1> <source-file2> …
gcc -shared -o <so-file/shared-lib> <temp-object1> <temp-object2> …
explain:
-fpic -- position independent code
-shared -- make a shared library
reference:
gcc -o <exe-name> -L<lib-path> -l<lib-name> <source-file1> <source-file2> ...
e.g.
gcc -o hello -L./lib -L/usr/lib -ltest -l:libtest.o hello.c util.c