关于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")

 

posted @ 2016-03-03 15:10  冷侃  阅读(5373)  评论(0编辑  收藏  举报