总结自己的项目。根据自己项目的结构,书写CMakeLists.txt,并给与注释。了解了Cmake的基本语法与用法。
常用Cmake命令- cmake_minimum_required - 指定CMake的最小版本要求
例子:
# CMake最小版本要求为2.8.3 cmake_minimum_required(VERSION 2.8.3)
- project - 定义工程名称,并可指定工程支持的语言
例子:
# 指定工程名为HELLOWORLD project(HELLOWORLD)
- set - 显式的定义变量
例子:
# 定义SRC变量,其值为sayhello.cpp hello.cpp set(SRC sayhello.cpp hello.cpp)
- include_directories - 向工程添加多个特定的头文件搜索路径 —>相当于指定g++编译器的-I参数
例子:
# 将/usr/include/myincludefolder 和 ./include 添加到头文件搜索路径 include_directories(/usr/include/myincludefolder ./include)
- link_directories - 向工程添加多个特定的库文件搜索路径 —>相当于指定g++编译器的-L参数
例子:
# 将/usr/lib/mylibfolder 和 ./lib 添加到库文件搜索路径 link_directories(/usr/lib/mylibfolder ./lib)
- add_library - 生成库文件
例子:
# 通过变量 SRC 生成 libhello.so 共享库
add_library(hello SHARED ${SRC})
- add_compile_options - 添加编译参数
例子:
# 添加编译参数 -Wall -std=c++11 -O2 add_compile_options(-Wall -std=c++11 -02
- add_executable - 生成可执行文件
例子:
# 编译main.cpp生成可执行文件main add_executable(main main.cpp)
- target_link_libraries - 为 target 添加需要链接的共享库 —>相同于指定g++编译器-l参数
例子:
# 将hello动态库文件链接到可执行文件main target_link_libraries(main hello)
- add_subdirectory - 向当前工程添加存放源文件的子目录,并可以指定中间二进制和目标二进制存放的位置
例子:
# 添加src子目录,src中需有一个CMakeLists.txt add_subdirectory(src)
- aux_source_directory - 发现一个目录下所有的源代码文件并将列表存储在一个变量中,这个指令临时被用来自动构建源文件列表
例子:
# 定义SRC变量,其值为当前目录下所有的源代码文件
aux_source_directory(. SRC)
# 编译SRC变量所代表的源代码文件,生成main可执行文件
add_executable(main ${SRC})
Cmake常用变量
- CMAKE_C_FLAGS gcc编译选项
- CMAKE_CXX_FLAGS g++编译选项
# 在CMAKE_CXX_FLAGS编译选项后追加-std=c++11
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
- CMAKE_BUILD_TYPE 编译类型(Debug, Release)
# 设定编译类型为debug,调试时需要选择debug set(CMAKE_BUILD_TYPE Debug) # 设定编译类型为release,发布时需要选择release set(CMAKE_BUILD_TYPE Release)
- CMAKE_SOURCE_DIR
- PROJECT_SOURCE_DIR
- _SOURCE_DIR
这三个变量指代的内容是一致的,不论采用何种编译方式,都是工程顶层目录。 - CMAKE_C_COMPILER:指定C编译器
- CMAKE_CXX_COMPILER:指定C++编译器
- EXECUTABLE_OUTPUT_PATH:可执行文件输出的存放路径
- LIBRARY_OUTPUT_PATH:库文件输出的存放路径
顶层目录下主要的文件夹有:bin、build、include、src、thirdparty。include文件夹下还有子文件夹,用于存放相关的头文件;src文件夹下也有子文件夹,存放不同的源文件。并且顶层目录下还必须有的CMakeLists.txt。
gyp@gyp:~/distributed-system-framework$ tree -x
.
├── bin
├── build
├── CMakeLists.txt
├── include
│ ├── client
│ │ ├── informationCollectorServerThread.h
│ │ ├── public.h
│ │ ├── subModule.h
│ │ ├── system.h
│ │ ├── tcpConnect.h
│ │ ├── tcpServer.h
│ │ └── transfer.h
│ ├── db
│ │ └── db.h
│ ├── dbConnectionPool
│ │ ├── connection.h
│ │ └── connectionPool.h
│ ├── loadBalanceServer
│ │ ├── ConsistentHashCircle.h
│ │ ├── HashFunction.h
│ │ ├── LoadBalanceServer.h
│ │ ├── public.h
│ │ ├── PysicalNode.h
│ │ ├── TcpServer.h
│ │ └── VirtualNode.h
│ └── server
│ ├── controller.h
│ ├── public.h
│ ├── server.h
│ ├── tcpClient.h
│ ├── tcpServer.h
│ ├── thread.h
│ ├── threadPool.h
│ └── view.h
├── src
│ ├── client
│ │ ├── CMakeLists.txt
│ │ ├── informationCollectorServerThread.cpp
│ │ ├── main.cpp
│ │ ├── subModule.cpp
│ │ ├── system.cpp
│ │ ├── tcpConnect.cpp
│ │ ├── tcpServer.cpp
│ │ └── transfer.cpp
│ ├── CMakeLists.txt
│ ├── informationCollector
│ │ ├── CMakeLists.txt
│ │ └── informationCollectorClient.cpp
│ ├── loadBalanceServer
│ │ ├── CMakeLists.txt
│ │ ├── ConsistentHashCircle.cpp
│ │ ├── HashFunction.cpp
│ │ ├── LoadBalanceServer.cpp
│ │ ├── main.cpp
│ │ ├── PysicalNode.cpp
│ │ ├── tcpServer.cpp
│ │ └── VirtualNode.cpp
│ └── server
│ ├── CMakeLists.txt
│ ├── controller.cpp
│ ├── db
│ │ └── db.cpp
│ ├── dbConnectionPool
│ │ ├── Connection.cpp
│ │ └── dbConnectionPool.cpp
│ ├── main.cpp
│ ├── mainServer.cpp
│ ├── tcpClient.cpp
│ ├── tcpServer.cpp
│ ├── thread.cpp
│ ├── threadPool.cpp
│ └── view.cpp
└── thirdparty
└── logger.h
项目简要目录:
gyp@gyp:~/distributed-system-framework$ tree -d . ├── bin ├── build ├── include │ ├── client │ ├── db │ ├── dbConnectionPool │ ├── loadBalanceServer │ └── server ├── src │ ├── client │ ├── informationCollector │ ├── loadBalanceServer │ └── server │ ├── db │ └── dbConnectionPool └── thirdparty
编译的目的是生成4个可执行程序,包括:client、server、loadbalanceserver、informationCollector
下面依次给出每个目录下CMakeLists.txt的写法和注释。
顶层目录:顶层目录下的CMakeLists.txt内容/home/gyp/distributed-system-framework/
#指定CMake的最小版本要求
cmake_minimum_required(VERSION 3.0.0)
#定义工程名称
project(dsf)
# CMAKE_CXX_FLAGS是g++编译选项,整行的作用:在CMAKE_CXX_FLAGS编译选项后追加 -g,-g是为了调试使用
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -g)
# EXECUTABLE_OUTPUT_PATH:可执行文件输出的存放路径,整行作用:设置可执行文件存放到/bin目录下
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
# include_directories - 向工程添加多个特定的头文件搜索路径 --->相当于指定g++编译器的-I参数
# 下面几行是向工程添加头文件搜索路径
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/thirdparty)
include_directories(${PROJECT_SOURCE_DIR}/include/client)
include_directories(${PROJECT_SOURCE_DIR}/include/dbConnectionPool)
include_directories(${PROJECT_SOURCE_DIR}/include/loadBalanceServer)
include_directories(${PROJECT_SOURCE_DIR}/include/server)
include_directories(${PROJECT_SOURCE_DIR}/include/db)
# add_subdirectory - 向当前工程添加存放源文件的子目录,即向顶层目录添加子目录src用于存放源文件
add_subdirectory(src)
src目录
src目录下的CMakeLists.txt内容/home/gyp/distributed-system-framework/src/
#src下全是子目录,所以向当前目录添加子目录,用于存放源文件 # 添加client子目录,client中需有一个CMakeLists.txt add_subdirectory(client) # 添加informationCollector子目录,informationCollector中需有一个CMakeLists.txt add_subdirectory(informationCollector) # 添加server子目录,server中需有一个CMakeLists.txt add_subdirectory(server) # 添加loadBalanceServer子目录,loadBalanceServer中需有一个CMakeLists.txt add_subdirectory(loadBalanceServer)src目录下的client目录
client目录下的CMakeLists.txt内容/home/gyp/distributed-system-framework/src/client/
# aux_source_directory - 发现一个目录下所有的源代码文件并将列表存储在一个变量中
# 这个指令临时被用来自动构建源文件列表
# 定义SRC_LIST变量,其值为当前目录下所有的源代码文件
aux_source_directory(. SRC_LIST)
add_definitions("-g")
# add_executable - 生成可执行文件
# 编译client目录下的所有源文件,生成可执行文件 client
add_executable(client ${SRC_LIST})
# target_link_libraries - 为 target 添加需要链接的共享库 --->相同于指定g++编译器-l参数
# 将event pthread jsoncpp三个动态库文件 链接到 可执行文件client
target_link_libraries(client event pthread jsoncpp)
src目录下的informationCollector目录
informationCollector目录下的CMakeLists.txt内容/home/gyp/distributed-system-framework/src/informationCollector/
和上面的CMakeLists.txt的内容差不多
# aux_source_directory - 发现一个目录下所有的源代码文件并将列表存储在一个变量中
# 这个指令临时被用来自动构建源文件列表
# 定义SRC_LIST变量,其值为当前目录下所有的源代码文件
aux_source_directory(. SRC_LIST)
add_definitions("-g")
# add_executable - 生成可执行文件
# 编译 informationCollector 目录下的所有源文件,生成可执行文件 informationCollector
add_executable(informationCollector ${SRC_LIST})
# target_link_libraries - 为 target 添加需要链接的共享库 --->相同于指定g++编译器-l参数
# 将 jsoncpp 动态库文件 链接到 可执行文件 informationCollector
target_link_libraries(informationCollector jsoncpp)
src目录下的loadBalanceServer目录
loadBalanceServer目录下的CMakeLists.txt内容/home/gyp/distributed-system-framework/src/loadBalanceServer/
# aux_source_directory - 发现一个目录下所有的源代码文件并将列表存储在一个变量中
# 这个指令临时被用来自动构建源文件列表
# 定义SRC_LIST变量,其值为当前目录下所有的源代码文件
aux_source_directory(. SRC_LIST)
add_definitions("-g")
# add_executable - 生成可执行文件
# 编译 loadBalanceServer 目录下的所有源文件,生成可执行文件 loadBalanceServer
add_executable(loadBalanceServer ${SRC_LIST})
# target_link_libraries - 为 target 添加需要链接的共享库 --->相同于指定g++编译器-l参数
# 将 pthread jsoncpp event crypto 三个动态库文件 链接到 可执行文件 loadBalanceServer
target_link_libraries(loadBalanceServer pthread jsoncpp event crypto)
src目录下的server目录
server目录下的CMakeLists.txt内容/home/gyp/distributed-system-framework/src/server/
# aux_source_directory - 发现一个目录下所有的源代码文件并将列表存储在一个变量中
# 这个指令临时被用来自动构建源文件列表
# 定义SRC_LIST变量,其值为当前目录下所有的源代码文件
aux_source_directory(. SRC_LIST)
# 定义DB_LIST变量,其值为 当前目录下的db目录(server/db)下 所有的源代码文件
aux_source_directory(./db DB_LIST)
# 定义DBCONNECTIONPOOL_LIST变量,其值为 当前目录下的dbConnectionPool目录(server/dbConnectionPool)下 所有的源代码文件
aux_source_directory(./dbConnectionPool DBCONNECTIONPOOL_LIST)
add_definitions("-g")
# add_executable - 生成可执行文件
# 编译 server 目录下的所有源文件,生成可执行文件 loadBalanceServer
add_executable(server ${SRC_LIST} ${DB_LIST} ${DBCONNECTIONPOOL_LIST})
# target_link_libraries - 为 target 添加需要链接的共享库 --->相同于指定g++编译器-l参数
# 将 pthread jsoncpp event crypto mysqlclient 四个动态库文件 链接到 可执行文件 server
target_link_libraries(server pthread jsoncpp event mysqlclient)



