CMake的所有的语句都写在一个叫 CMakeLists.txt 的文件中。当 CMakeLists.txt 文件确定后,可以用 ccmake 命令对相关的变量值进行配置。这个命令必须指向 CMakeLists.txt 所在的目录。配置完成之后,应用 cmake 命令生成相应的 makefile。
一、项目的目录结构
- src:源文件
- inc:头文件
- test:测试文件,主函数入口
- proto:proto文件
- log:项目产生的日志文件
- build:在此文件夹中构建项目,不污染源文件
- bin:生成的可执行文件
- build.sh:cmake的脚本
二、实例说明Cmake编写规则
src文件下的CMakeLists.txt :
# 指定CMake编译最低要求版本
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
# 将C++文件赋值给变量SRC_LIST_Server
SET(SRC_LIST_Server
ChatServer.cpp
ChatSystem.pb.cc
ChatStatus.pb.cc
Mysql.cpp
Redis.cpp
)
SET(SRC_LIST_Client
ChatClient.cpp
ChatSystem.pb.cc
ChatStatus.pb.cc
Mysql.cpp
Redis.cpp
)
# 设置编译选项
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lpthread -g -Wall")
# 查找protobuf头文件
# find_package(Protobuf REQUIRED)
# if(PROTOBUF_FOUND)
# message(STATUS "protobuf library found")
# else()
# message(FATAL_ERROR "protobuf library is needed but cant be found")
# endif()
#include_directories(${PROTOBUF_INCLUDE_DIRS})
# 指定头文件和库文件目录
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/inc)
# 指定生成库文件的目录
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
INCLUDE_DIRECTORIES(/usr/local/include/google/protobuf)
INCLUDE_DIRECTORIES(/usr/local/boost/include)
INCLUDE_DIRECTORIES(/usr/include/mysql)
INCLUDE_DIRECTORIES(/usr/local/include/hiredis)
INCLUDE_DIRECTORIES(/usr/local/include/glog)
LINK_DIRECTORIES(/usr/local/boost/lib)
# 生成库文件,默认生成静态库,SHARED指定生成库类型为动态库
ADD_LIBRARY(chatserver SHARED ${SRC_LIST_Server})
ADD_LIBRARY(chatclient SHARED ${SRC_LIST_Client})
# 需要链接的库文件
TARGET_LINK_LIBRARIES(chatserver
protobuf
hiredis
mysqlclient
glog
)
TARGET_LINK_LIBRARIES(chatclient
protobuf
hiredis
mysqlclient
glog
)
test文件下的CMakeLists.txt :
# 指定CMake编译最低要求版本
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
# 将Server.cpp赋值给Server变量
SET(Server Server.cpp)
SET(Client Client.cpp)
# 设置编译选项
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lpthread -g -Wall")
# 设置生成的执行文件的输出目录
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/lib)
INCLUDE_DIRECTORIES(/usr/local/include/google/protobuf)
INCLUDE_DIRECTORIES(/usr/local/boost/include)
INCLUDE_DIRECTORIES(/usr/include/mysql)
INCLUDE_DIRECTORIES(/usr/local/include/hiredis)
INCLUDE_DIRECTORIES(/usr/local/include/glog)
LINK_DIRECTORIES(/usr/local/boost/lib)
# 生成可执行文件server,依赖于Server.cpp
ADD_EXECUTABLE(server ${Server})
ADD_EXECUTABLE(client ${Client})
# 指定server的链接库chatserver和protobuf
TARGET_LINK_LIBRARIES(server chatserver)
TARGET_LINK_LIBRARIES(client chatclient)
【指令说明】:
- CMAKE_MINIMUM_REQUIRED:声明Cmake版本,如果低于指定版本则会停止处理工程文件,并报告错误。
- SET:定义变量并赋值。
- CMAKE_CXX_FLAGS:设置编译选项。
- PROJECT_SOURCE_DIR:预定义的变量。
- ${}:取值。
- INCLUDE_DIRECTORIES:向工程添加多个特定的头文件搜索路径,路径之间用空格分隔。
- LINK_DIRECTORIES:添加非标准的共享库搜索路径。
- ADD_LIBRARY:生成动态库或静态库。
- TARGET_LINK_LIBRARIES:为TARGET添加需要链接的共享库。
- ADD_EXECUTABLE:生成可执行文件。
【运行说明】:
#!bin/bash
protoc -I=./proto/ --cpp_out=./proto ./proto/ChatSystem.proto
mv ./proto/ChatSystem.pb.h ./inc/ChatSystem.pb.h
mv ./proto/ChatSystem.pb.cc ./src/ChatSystem.pb.cc
if [ ! -e ./build ]
then
mkdir build
else
rm -rf build/*
fi
if [ ! -e ./bin ]
then mkdir bin
else rm -rf bin/*
fi
if [ ! -e ./log ]
then mkdir log
else rm -rf log/*
fi
if [ ! -e ./lib ]
then mkdir lib
else rm -rf lib/*
fi
cd build
cmake ..
make
参考:https://github.com/carl-wang-cn/demo/tree/master/cmake