背景
很多情况下编译项目需要链接第三方库,此时需要知道第三方库的以下信息:
- .h头文件的地址
- 动态库.so或者静态库.a文件的地址
- 链接的库的名字
常用库
有些常用的库会提供一个FindXXXX.cmake
文件,此时我们只需要使用以下命令(以log4cpp
为例)
# 添加REQUIRED参数来表示这是必须的依赖库
find_package(LOG4CPP REQUIRED)
include_directories(${LOG4CPP_INCLUDE_DIR})
目录结构
为了能够包好多个Finder,需要组织以下目录结构以便之后添加相关module
├── CMakeLists.txt
│ ├── cmake
│ │ ├── modules # 用以存放FindXXXX.cmake
│ │ │ └── FindLOG4CPP.cmake
│ │ └── toolschain
│ │ └── Tiny4412.cmake # 用于存放toolschain.cmake
在顶层CMakeLists.txt中添加modules路径
set(CMAKE_MODULE_PATH
APPEND "${CMAKE_SOURCE_DIR}/cmake/modules/"
)
不常用库
有些时候,一些库并没有提供CMake模块文件,或者使用makefile
进行编译,就需要手写Finder了,建立./cmake/modules/
目录,用以存放本项目使用到的Finder。再顶层CMakeLists.txt
添加以下命令
set(CMAKE_MODULE_PATH
APPEND "${CMAKE_SOURCE_DIR}/cmake/modules/"
)
(依旧以log4cpp为例)建立一个名为FindLOG4CPP.cmake
的文件,内容如下
# - Find Log4cpp
# Find the native LOG4CPP includes and library
#
# LOG4CPP_INCLUDE_DIRS - where to find LOG4CPP.h, etc.
# LOG4CPP_LIBRARIES - List of libraries when using LOG4CPP.
# LOG4CPP_FOUND - True if LOG4CPP found.
# 判断是否已经包含log4cpp
if (LOG4CPP_INCLUDE_DIR)
set(LOG4CPP_FIND_QUIETLY TRUE)
endif ()
# 查找头文件位置
# PATH_SUFFIXES 路径后缀,正常安装的log4cpp位于系统路径/log4cpp/文件夹下
find_path(LOG4CPP_INCLUDE_DIR
NAMES Category.hh
# 可以通过以下命令来手动制定查找路径
# PATHS /usr/local/include
PATH_SUFFIXES log4cpp
DOC "Log4cpp include directories"
)
# 查找库文件位置
find_library(LOG4CPP_LIBRARY
NAMES log4cpp
DOC "Log4cpp library"
)
# 同时找到头文件位置和库文件位置时给相关变量赋值
if (LOG4CPP_INCLUDE_DIR AND LOG4CPP_LIBRARY)
set(LOG4CPP_FOUND TRUE)
set(LOG4CPP_LIBRARIES ${LOG4CPP_LIBRARY})
set(LOG4CPP_INCLUDE_DIRS ${LOG4CPP_INCLUDE_DIR})
else ()
set(LOG4CPP_FOUND FALSE)
message(WARNING "LOG4CPP not found")
endif ()
# 打印一些错误信息
if (LOG4CPP_FOUND)
if (NOT LOG4CPP_FIND_QUIETLY)
message(STATUS "Found LOG4CPP: ${LOG4CPP_LIBRARIES}")
endif ()
else ()
if (LOG4CPP_FIND_REQUIRED)
message(STATUS "Looked for LOG4CPP libraries named ${LOG4CPPS_NAMES}.")
message(FATAL_ERROR "Could NOT find LOG4CPP library")
endif ()
endif ()
# 这个选项不是很懂,貌似是给cmake gui用的
mark_as_advanced(
LOG4CPP_LIBRARIES
LOG4CPP_INCLUDE_DIRS
)
链接
此时只需要按照常用库的方法将相关变量写入include_directories
即可,在生成可执行文件时链接
add_executable(test test.cc)
target_link_libraries(test ${LOG4CPP_LIBRARIES})