CMake 入门实战
http://www.hahack.com/codes/cmake/
CMake 实践与踩坑记录
https://blog.csdn.net/weixin_43619346/category_9519766.html
CMake 手册详解
http://www.cnblogs.com/coderfenghc/archive/2012/06/16/CMake_ch_01.html
两个自己写的CMakeLists.txt用到了常用的编译功能
=================================================================================
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
#指定C编译器
SET(CMAKE_C_COMPILER "arm-none-linux-gnueabi-gcc")
#指定C++交叉编译器
SET(CMAKE_CXX_COMPILER "arm-none-linux-gnueabi-g++")# 头文件
INCLUDE_DIRECTORIES(/mnt/hgfs/share/build_libeventARM/include)
# 库路径
LINK_DIRECTORIES(/mnt/hgfs/share/build_libeventARM/lib)
# 项目信息
project (Demo)
#指定程序版本
set (Demo_VERSION_MAJOR 1)
set (Demo_VERSION_MINOR 0)
# 指定生成目标
add_executable(Demo main.cpp)
# 引用库
target_link_libraries(Demo event)
# 指定 MathFunctions 库的安装路径
#install (TARGETS MathFunctions DESTINATION bin)
#install (FILES MathFunctions.h DESTINATION include)
# 指定安装路径
install (TARGETS Demo DESTINATION bin)
#install (FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION include)
# 构建一个 CPack 安装包
include (InstallRequiredSystemLibraries)
#set (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set (CPACK_PACKAGE_VERSION_MAJOR "${Demo_VERSION_MAJOR}")
set (CPACK_PACKAGE_VERSION_MINOR "${Demo_VERSION_MINOR}")
include (CPack)
#生成二进制安装包
#cpack -C CPackConfig.cmake
#生成源码安装包
#cpack -C CPackSourceConfig.cmake
=================================================================================
#Make 最低版本号要求
cmake_minimum_required (VERSION 2.8)
#指定C编译器
SET(CMAKE_C_COMPILER "gcc")
#指定C++编译器
SET(CMAKE_CXX_COMPILER "g++")
#支持GDB
#set(CMAKE_BUILD_TYPE "Debug")
#set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
#set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
# 头文件
#INCLUDE_DIRECTORIES(/mnt/hgfs/share/build_libeventARM/include)
# 库路径
#LINK_DIRECTORIES(/mnt/hgfs/share/build_libeventARM/lib)
# 项目信息
project (Demo)
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)
# 添加 src 子目录
add_subdirectory(../../src obj)
# 指定生成目标
add_executable(Demo main.cpp)
# 添加链接库
target_link_libraries(Demo src)
# 加入一个配置头文件,用于处理 CMake 对源码的设置
configure_file (
"${PROJECT_SOURCE_DIR}/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
)
option (USE_MYDEF "Use provided DEFINE" ON)
if (USE_MYDEF)
MESSAGE(STATUS "use my define")
endif (USE_MYDEF)
# 启用测试
enable_testing()
add_test (test_USEMYDEF Demo)
set_tests_properties (test_USEMYDEF PROPERTIES PASS_REGULAR_EXPRESSION "run testValueInit.+ test successfully")
add_test (test_USEMYDEF1 Demo)
set_tests_properties (test_USEMYDEF1 PROPERTIES PASS_REGULAR_EXPRESSION "run testAssignment\\(\\) test successfully")
#rm -rf CMakeFiles CMakeCache.txt Makefile cmake_install.cmake Demo
#rm -rf obj/*
-----另一个文件夹的CMakeLists.txt
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_LIB_SRCS 变量
aux_source_directory(. DIR_LIB_SRCS)
# 生成链接库
add_library (src ${DIR_LIB_SRCS})