gcc -o program main.o \
-Wl,--whole-archive -lmylib \
-Wl,--no-whole-archive -llib1 -llib2
gcc 链接时会优先链接动态库,没有动态库的情况下会选择静态库。默认只选择静态库中被引用到目标文件进行链接。使用 -Wl,–whole-archive 选项会强制将整个静态库连接到可执行文件中,-Wl,–no-whole-archive 是结束标志。
参考
https://stackoverflow.com/questions/14116420/how-to-force-gcc-to-link-an-unused-static-library#:~:text=If%20there%20is%20a%20specific%20function%20in%20the,Add%20-u%20JNI_OnLoad%20to%20your%20link%20command.%20Share