關於cmake輸出動態鏈接庫名字的問題


使用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")

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM