使用LD_LIBRARY_PATH的缺点是要实现设置LD_LIBRARY_PATH。不够自动化。那么大型的商业程序是如何加载自己的so呢。
这里以QtCreator为例。
QtCreator安装在/home/xxx/Qt5.3.1目录下。使用ldd查看qtcreator依赖的so。结果如下:
xxx@ubuntu:~/Qt5.3.1/Tools/QtCreator/bin$ ldd qtcreator linux-gate.so.1 => (0xb7701000) libExtensionSystem.so.1 => /home/xxx/Qt5.3.1/Tools/QtCreator/bin/./../lib/qtcreator/libExtensionSystem.so.1 (0xb76c2000) libQt5Widgets.so.5 => /home/xxx/Qt5.3.1/Tools/QtCreator/bin/./../lib/qtcreator/libQt5Widgets.so.5 (0xb707e000) libQt5Network.so.5 => /home/xxx/Qt5.3.1/Tools/QtCreator/bin/./../lib/qtcreator/libQt5Network.so.5 (0xb6f19000) libQt5Gui.so.5 => /home/xxx/Qt5.3.1/Tools/QtCreator/bin/./../lib/qtcreator/libQt5Gui.so.5 (0xb69d8000) libQt5Core.so.5 => /home/xxx/Qt5.3.1/Tools/QtCreator/bin/./../lib/qtcreator/libQt5Core.so.5 (0xb649d000) libpthread.so.0 => /lib/i386-linux-gnu/libpthread.so.0 (0xb646b000) libstdc++.so.6 => /usr/lib/i386-linux-gnu/libstdc++.so.6 (0xb6382000) libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1 (0xb6364000) libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb61b6000) libgobject-2.0.so.0 => /usr/lib/i386-linux-gnu/libgobject-2.0.so.0 (0xb6164000) libglib-2.0.so.0 => /lib/i386-linux-gnu/libglib-2.0.so.0 (0xb6058000) libX11.so.6 => /usr/lib/i386-linux-gnu/libX11.so.6 (0xb5f24000) libm.so.6 => /lib/i386-linux-gnu/libm.so.6 (0xb5edd000) libGL.so.1 => /usr/lib/i386-linux-gnu/mesa/libGL.so.1 (0xb5e7d000) libicui18n.so.52 => /home/xx/Qt5.3.1/Tools/QtCreator/bin/./../lib/qtcreator/./libicui18n.so.52 (0xb5c53000) libicuuc.so.52 => /home/xxx/Qt5.3.1/Tools/QtCreator/bin/./../lib/qtcreator/./libicuuc.so.52 (0xb5ad7000) libdl.so.2 => /lib/i386-linux-gnu/libdl.so.2 (0xb5ad2000) libgthread-2.0.so.0 => /usr/lib/i386-linux-gnu/libgthread-2.0.so.0 (0xb5ace000) librt.so.1 => /lib/i386-linux-gnu/librt.so.1 (0xb5ac5000) /lib/ld-linux.so.2 (0xb7702000) libffi.so.6 => /usr/lib/i386-linux-gnu/libffi.so.6 (0xb5abe000) libpcre.so.3 => /lib/i386-linux-gnu/libpcre.so.3 (0xb5a80000) libxcb.so.1 => /usr/lib/i386-linux-gnu/libxcb.so.1 (0xb5a5e000) libglapi.so.0 => /usr/lib/i386-linux-gnu/libglapi.so.0 (0xb5a45000) libXext.so.6 => /usr/lib/i386-linux-gnu/libXext.so.6 (0xb5a32000) libXdamage.so.1 => /usr/lib/i386-linux-gnu/libXdamage.so.1 (0xb5a2e000) libXfixes.so.3 => /usr/lib/i386-linux-gnu/libXfixes.so.3 (0xb5a28000) libX11-xcb.so.1 => /usr/lib/i386-linux-gnu/libX11-xcb.so.1 (0xb5a25000) libxcb-glx.so.0 => /usr/lib/i386-linux-gnu/libxcb-glx.so.0 (0xb5a0c000) libxcb-dri2.so.0 => /usr/lib/i386-linux-gnu/libxcb-dri2.so.0 (0xb5a06000) libxcb-dri3.so.0 => /usr/lib/i386-linux-gnu/libxcb-dri3.so.0 (0xb5a02000) libxcb-present.so.0 => /usr/lib/i386-linux-gnu/libxcb-present.so.0 (0xb59fe000) libxcb-sync.so.1 => /usr/lib/i386-linux-gnu/libxcb-sync.so.1 (0xb59f7000) libxshmfence.so.1 => /usr/lib/i386-linux-gnu/libxshmfence.so.1 (0xb59f3000) libXxf86vm.so.1 => /usr/lib/i386-linux-gnu/libXxf86vm.so.1 (0xb59ed000) libdrm.so.2 => /usr/lib/i386-linux-gnu/libdrm.so.2 (0xb59df000) libicudata.so.52 => /home/xxx/Qt5.3.1/Tools/QtCreator/bin/./../lib/qtcreator/././libicudata.so.52 (0xb4373000) libXau.so.6 => /usr/lib/i386-linux-gnu/libXau.so.6 (0xb436e000) libXdmcp.so.6 => /usr/lib/i386-linux-gnu/libXdmcp.so.6 (0xb4367000)
xxx@ubuntu:~/Qt5.3.1/Tools/QtCreator/bin$ readelf --dynamic qtcreator Dynamic section at offset 0x11eb0 contains 30 entries: 标记 类型 名称/值 0x00000001 (NEEDED) 共享库:[libExtensionSystem.so.1] 0x00000001 (NEEDED) 共享库:[libQt5Widgets.so.5] 0x00000001 (NEEDED) 共享库:[libQt5Network.so.5] 0x00000001 (NEEDED) 共享库:[libQt5Gui.so.5] 0x00000001 (NEEDED) 共享库:[libQt5Core.so.5] 0x00000001 (NEEDED) 共享库:[libpthread.so.0] 0x00000001 (NEEDED) 共享库:[libstdc++.so.6] 0x00000001 (NEEDED) 共享库:[libgcc_s.so.1] 0x00000001 (NEEDED) 共享库:[libc.so.6] 0x0000000f (RPATH) Library rpath: [$ORIGIN/../lib/qtcreator]
秘密就在这句:0x0000000f (RPATH) Library rpath: [$ORIGIN/../lib/qtcreator]
rpath与ORIGIN
rpath是gcc的一个参数。rpath添加一个目录。当程序被加载时,搜寻此目录,寻找动态库。rpath添加的目录信息保存在可执行文件中。即使这句
0x0000000f (RPATH) Library rpath: [xxx]。
现在的问题是,可执行文件如何知道自身所在目录。
ORIGIN变量代表了此目录。关于 ORIGIN更详细的信息,可参考此文档。
例子
例子目录结构如下
src
…main.c
…Makefile
…lib/foo.c
操作系统:ubuntu 14 32位
编译i:gcc 4.8
main.c文件源码
void test_tk(); int main(void) { test_tk(); return 0; }
main:main.c lib/libfoo.so gcc -L${shell pwd}/lib -g -Wall -o test -Wl,-rpath,'$$ORIGIN/lib' main.c -lfoo lib/libfoo.so:lib/foo.c gcc -g -Wall -fPIC -shared -o lib/libfoo.so lib/foo.c
#include <stdio.h> void test_tk() { printf("called!\n"); }
Dynamic section at offset 0xf04 contains 26 entries: 标记 类型 名称/值 0x00000001 (NEEDED) 共享库:[libfoo.so] 0x00000001 (NEEDED) 共享库:[libc.so.6] 0x0000000f (RPATH) Library rpath: [$ORIGIN/lib] ..........................................................
最近在做C++开发,在MBP上用Cmake构建项目的时候,发现 make install
之后生成的执行文件会运行出错:
dyld: Library not loaded: @rpath/libadk.dylib
Referenced from: /Users/kesco/Documents/workspaces/cpp/apue_practise/build/bin/./thread
Reason: image not found
[1] 52362 trace trap ./thread
提示我,动态链接库( libadk.dylib
也是工程内的一个子项目)找不着。这就奇怪了,因为我 make install
安装的时候,是把动态链接库和执行文件都放在同一个目录下的。为啥会找不着呢,而之前在Windows下是没问题的,后来查了下Cmake的文档,原来是Cmake版本的问题=_+。
如果你的CMakeList.txt上写的 cmake_minium_required
为2.6的话,会报下面异常:
....
CMake Warning (dev):
Policy CMP0042 is not set: MACOSX_RPATH is enabled by default. Run "cmake --help-policy CMP0042" for policy details. Use the cmake_policy command to
set the policy and suppress this warning.
MACOSX_RPATH is not specified for the following targets:
cjson
iniparser
stemmer
word2vec
This warning is for project developers. Use -Wno-dev to suppress it.
-- Generating done
....
而在 CMP0042
更新,也就是Cmake 2.8.1.2之后,如果你声明的 cmake_minium_required
为2.8以上, MACOSX_RPATH
会默认启动,这时候编译的执行文件在查找链接库的时候会往 @rpath
上搜索,所以就找不到要链接的库( libadk.dylib
在同一个目录下)。我们可以用 otool -L <file>
命令来查看执行文件的链接库依赖。
bin git:master ❯ otool -L thread
thread:
@rpath/libadk.dylib (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
知道原因之后就好办了,我们只需在CMakeList.txt上把 MACOSX_RPATH
关掉就好了。
set(CMAKE_MACOSX_RPATH 0)
add_subdirectory(adk)
add_subdirectory(thread)
....
重新用 cmake
生成MakeFile构建就不会报错了。不过, RPATH
是啥呢?以前写C++的时候都是在Windows上开着VS来的,没遇到过这样的问题,现在遇到了,自然要查清楚。
在Linux环境下,使用动态链接的程序在运行时会自动链接 ld.so
这个库(OS X上是 dyld
),然后通过 ld.so
来查找链接其它的库。而 RPATH
就是编译的时候链接到执行文件的链接库路径。OS X在 RPATH
的设置上和Linux还是有点出入的,OS X的 RPATH
采用的是绝对路径。
ld.so
搜索路径的优先级是这样的:
1. RPATH
,编译链接时加入 -rpath
参数指明所谓的 RUNPATH
,这样可执行文件(或者依赖其他动态链接库的动态链接库)就能告诉 ld.so
到哪里去搜索对应的动态链接库了。
2. LD_LIBRARY_PATH
,对于没有设定 RPATH
的可执行文件或者动态链接库,我们可以用 LD_LIBRARY_PATH
这个环境变量通知 ld.so
往哪里查找链接库。
3. /etc/ld.so.conf
,系统对 ld.so
的路径配置文件。
4. /usr/lib
、 /lib
和 /usr/local/lib
,系统默认路径。
在分发程序的时候,执行文件使用的链接库在系统内不一定会有,或者自带了的版本不对,一般都会在程序文件夹内都会附带相应的链接库,所以最好还是把 RPATH
加上。Cmake对RPATH提供了很多选项支持,我们一般只关注这几个变量就好了: CMAKE_SKIP_BUILD_RPATH
、 CMAKE_BUILD_WITH_INSTALL_RPATH
、 CMAKE_INSTALL_RPATH
和 CMAKE_INSTALL_RPATH_USE_LINK_PATH
。
set(CMAKE_SKIP_BUILD_RPATH FALSE) # 编译时加上RPATH
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) # 编译时RPATH不使用安装的RPATH
set(CMAKE_INSTALL_RPATH "") # 安装RPATH为空
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE) # 安装的执行文件不加上RPATH
Cmake在默认情况下, make install
会把安装的执行文件的 RPATH
删掉的,所以就会出现上面我执行安装好的执行文件报错的问题。
Cmake的默认设置我们肯定是不能使用的,我们需要一个安装的时候也要带上 RPATH
的设置。
set(INSTALL_LIB_DIR "${PROJECT_BINARY_DIR}/lib") # 假设安装目录在编译目录的lib子目录内
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# 确保链接库不在系统默认安装的目录上时更改到项目lib上
list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES ${CMAKE_INSTALL_RPATH} isSystemDir)
if("${isSystemDir}" STREQUAL "-1")
set(CMAKE_INSTALL_RPATH "${INSTALL_LIB_DIR}")
endif("${isSystemDir}" STREQUAL "-1")
现在Cmake在C++项目上应用得越来越多了,但是Cmake的文档很分散,写Cmake构建脚本的时候会踩上很多坑,只能慢慢积累经验总结。
CMakeLists.txt解读
# CMake要求的最低版本号 cmake_minimum_required( VERSION 2.8 ) # 项目名称 project( ycm_support_libs ) # 设置客户端lib和服务端lib的变量名称 set( CLIENT_LIB "ycm_client_support" ) set( SERVER_LIB "ycm_core" ) # 设置Python的版本号变量 set( Python_ADDITIONAL_VERSIONS 2.7 2.6 ) # 进行Python包的查找,这里是REQUIRED表示必须 find_package( PythonLibs 2.6 REQUIRED ) # 如果Python版本号低于3.0.0就进行FATAL_ERROR的出错信息 if ( NOT PYTHONLIBS_VERSION_STRING VERSION_LESS "3.0.0" ) message( FATAL_ERROR "CMake found python3 libs instead of python2 libs. YCM works only with " "python2.\n" ) endif() # 各种option option( USE_DEV_FLAGS "Use compilation flags meant for YCM developers" OFF ) # 这个变量就是我上文讲到的很关键的一个变量,来判断当前用户需要不需要libclang option( USE_CLANG_COMPLETER "Use Clang semantic completer for C/C++/ObjC" OFF ) # install.sh中的--system-libclang就与这个变量进行交互 option( USE_SYSTEM_LIBCLANG "Set to ON to use the system libclang library" OFF ) # YCM作者推荐的用法,在这里直接写入Clang的相关路径 注意这里的CACHE PATH,表示当用户如果命令行 # 进行指定,那优先会去读用户的命令行,而不是用这里的set,并且把相关的值写入Cache中 set( PATH_TO_LLVM_ROOT "" CACHE PATH "Path to the root of a LLVM+Clang binary distribution" ) # YCM作者推荐的另外一种安装方法,直接将libclang.so全路径写死 set( EXTERNAL_LIBCLANG_PATH "" CACHE PATH "Path to the libclang library to use" ) # 如果你使用libclang但是没有指定用不用系统的libclang,没有指定llvm_root,没有指定额外的libclang.so,那么就会带你去下载 if ( USE_CLANG_COMPLETER AND NOT USE_SYSTEM_LIBCLANG AND NOT PATH_TO_LLVM_ROOT AND NOT EXTERNAL_LIBCLANG_PATH ) message( "Downloading Clang 3.4" ) # 这就是llvm官网的3.4下载路径 set( CLANG_URL "http://llvm.org/releases/3.4" ) # 如果当前客户端是苹果 Mac OS X if ( APPLE ) # 设置Clang的文件夹名称 set( CLANG_DIRNAME "clang+llvm-3.4-x86_64-apple-darwin10.9" ) # 设置Clang的MD5校验码 set( CLANG_MD5 "4f43ea0e87090ae5e7bec12373ca4927" ) # 设置Clang文件名称为之后加上tar.gz set( CLANG_FILENAME "${CLANG_DIRNAME}.tar.gz" ) else() # 如果是64位平台 if ( 64_BIT_PLATFORM ) # 设置Clang的文件夹名称 set( CLANG_DIRNAME "clang+llvm-3.4-x86_64-unknown-ubuntu12.04" ) # 设置Clang的MD5校验码 set( CLANG_MD5 "6077459d20a7ff412eefc6ce3b9f5c85" ) # 设置Clang文件名称为之后加上tar.gz set( CLANG_FILENAME "${CLANG_DIRNAME}.tar.xz" ) else() # 表示此时为32位的Linux,下载3.3版本 message( "No pre-built Clang 3.4 binaries for 32 bit linux, " "downloading Clang 3.3" ) set( CLANG_URL "http://llvm.org/releases/3.3" ) # 设置Clang的文件夹名称 set( CLANG_DIRNAME "clang+llvm-3.3-i386-debian6" ) # 设置Clang的MD5校验码 set( CLANG_MD5 "415d033b60659433d4631df894673802" ) # 设置Clang文件名称为之后加上tar.gz set( CLANG_FILENAME "${CLANG_DIRNAME}.tar.bz2" ) endif() endif() # 下载命令 file( DOWNLOAD "${CLANG_URL}/${CLANG_FILENAME}" "./${CLANG_FILENAME}" SHOW_PROGRESS EXPECTED_MD5 "${CLANG_MD5}" ) # 文件名正则表达式匹配,进行相应的解压 if ( CLANG_FILENAME MATCHES ".+bz2" ) # 执行相关的外部命令 tar execute_process( COMMAND tar -xjf ${CLANG_FILENAME} ) elseif( CLANG_FILENAME MATCHES ".+xz" ) execute_process( COMMAND tar -xJf ${CLANG_FILENAME} ) else() execute_process( COMMAND tar -xzf ${CLANG_FILENAME} ) endif() # 设置PATH_TO_LLVM_ROOT的路径为当前CMake二进制路径下的Clang目录 set( PATH_TO_LLVM_ROOT "${CMAKE_CURRENT_BINARY_DIR}/../${CLANG_DIRNAME}" ) endif() # 如果设置了PATH_TO_LLVM_ROOT或者用户使用系统libclang或者有额外的libclang,就开启USE_CLANG_COMPLETER # 这个变量我上文提过,很关键 if ( PATH_TO_LLVM_ROOT OR USE_SYSTEM_LIBCLANG OR EXTERNAL_LIBCLANG_PATH ) set( USE_CLANG_COMPLETER TRUE ) endif() # 开始使用这个变量,如果用户确定使用libclang,但是没有root没有系统clang没有额外clang,那么 # 进行错误性提示 if ( USE_CLANG_COMPLETER AND NOT PATH_TO_LLVM_ROOT AND NOT USE_SYSTEM_LIBCLANG AND NOT EXTERNAL_LIBCLANG_PATH ) message( FATAL_ERROR "You have not specified which libclang to use. You have several options:\n" " 1. Set PATH_TO_LLVM_ROOT to a path to the root of a LLVM+Clang binary " "distribution. You can download such a binary distro from llvm.org. This " "is the recommended approach.\n" " 2. Set USE_SYSTEM_LIBCLANG to ON; this makes YCM search for the system " "version of libclang.\n" " 3. Set EXTERNAL_LIBCLANG_PATH to a path to whatever " "libclang.[so|dylib|dll] you wish to use.\n" "You HAVE to pick one option. See the docs for more information.") endif() # 进行用户提醒,提醒用户当前是否使用libclang if ( USE_CLANG_COMPLETER ) message( "Using libclang to provide semantic completion for C/C++/ObjC" ) else() message( "NOT using libclang, no semantic completion for C/C++/ObjC will be " "available" ) endif() # 如果设置了root就设置CLANG_INCLUDES_DIR为root下的include # 否则CLANG_INCLUDES_DIR为CMake下llvm/include if ( PATH_TO_LLVM_ROOT ) set( CLANG_INCLUDES_DIR "${PATH_TO_LLVM_ROOT}/include" ) else() set( CLANG_INCLUDES_DIR "${CMAKE_SOURCE_DIR}/llvm/include" ) endif() # 如果当前的include路径不是绝对路径 if ( NOT IS_ABSOLUTE "${CLANG_INCLUDES_DIR}" ) # 设置它为绝对路径 get_filename_component(CLANG_INCLUDES_DIR "${CMAKE_BINARY_DIR}/${CLANG_INCLUDES_DIR}" ABSOLUTE) endif() # 如果没有额外的libclang,但是有root if ( NOT EXTERNAL_LIBCLANG_PATH AND PATH_TO_LLVM_ROOT ) if ( MINGW ) # 如果是MINGW # 设置libclang的寻找路径(后面的find_library会去寻找) set( LIBCLANG_SEARCH_PATH "${PATH_TO_LLVM_ROOT}/bin" ) else() set( LIBCLANG_SEARCH_PATH "${PATH_TO_LLVM_ROOT}/lib" ) endif() # 这里TEMP会被find_library去寻找clang,和libclang两个so,并且复制路径给TEMP find_library( TEMP NAMES clang libclang PATHS ${LIBCLANG_SEARCH_PATH} NO_DEFAULT_PATH ) message("temp is ${TEMP}") # 设置额外的libclang为这个路径 set( EXTERNAL_LIBCLANG_PATH ${TEMP} ) endif() # 如果当前为苹果,设置额外的flag if ( APPLE ) set( CMAKE_INCLUDE_SYSTEM_FLAG_CXX "-isystem " ) endif() # 如果用户使用系统自带的boost if ( USE_SYSTEM_BOOST ) # 进行find boost命令,会用到python,filesystem,system,regex,thread等components find_package( Boost REQUIRED COMPONENTS python filesystem system regex thread ) else() # 使用自己的Boost set( Boost_INCLUDE_DIR ${BoostParts_SOURCE_DIR} ) set( Boost_LIBRARIES BoostParts ) endif() # 相关头文件的加入,Boost,Python和Clang include_directories( SYSTEM ${Boost_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS} ${CLANG_INCLUDES_DIR} ) # 全局递归查找h,cpp文件给SERVER_SOURCES file( GLOB_RECURSE SERVER_SOURCES *.h *.cpp ) # 全局递归查找测试相关文件 file( GLOB_RECURSE to_remove tests/*.h tests/*.cpp CMakeFiles/*.cpp *client* ) if( to_remove ) # 学习list相关的REMOVE_ITEM命令 list( REMOVE_ITEM SERVER_SOURCES ${to_remove} ) endif() # 这里就是这个变量最关键的地方,它会去include并且开启宏 if ( USE_CLANG_COMPLETER ) include_directories( ${CMAKE_CURRENT_SOURCE_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/ClangCompleter" ) add_definitions( -DUSE_CLANG_COMPLETER ) else() # 否则的话寻找所有ClangCompleter下的头和源文件进行删除 file( GLOB_RECURSE to_remove_clang ClangCompleter/*.h ClangCompleter/*.cpp ) if( to_remove_clang ) list( REMOVE_ITEM SERVER_SOURCES ${to_remove_clang} ) endif() endif() # 如果用户使用额外的libclang或者使用系统自带的libclang if ( EXTERNAL_LIBCLANG_PATH OR USE_SYSTEM_LIBCLANG ) if ( USE_SYSTEM_LIBCLANG ) # 如果是系统自带 if ( APPLE ) set( ENV_LIB_PATHS ENV DYLD_LIBRARY_PATH ) # 将环境变量下的DYLD_LIBRARY_PATH给ENV_LIB_PATHS elseif ( UNIX ) set( ENV_LIB_PATHS ENV LD_LIBRARY_PATH ) # 这也是我之前讲的一定要把你编译的libclang加入到这个环境变量中,因为它会根据这个去寻找 elseif ( WIN32 ) set( ENV_LIB_PATHS ENV PATH ) else () set( ENV_LIB_PATHS "" ) endif() file( GLOB SYS_LLVM_PATHS "/usr/lib/llvm*/lib" ) # 进行相关的libclang查找,在你之前给它指定的环境变量中 find_library( TEMP clang PATHS ${ENV_LIB_PATHS} /usr/lib /usr/lib/llvm ${SYS_LLVM_PATHS} /Library/Developer/CommandLineTools/usr/lib, /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib ) # 将寻找到的变量给EXTERNAL_LIBCLANG_PATH set( EXTERNAL_LIBCLANG_PATH ${TEMP} ) else() if ( NOT APPLE ) # 设置相关rpath set( CMAKE_BUILD_WITH_INSTALL_RPATH TRUE ) # 设置make install之后的rpath set( CMAKE_INSTALL_RPATH "\$ORIGIN" ) endif() endif() set( LIBCLANG_TARGET "" ) message( "Using external libclang: ${EXTERNAL_LIBCLANG_PATH}" ) message("libclang_target is ${LIBCLANG_TARGET}") else() set( LIBCLANG_TARGET ) endif() # 如果有额外的rpath,在这里进行设置 if ( EXTRA_RPATH ) set( CMAKE_INSTALL_RPATH "${EXTRA_RPATH}:${CMAKE_INSTALL_RPATH}" ) endif() # 如果在Linux下需要额外的rt库 if ( UNIX AND NOT APPLE ) set( EXTRA_LIBS rt ) endif() # 将目录下所有的h和cpp给CLIENT_SOURCES file( GLOB CLIENT_SOURCES *.h *.cpp ) # 相应SERVER_SPECIFIC赋值 file( GLOB SERVER_SPECIFIC *ycm_core* ) if( SERVER_SPECIFIC ) # 移除相关CLIEN_SOURCES下的SERVER_SPECIFIC list( REMOVE_ITEM CLIENT_SOURCES ${SERVER_SPECIFIC} ) endif() # 创建client的library,并且是动态库 add_library( ${CLIENT_LIB} SHARED ${CLIENT_SOURCES} ) # 将这个库与之前的rt,Boost,Python进行链接 target_link_libraries( ${CLIENT_LIB} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} ${EXTRA_LIBS} ) # 创建server的library,并且是动态库 add_library( ${SERVER_LIB} SHARED ${SERVER_SOURCES} ) # 将这个库与之前的rt,Boost,Python,libclang,而外的server lib进行链接 target_link_libraries( ${SERVER_LIB} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} ${LIBCLANG_TARGET} ${EXTRA_LIBS} ) # 如果定义了LIBCLANG_TARGET if( LIBCLANG_TARGET ) if( NOT WIN32 ) # 在非WIN32情况下增加自定义命令,将libclang.so/dll拷贝到自己目录下 add_custom_command( TARGET ${SERVER_LIB} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${LIBCLANG_TARGET}" "$<TARGET_FILE_DIR:${SERVER_LIB}>" ) else() add_custom_command( TARGET ${SERVER_LIB} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${PATH_TO_LLVM_ROOT}/bin/libclang.dll" "$<TARGET_FILE_DIR:${SERVER_LIB}>") endif() endif() # 建立依赖关系,表示这个项目需要这两个库共同完成 add_custom_target( ${PROJECT_NAME} DEPENDS ${CLIENT_LIB} ${SERVER_LIB} ) # Mac下的相关设置,如果是利用rpath的话Mac下还是会去寻找系统库,即使用户显示指定 # 这里需要改用@loader_path if ( EXTERNAL_LIBCLANG_PATH AND APPLE ) add_custom_command( TARGET ${SERVER_LIB} POST_BUILD COMMAND install_name_tool "-change" "@rpath/libclang.dylib" "@loader_path/libclang.dylib" "$<TARGET_FILE:${SERVER_LIB}>" ) endif() # 将这些库的前缀lib去掉,因为会扰乱Python模块的查找 set_target_properties( ${CLIENT_LIB} PROPERTIES PREFIX "") set_target_properties( ${SERVER_LIB} PROPERTIES PREFIX "") if ( WIN32 OR CYGWIN ) # 进行Windows下相关库的转移存放 set_target_properties( ${CLIENT_LIB} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../.. ) set_target_properties( ${SERVER_LIB} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../.. ) foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} ) string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG ) set_target_properties( ${CLIENT_LIB} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${PROJECT_SOURCE_DIR}/../.. ) set_target_properties( ${SERVER_LIB} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${PROJECT_SOURCE_DIR}/../.. ) endforeach() if ( WIN32 ) # 建立后缀名.pyd set_target_properties( ${CLIENT_LIB} PROPERTIES SUFFIX ".pyd") set_target_properties( ${SERVER_LIB} PROPERTIES SUFFIX ".pyd") elseif ( CYGWIN ) # CYGIN下后缀为dll set_target_properties( ${CLIENT_LIB} PROPERTIES SUFFIX ".dll") set_target_properties( ${SERVER_LIB} PROPERTIES SUFFIX ".dll") endif() else() # Mac和Linux下都为.so,虽然Mac下应该默认为.dylib,但Python识别不了dylib,因此这里还是设置成.so set_target_properties( ${CLIENT_LIB} PROPERTIES SUFFIX ".so") set_target_properties( ${SERVER_LIB} PROPERTIES SUFFIX ".so") endif() # 设置相关lib的输出目录 set_target_properties( ${CLIENT_LIB} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../.. ) set_target_properties( ${SERVER_LIB} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../.. ) if ( USE_DEV_FLAGS AND ( CMAKE_COMPILER_IS_GNUCXX OR COMPILER_IS_CLANG ) AND NOT CMAKE_GENERATOR_IS_XCODE ) # 增加相应flag set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror" ) endif() # 提出警告在使用C++11特性下 if ( USE_DEV_FLAGS AND COMPILER_IS_CLANG AND NOT CMAKE_GENERATOR_IS_XCODE AND NOT SYSTEM_IS_FREEBSD ) set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wc++98-compat" ) endif() if( SYSTEM_IS_SUNOS ) # SunOS需要-pthreads这个flag才能正常使用 set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthreads" ) endif() # 增加测试子目录tests add_subdirectory( tests )