在cmake中,同時生成靜態庫與動態庫的時候,需要一些技巧,因爲cmake中不能生成同名的靜態庫和動態庫,只能先生成不同名的庫之後再把其中一個庫的名字改名。
# 生成動態庫目標
add_library(MathFunctions SHARED ${srcs})
# 生成靜態庫目標
add_library(MathFunctions_static STATIC ${srcs})
# 指定靜態庫的輸出名稱
set_target_properties(MathFunctions_static PROPERTIES OUTPUT_NAME "MathFunctions")
# 使動態庫和靜態庫同時存在
set_target_properties(MathFunctions PROPERTIES CLEAN_DIRECT_OUTPUT 1)
set_target_properties(MathFunctions_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
# 指定動態庫版本
# VERSION 動態庫版本
# SOVERSION API版本
set_target_properties(person PROPERTIES VERSION 1.0 SOVERSION 1)
# 將動態庫與動態庫同時安裝到lib目錄中
install (TARGETS MathFunctions MathFunctions_static DESTINATION lib)
運行效果:
-rw-r--r-- 1 laolang users 5608 10月 12 02:25 MathFunctions.a
lrwxrwxrwx 1 laolang users 14 10月 12 02:25 MathFunctions.so -> MathFunctions.so.1
lrwxrwxrwx 1 laolang users 16 10月 12 02:25 MathFunctions.so.1 -> MathFunctions.so.1.0
-rwxr-xr-x 1 laolang users 10688 10月 12 02:25 MathFunctions.so.1.0
下面將完整的cmake教程工程鏈接如下:
在cmake環境下可以直接編譯,方便學習
https://download.csdn.net/download/andylauren/11583174