1.CMake编译原理
CMake是一种跨平台编译工具,比make更为高级,使用起来要方便得多。CMake主要是编写CMakeLists.txt文件,然后用cmake命令将CMakeLists.txt文件转化为make所需要的makefile文件,最后用make命令编译源码生成可执行程序或共享库(so(shared object))。因此CMake的编译基本就两个步骤:
1. cmake
2. make
cmake 指向CMakeLists.txt所在的目录,例如cmake .. 表示CMakeLists.txt在当前目录的上一级目录。cmake后会生成很多编译的中间文件以及makefile文件,所以一般建议新建一个新的目录,专门用来编译,例如
mkdir build
cd build
cmake ..
make
make根据生成makefile文件,编译程序。
编写CMakeLists.txt
接下来编写CMakeLists.txt文件,该文件放在和src,include的同级目录,实际方哪里都可以,只要里面编写的路径能够正确指向就好了。CMakeLists.txt文件,如下所示:
1 #1.cmake verson,指定cmake版本
2 cmake_minimum_required(VERSION 3.2)
3
4 #2.project name,指定项目的名称,一般和项目的文件夹名称对应
5 PROJECT(test_sqrt)
6
7 #3.head file path,头文件目录
8 INCLUDE_DIRECTORIES(
9 include
10 )
11
12 #4.source directory,源文件目录
13 AUX_SOURCE_DIRECTORY(src DIR_SRCS)
14
15 #5.set environment variable,设置环境变量,编译用到的源文件全部都要放到这里,否则编译能够通过,但是执行的时候会出现各种问题,比如"symbol lookup error xxxxx , undefined symbol"
16 SET(TEST_MATH
17 ${DIR_SRCS}
18 )
19
20 #6.add executable file,添加要编译的可执行文件
21 ADD_EXECUTABLE(${PROJECT_NAME} ${TEST_MATH})
22
23 #7.add link library,添加可执行文件所需要的库,比如我们用到了libm.so(命名规则:lib+name+.so),就添加该库的名称
24 TARGET_LINK_LIBRARIES(${PROJECT_NAME} m)
cmake_minimum_required(VERSION 2.6 FATAL_ERROR) #指定cmake版本
cmake_policy(VERSION 2.6)
project(tunerservice) #指定项目文件夹
include(GNUInstallDirs)
# 1. ENV setup
# 1.1 Include/link path
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include") #头文件目录
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/tunerservice/world")
include_directories("${CMAKE_INSTALL_INCLUDEDIR}")
link_directories("${CMAKE_INSTALL_LIBDIR}") #添加需要链接的库文件目录
# 1.2 C/CXX/LD Flags
add_definitions("-std=c++11 -fPIC -Wno-deprecated") #-W -Wall 添加编译条件
# 2. Build
# 2.1 Build libtuner
set(TUNER_LIB_VERSION "1.0.0")
set(TUNER_LIB_NAME "tuner")
set(TUNER_SRC_FILES libtuner/SVPTunerService_Client.cpp
libtuner/SVPSourceStub_Tuner.cpp) #设置源文件
add_library(${TUNER_LIB_NAME} SHARED ${TUNER_SRC_FILES}) #将TUNER_SRC_FILES编译成动态库libtuner.so.1.0.0,SHARED 表示动态库 STATIC表示静态库 MODULE表示模块
set_target_properties(${TUNER_LIB_NAME} PROPERTIES
VERSION "${TUNER_LIB_VERSION}"
CLEAN_DIRECT_OUTPUT 1)
target_link_libraries(${TUNER_LIB_NAME} source service basic RCF pthread m rt dl)
# 2.2 Build worldtuner
set(WORLD_TUNER_LIB_VERSION "1.0.0")
set(WORLD_TUNER_LIB_NAME "worldtuner")
set(WORLD_TUNER_SRC_FILES tunerservice/Receiver/Rec_analog.c
tunerservice/Receiver/Rec_main.c
tunerservice/Receiver/NXP_tuner_i2c.c)
add_library(${WORLD_TUNER_LIB_NAME} SHARED ${WORLD_TUNER_SRC_FILES})
set_target_properties(${WORLD_TUNER_LIB_NAME} PROPERTIES
VERSION "${WORLD_TUNER_LIB_VERSION}"
CLEAN_DIRECT_OUTPUT 1) //cmake在构建一个target的时候,会删除之前生成的target,一样是通过设置SET_TARGET_PROPERTIES(hello PROPERTIES CLEAN_DIRECT_OUTPUT 1)来达到目的 PROPERTIES VERSION 设置动态库的版本号
target_link_libraries(${WORLD_TUNER_LIB_NAME} pthread m rt dl)
# 2.3 Build tunerservice
set(TUNERSERVICE_BIN_NAME "tunerservice")
add_definitions("-DWORLD_TUNER")
set(TUNERSERVICE_SRC_FILES tunerservice/SVPTunerService.cpp
tunerservice/SVPTunerService_Server.cpp
tunerservice/SVPRmtTuner.cpp)
add_executable(${TUNERSERVICE_BIN_NAME} ${TUNERSERVICE_SRC_FILES})
target_link_libraries(${TUNERSERVICE_BIN_NAME} servicefw ${WORLD_TUNER_LIB_NAME} remote device asound service basic RCF pthread m rt)
# 3. Install
# 3.1 Install include files
install(PROGRAMS include/SVPTuner.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(DIRECTORY include/tuner DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(DIRECTORY include/rpc DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
# 3.2 Install libtuner
install(TARGETS ${TUNER_LIB_NAME}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(TARGETS ${WORLD_TUNER_LIB_NAME}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
# 3.3 Install tunerservice
install(TARGETS ${TUNERSERVICE_BIN_NAME}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
# 3.4 Install pkgconfig
#configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tuner.pc.in ${CMAKE_CURRENT_SOURCE_DIR}/tuner.pc @ONLY)
#install(PROGRAMS tuner.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
编译和运行程序
准备好了以上的所有材料,接下来,就可以编译了,由于编译中出现许多中间的文件,因此最好新建一个独立的目录build,在该目录下进行编译,编译步骤如下所示:
mkdir build
cd build
cmake ..
make
操作后,在build下生成的目录结构如下:
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ │ ├── 3.2.2
│ │ │ ├── CMakeCCompiler.cmake
│ │ │ ├── CMakeCXXCompiler.cmake
│ │ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ │ ├── CMakeSystem.cmake
│ │ │ ├── CompilerIdC
│ │ │ │ ├── a.out
│ │ │ │ └── CMakeCCompilerId.c
│ │ │ └── CompilerIdCXX
│ │ │ ├── a.out
│ │ │ └── CMakeCXXCompilerId.cpp
│ │ ├── cmake.check_cache
│ │ ├── CMakeDirectoryInformation.cmake
│ │ ├── CMakeOutput.log
│ │ ├── CMakeTmp
│ │ ├── feature_tests.bin
│ │ ├── feature_tests.c
│ │ ├── feature_tests.cxx
│ │ ├── Makefile2
│ │ ├── Makefile.cmake
│ │ ├── progress.marks
│ │ ├── TargetDirectories.txt
│ │ └── test_sqrt.dir
│ │ ├── build.make
│ │ ├── C.includecache
│ │ ├── cmake_clean.cmake
│ │ ├── DependInfo.cmake
│ │ ├── depend.internal
│ │ ├── depend.make
│ │ ├── flags.make
│ │ ├── link.txt
│ │ ├── progress.make
│ │ └── src
│ │ ├── b.c.o
│ │ └── main.c.o
│ ├── cmake_install.cmake
│ ├── Makefile
│ └── test_sqrt
├── CMakeLists.txt
├── include
│ └── b.h
└── src
├── b.c
└── main.c
三.Cmake第一个程序
• 1.编写hello.c
• 2. 编写CMakeLists.txt
• 3. 执行cmake .
“.” 代表的意思是当前目录,cmake会查找该目录下的CMakeList.txt 文件
• 4.执行过程,生成Makefile
• 5. 执行make