CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件
- 示例文件结构
eric@eric-PC:~/Documents/work/linux-c/cmake_demo$ tree -L 3
.
├── build.sh # 一键编译脚本
├── CMakeLists.txt # 顶层一级CMakeLists.txt
└── src # 源码目录
├── CMakeLists.txt # 二级CMakeLists.txt
├── curl # 模块一 curl
│ ├── CMakeLists.txt # 三级 CMakeLists.txt
│ ├── curl_demo.c # 模块一 源码
│ └── curl_demo.h # 模块一 头文件
├── timer # 模块二 timer
│ ├── CMakeLists.txt
│ ├── event_timer.c
│ └── livev_demo.c
├── tools # 模块三 tools (公共模块)
│ ├── CMakeLists.txt
│ ├── tools.c
│ └── tools.h
├── main.c # main函数入口
└── main.h
- 一键编译脚本,build.sh
#!/bin/bash
mkdir -p build build/bin build/lib
cd build
if [ " $1" == " " ]; then
compiler="gcc"
elif [ $1 == "--help" -o $1 == "-help" -o $1 == "--h" -o $1 == "-h" ]; then
echo "usage: $0 [compiler] [compiler path]"
echo " compiler: specify the compiler you are using, default: gcc"
echo " compiler path: specify the compiler path you are using"
echo " eg:"
echo " ./build.sh"
echo " ./build.sh arm-linux-gnueabihf-gcc"
echo " ./build.sh /usr/bin/arm-linux-gnueabihf-gcc"
echo " ./build.sh arm-linux-gnueabihf-gcc /usr/bin"
exit
else
if [ " $2" == " " ]; then
compiler=$1
else
compiler=$2/$1
fi
fi
path=$(which $compiler)
if [ " $path" == " " ]; then
echo -e "\033[31mNo $compiler compiler found in the system\033[0m"
exit
fi
cmake .. "-DCMAKE_C_COMPILER=$path"
make
- 一级CMakeLists.txt (仅仅指定源码目录)
# 制定cmake最低版本要求
cmake_minimum_required(VERSION 2.8)
set(SUBDIRS "src")
foreach(subdir ${SUBDIRS})
add_subdirectory(${subdir})
endforeach()
- 二级CMakeLists.txt (主要编译参数指定,以及目标生成)
# 建立工程
project(cmake_test)
# 设置目标
set(TARGETS "test")
# 设置子目录
set(SUBDIRS "curl" "timer" "tools" )
set(INCDIRS "curl" "timer" "tools" )
# 生成路径
set(OUTDIRS "../build")
# 所有需要编译的子模块
set(LIBNAMES "curl" "timer" "tools" )
set(PROJECT_ROOT_PATH "${PROJECT_SOURCE_DIR}")
set(LIBRARY_OUTPUT_PATH "${PROJECT_ROOT_PATH}/${OUTDIRS}/lib/")
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_ROOT_PATH}/${OUTDIRS}/bin/")
set(CMAKE_BUILD_TYPE "Release") # Debug Release
set(CMAKE_LIB_TYPE "STATIC") # SHARED STATIC
set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "g++" )
# 设定变异参数
set(CMAKE_C_FLAGS "-Wall")
set(CMAKE_C_FLAGS "-lpthread")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -O0 -g -ggdb")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O3 -DNDEBUG")
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-std=c++11")
set(CMAKE_CXX_FLAGS "-lpthread")
set(CMAKE_CXX_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -O0 -g -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3")
endif(CMAKE_COMPILER_IS_GNUCXX)
# 添加include路径
foreach(incdir ${INCDIRS})
include_directories(${incdir})
endforeach()
# 添加子路径
foreach(subdir ${SUBDIRS})
add_subdirectory(${PROJECT_ROOT_PATH}/${subdir})
endforeach()
link_directories(${LIBRARY_OUTPUT_PATH})
# 当前目录下查找所有源文件
aux_source_directory(. DIR_SRCS)
# 生成可执行文件
add_executable(${TARGETS} ${DIR_SRCS})
# 添加依赖 (TARGETS 依赖 app1 & app2)
foreach(findlib ${LIBNAMES})
target_link_libraries(${TARGETS} ${findlib})
endforeach()
# 链接其他动态库
find_package("Threads")
target_link_libraries(${TARGETS} ${CMAKE_THREAD_LIBS_INIT})
find_package("CURL")
message(WARNING "find CURL_LIBRARY!:"${CURL_LIBRARY})
target_link_libraries(${TARGETS} ${CURL_LIBRARY})
- 三级 CMakeLists.txt (编译子模块,使之生成对应的lib)
aux_source_directory(. DIR_SRCS)
# 生成lib
string(REGEX REPLACE ".*/(.*)" "\\1" LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR})
if (DIR_SRCS)
foreach(libname ${LIBNAMES})
if (${LIB_NAME} STREQUAL ${libname})
add_library(${libname} ${CMAKE_LIB_TYPE} ${DIR_SRCS})
# 添加本模块所依赖的其他模块
target_link_libraries(${libname} "tools")
endif()
endforeach()
else()
message(WARNING "not find is src file!")
endif()
- 一键编译
eric@eric-PC:~/Documents/work/linux-c/cmake_demo$ ./build.sh
-- The C compiler identification is GNU 8.3.0
-- The CXX compiler identification is GNU 8.3.0
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning (dev) at src/CMakeLists.txt:69:
Syntax Warning in cmake code at column 38
Argument not separated from preceding token by whitespace.
This warning is for project developers. Use -Wno-dev to suppress it.
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE
-- Found CURL: /usr/local/lib/libcurl.so (found version "7.71.1")
CMake Warning at src/CMakeLists.txt:69 (message):
find CURL_LIBRARY!:/usr/local/lib/libcurl.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/eric/Documents/work/linux-c/cmake_demo/build
Scanning dependencies of target tools
[ 10%] Building C object src/tools/CMakeFiles/tools.dir/tools.c.o
[ 20%] Linking C static library ../../lib/libtools.a
[ 20%] Built target tools
Scanning dependencies of target curl
[ 30%] Building C object src/curl/CMakeFiles/curl.dir/curl_demo.c.o
[ 40%] Building C object src/curl/CMakeFiles/curl.dir/curl_ota.c.o
[ 50%] Linking C static library ../../lib/libcurl.a
[ 50%] Built target curl
Scanning dependencies of target timer
[ 60%] Building C object src/timer/CMakeFiles/timer.dir/event_timer.c.o
[ 70%] Building C object src/timer/CMakeFiles/timer.dir/livev_demo.c.o
[ 80%] Linking C static library ../../lib/libtimer.a
[ 80%] Built target timer
Scanning dependencies of target test
[ 90%] Building C object src/CMakeFiles/test.dir/main.c.o
[100%] Linking C executable ../bin/test
[100%] Built target test
- 查看build目录
eric@eric-PC:~/Documents/work/linux-c/cmake_demo/build$ tree -L 2
.
├── bin
│ └── test
├── CMakeCache.txt
├── CMakeFiles
│ ├── 3.13.4
│ ├── cmake.check_cache
│ ├── CMakeDirectoryInformation.cmake
│ ├── CMakeOutput.log
│ ├── CMakeTmp
│ ├── feature_tests.bin
│ ├── feature_tests.c
│ ├── feature_tests.cxx
│ ├── Makefile2
│ ├── Makefile.cmake
│ ├── progress.marks
│ └── TargetDirectories.txt
├── cmake_install.cmake
├── lib
│ ├── libcurl.a
│ ├── libtimer.a
│ └── libtools.a
├── Makefile
└── src
├── CMakeFiles
├── cmake_install.cmake
├── curl
├── Makefile
├── timer
└── tools
10 directories, 19 files