Cmake实例
CMAKE实例(包括安装install)
工程目录:目标是生成一个动态库DLL、库LIB、包含文件include,并安装到/build/bin目录下.
EventBus:
|-3rd/
|-src/*h;*.cpp
|-doc/
|-readme
|-CMakeLists.txt
最外层代码:
cmake_minimum_required (VERSION 2.6)
project (EventBus)
# subdirs
ADD_SUBDIRECTORY(src)
set(PROJECT_BINARY_DIR ${PROJECT_SOURCE_DIR}/build)
set(CMAKE_INSTALL_PREFIX bin)
# 设置例子
OPTION(BUILD_EXAMPLES "Enable to build Examples" OFF)
if(BUILD_EXAMPLES)
ADD_SUBDIRECTORY(example)
endif(BUILD_EXAMPLES)
# 设置编译选项
if(WIN32)
IF(MSVC)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /MDT")
ENDIF(MSVC)
endif(WIN32)
# 设置生成目标的前缀
SET(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "add a postfix, usually d on windows")
SET(CMAKE_RELEASE_POSTFIX "" CACHE STRING "add a postfix, usually empty on windows")
SET(CMAKE_RELWITHDEBINFO_POSTFIX "rd" CACHE STRING "add a postfix, usually empty on windows")
SET(CMAKE_MINSIZEREL_POSTFIX "s" CACHE STRING "add a postfix, usually empty on windows")
# Set the build postfix extension according to what configuration is being built.
IF (CMAKE_BUILD_TYPE MATCHES "Release")
SET(CMAKE_BUILD_POSTFIX "${CMAKE_RELEASE_POSTFIX}")
ELSEIF (CMAKE_BUILD_TYPE MATCHES "MinSizeRel")
SET(CMAKE_BUILD_POSTFIX "${CMAKE_MINSIZEREL_POSTFIX}")
ELSEIF(CMAKE_BUILD_TYPE MATCHES "RelWithDebInfo")
SET(CMAKE_BUILD_POSTFIX "${CMAKE_RELWITHDEBINFO_POSTFIX}")
ELSEIF(CMAKE_BUILD_TYPE MATCHES "Debug")
SET(CMAKE_BUILD_POSTFIX "${CMAKE_DEBUG_POSTFIX}")
ELSE()
SET(CMAKE_BUILD_POSTFIX "")
ENDIF()
# copy *.h to bin/include/
install(DIRECTORY src/ DESTINATION include
FILES_MATCHING PATTERN "*.h")
src目录下的:
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/3rd/boost/include)
link_directories(${PROJECT_SOURCE_DIR}/3rd/boost/lib)
SET(TARGET_NAME ${PROJECT_NAME})
SET(LIBRARY_OUTPUT_PATH bin)
# 定义导出符号
ADD_DEFINITIONS(-DEventBus_LIBRARY)
file(GLOB_RECURSE SOURCES "*.cpp")
file(GLOB_RECURSE HEADERS "*.h")
# 设置boost使用静态链接
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
# 生成动态库
ADD_library(${TARGET_NAME} SHARED ${SOURCES} ${HEADERS})
#target_link_libraries(${TARGET_NAME} Boost::date_time Boost::filesystem Boost::iostreams )
# install to bin/
message("install to ${PROJECT_BINARY_DIR}/bin")
install(TARGETS ${TARGET_NAME}
RUNTIME DESTINATION bin
ARCHIVE DESTINATION lib
)
值得一提的是安装命令install,按照文档介绍及网上资料,LIB文件不会拷贝,纠结了半天,后来仔细看了下文档说明,才搞明白DLL中的LIB是按照ARCHIVE来说而不LIBRARY。其它工程很详细,不再说明。
附:
install(TARGETS targets... [EXPORT <export-name>]
[[ARCHIVE|LIBRARY|RUNTIME|FRAMEWORK|BUNDLE|
PRIVATE_HEADER|PUBLIC_HEADER|RESOURCE]
[DESTINATION <dir>]
[PERMISSIONS permissions...]
[CONFIGURATIONS [Debug|Release|...]]
[COMPONENT <component>]
[OPTIONAL] [EXCLUDE_FROM_ALL]
[NAMELINK_ONLY|NAMELINK_SKIP]
] [...]
[INCLUDES DESTINATION [<dir> ...]]
) The TARGETS form specifies rules for installing targets from a project. There are five kinds of target files that may be installed: ARCHIVE, LIBRARY, RUNTIME, FRAMEWORK, and BUNDLE. Executables are treated as RUNTIME targets, except that those marked with the MACOSX_BUNDLE property are treated as BUNDLE targets on OS X. **Static libraries are always treated as ARCHIVE targets. Module libraries are always treated as LIBRARY targets.** For non- DLL platforms shared libraries are treated as LIBRARY targets, except that those marked with the FRAMEWORK property are treated as FRAMEWORK targets on OS X. **For DLL platforms the DLL part of a shared library is treated as a RUNTIME target and the corresponding import library is treated as an ARCHIVE target. All Windows-based systems including Cygwin are DLL platforms.** The ARCHIVE, LIBRARY, RUNTIME, and FRAMEWORK arguments change the type of target to which the subsequent properties apply. If none is given the installation properties apply to all target types. If only one is given then only targets of that type will be installed (which can be used to install just a DLL or just an import library)
参考: