關於cmake輸出動態連結庫名字的問題

冷侃發表於2016-03-03

使用cmake進行專案編譯管理時,我們經常使用

add_library(foo SHARED foo.cpp)

這樣的話,輸出時,如果在win下面會得到foo.dll,linux下面會得到libfoo.so,mac 下得到libfoo.dylib

如果我們編寫的是用於lua擴充套件的C模組,那麼在進行require的時候,比如這樣:

require 'libfoo'    --預設載入libfoo.[so|dll],並且執行luaopen_libluafoo
require 'foo'     --載入foo.so,並且執行luaopen_luafoo

並且各個平臺下各不相同,這真是太苦惱的,cmake就是方便

##這樣就不會有lib字首了
set_target_properties(foo PREFIX "")

##由於在mac下如果載入的是dylib也是件麻煩的事,不然把字尾也改了吧反正不考慮windows
set_target_properties(foo SUFFIX "so")

 對了,吐槽一下mac的rpath也是件麻煩的事情

好貼留名一下https://gist.github.com/robertmaynard/5750737

# enable @rpath in the install name for any shared library being built
set(CMAKE_MACOSX_RPATH 1)

# the install RPATH for bar to find foo in the install tree.
# if the install RPATH is not provided, the install bar will have none
set_target_properties(bar PROPERTIES INSTALL_RPATH "@loader_path/../lib")

 

相關文章