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