其他分享
首页 > 其他分享> > CMake 常用用法

CMake 常用用法

作者:互联网

目录

环境

Debian 9
cmake version 3.7.2

CMakeLists.txt

cmake 主要通过 CMakeLists.txt 文件生成编译所需文件,子目录也需包含 CMakeLists.txt 文件。废话不多说,直接上 CMakeLists.txt 文件中的常用内容

cmake_minimum_required (VERSION 2.8)

set(PRO_NAME cmake_demo)

project (${PRO_NAME})

# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)

#这里不使用CMAKE_BUILD_TYPE ,自定义make_type 来修改编译参数
#set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_FLAGS "-Wall -static")

set(make_type Release1)

if(${make_type} STREQUAL Release1)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3  -DNDEBUG")
	message(STATUS "CMAKE_C_FLAGS or CMAKE_CXX_FLAGS : ${CMAKE_CXX_FLAGS}")
elseif(${make_type} STREQUAL Release2)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Dctest")
	message(STATUS "CMAKE_C_FLAGS or CMAKE_CXX_FLAGS : ${CMAKE_CXX_FLAGS}")
elseif(${make_type} STREQUAL Debug)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
	message(STATUS "CMAKE_C_FLAGS or CMAKE_CXX_FLAGS : ${CMAKE_CXX_FLAGS}")
else()
	message(FATAL_ERROR "make_type configuration error.")
endif()

set(CMAKE_C_FLAGS  "${CMAKE_CXX_FLAGS}")

set(CMAKE_C_COMPILER "arm-linux-gnueabihf-gcc")
set(CMAKE_CXX_COMPILER  "arm-linux-gnueabihf-g++")

#添加头文件与链接库文件地址
message(STATUS  "${CMAKE_CURRENT_SOURCE_DIR}")
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/glog/include)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/glog/lib)

#添加子目录头文件
include_directories(./src)
# 添加 src 子目录
add_subdirectory(src)

#option可使用 ccmake 来修改其定义 ON or OFF,此处默认 ON
option(MESSAGE_IF  "MESSAGE_IF configuration" ON)
if(MESSAGE_IF)
	message(STATUS  "MESSAGE_IF is ON")
else()
	message(STATUS  "MESSAGE_IF is OFF")
endif()

# 指定生成目标
add_executable(${PRO_NAME} ${DIR_SRCS})

# 添加链接库
target_link_libraries(${PRO_NAME} 
#					-static    #此处也可定义静态编译
					cmake_test2
					-lglog  #也可以写成glog				
 					-lpthread
)

#set(CMAKE_EXE_LINKER_FLAGS "-static") #此处也可定义静态编译
#add_definitions(-DNDEBUG)  #此处也可定义部分编译参数

#修改安装位置
set(CMAKE_INSTALL_PREFIX ./output CACHE PATH  "change install path" FORCE)
#定义安装文件位置
install (TARGETS ${PRO_NAME} DESTINATION bin)
#install (FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION include)

# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_LIB_SRCS 变量
aux_source_directory(. DIR_LIB_SRCS)

# 生成链接库
add_library (cmake_test2  ${DIR_LIB_SRCS})

# 指定 cmake_test2 库的安装路径
install (TARGETS cmake_test2 DESTINATION lib)
install (FILES cmake_test2.h DESTINATION include)

文件树

├── CMakeLists.txt
├── cmake_test.cpp
├── glog
│   ├── include
│   │   └── glog
│   │       ├── export.h
│   │       ├── logging.h
│   │       ├── log_severity.h
│   │       ├── platform.h
│   │       ├── raw_logging.h
│   │       ├── stl_logging.h
│   │       └── vlog_is_on.h
│   └── lib
│       ├── cmake
│       │   └── glog
│       │       ├── glog-config.cmake
│       │       ├── glog-config-version.cmake
│       │       ├── glog-modules.cmake
│       │       ├── glog-targets.cmake
│       │       └── glog-targets-noconfig.cmake
│       ├── libglog.a
│       ├── libglog.so -> libglog.so.1
│       ├── libglog.so.0.6.0
│       ├── libglog.so.1 -> libglog.so.0.6.0
│       └── pkgconfig
│           └── libglog.pc
└── src
    ├── CMakeLists.txt
    ├── cmake_test2.cpp
    └── cmake_test2.h

其中 glog 目录为上一篇文章所编译的链接库,src 目录为子目录,定义下相关需调用的函数,cmake_test.cpp 文件所在 main 函数

cmake_test.cpp

#include <iostream>

#include  <glog/logging.h>

#include "cmake_test2.h"


int main(int argc, char **argv) {
	FLAGS_logtostderr = true;
	//FLAGS_alsologtostderr = true;

	google::InitGoogleLogging(argv[0]);
	FLAGS_log_dir = "./";

	glog_print(500);  //调用src文件夹中的函数

	LOG(INFO) << "INFO";
	LOG(WARNING) << "WARNING";
	DLOG(ERROR) << "ERROR";
	//LOG(FATAL) << "FATAL";   //执行则退出程序
#if ctest
	DCHECK(!FLAGS_logtostderr) << "DCHECK";  //检查括号内部为真,否则退出程序
#endif
	DLOG_IF(INFO, true) << "DLOG_IF";    //条件成立打印
	DCHECK_EQ(FLAGS_alsologtostderr,0) << "DCHECK_EQ";  检查括号内部相等,否则退出程序

	return 0;
}

编译安装命令

进入工程目录,使用以下命令

#使用命令生成编译目录 _build
cmake -H. -B_build 
#使用命令编译,在目录 _build 下生成对应文件
cmake --build _build/  
#将文件安装在目录 _build/output 下
cmake --build _build/ --target install 

仓库

完整测试例程在 GitHub 地址:
https://github.com/feitingfj/cmake_demo

参考

https://aiden-dong.github.io/2019/07/20/CMake%E6%95%99%E7%A8%8B%E4%B9%8BCMake%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E5%BA%94%E7%94%A8/

https://zhjwpku.com/2019/11/15/cmake-basic-commands-intro.html
https://blog.csdn.net/qq26983255/article/details/83303606
https://www.hahack.com/codes/cmake/

标签:常用,set,CMAKE,CMake,CXX,用法,FLAGS,cmake,glog
来源: https://blog.csdn.net/feitingfj/article/details/121105296