I. Principles of CMake compilation
Cmake is a cross platform compilation tool, which is more advanced than make and much more convenient to use. Cmake is mainly to write CMakeLists.txt file, then use cmake command to convert CMakeLists.txt file to make required makefile file file, and finally use make command to compile source code to generate executable program or shared object. Therefore, there are basically two steps to compile cmake:
cmake
make
cmake points to the directory where CMakeLists.txt is located, for example, cmake.. / indicates that CMakeLists.txt is at the upper level of the current directory. Many compiled intermediate files and makefile s will be generated after cmake, so it is generally recommended to create a new directory for compilation, for example:
mkdir build
cd build
cmake ../
make
make compiles the program according to the makefile generated.
II. Preparation of CMakeLists.txt file
#Notation #cmake file for project association #The minimum version of cmake is required, and the build process below 2.8 will be terminated. CMAKE_MINIMUM_REQUIRED(VERSION 2.8) #Define project name PROJECT(association) #Print related message #MESSAGE(STATUS "Project: ${PROJECT_NAME}") #MESSAGE(STATUS "Project Directory: ${PROJECT_SOURCE_DIR}") #Specify compile type debug version SET(CMAKE_BUILE_TYPE DEBUG) #Distribution version #SET(CMAKE_BUILE_TYPE RELEASE) #SET(CMAKE_C_FLAGS_DEBUG "-g -Wall") #C #SET(CMAKE_CXX_FLAGS_DEBUG "-g -Wall") #C++ #Set up C + + compilation SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -s -Wall -W -O3") #add subdirectory ADD_SUBDIRECTORY(src/include) #Set variables to represent all source files SET(SOURCE_FILES src/main.cpp ) #Configure the directory of related library files, LINK_DIRECTORIES( /usr/local/lib ) #Looking for BZip2 FIND_PACKAGE(BZip2) if (BZIP2_FOUND) MESSAGE(STATUS "${BZIP_INCLUDE_DIRS}") MESSAGE(STATUS " ${BZIP2_LIBRARIES}") endif (BZIP2_FOUND) if (NOT BZIP2_FOUND) MESSAGE(STATUS "NOT BZIP2_FOUND") endif (NOT BZIP2_FOUND) #Directory of related header files INCLUDE_DIRECTORIES( /usr/local/include ${PROJECT_SOURCE_DIR}/utility_inc ${BZIP_INCLUDE_DIRS} ) #Link library LINK_LIBRARIES( ${PROJECT_SOURCE_DIR}/static_libs/libSentinelKeys64.a ${BZIP2_LIBRARIES} ) #Generate executable ADD_EXECUTABLE(${PROJECT_NAME} ${SOURCE_FILES}) #Dependent library files TARGET_LINK_LIBRARIES(${PROJECT_NAME} eventloop)
III. preparation of the subdirectory CMakeLists.txt
SET(EVENTLOOP_SOURCE_FILES tool/BlockingQueue.hpp tool/Copyable.h tool/ExecuteState.h tool/Likely.h EventLoop.h EventLoop.cpp ) #Generate static link library eventloop ADD_LIBRARY(eventloop ${EVENTLOOP_SOURCE_FILES})