CMake

news/发布时间2024/5/19 22:20:15

目录
  • CMake
    • Build the Smallest Project
    • Optimize the CMakeLists.txt
      • Set the Project Version
      • Configure the header File
      • Add the Compile Timestamp
      • Specify the C++ Standard
      • Add the Library
    • Make the Library Optional
    • Add the Requirements for the Use of the Library
    • Original Link

CMake

Build the Smallest Project

Create directory structure as follows:

  • Tutorial/
    • build/
    • CMakeLists.txt
    • tutorial.cpp

The files content as follows:

# CMakeLists.txt# Specify the minimum version of CMake.
cmake_minimum_required(VERSION 3.15)
# Specify the project name.
project(Tutorial)
# It's used to generate the executables
add_executable(Tutorial tutorial.cpp)
// tutorial.cpp#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>int main(int argc, char* argv[])
{if (argc < 2) {std::cout << "Usage: " << argv[0] << " number" << std::endl;return 1;}// convert input to doubleconst double inputValue = atof(argv[1]);// calculate square rootconst double outputValue = sqrt(inputValue);std::cout << "The square root of " << inputValue<< " is " << outputValue<< std::endl;return 0;
}

Enter the directory build, and execute the command cmake -G"MinGW Makefiles" ... This will generate a file called Makefile.

Execute the command cmake --build ., which calls the compiler to compile and link the project.

--build specifies the directory where the compiled files are stored, including the executable files.

The executable file Tutorial.exe are generated in the directory build, and you can execute it.

Optimize the CMakeLists.txt

Use PROJECT_NAME to replace project name in CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)
project(Tutorial)
add_executable(${PROJECT_NAME} tutorial.cpp)

We can also use a variable to represent multiple source files.

set(SRC_LIST a.cpp b.cpp c.cpp)
add_executable(${PROJECT_NAME} ${SRC_LIST})

Set the Project Version

Modify the content of CMakeLists.txt

project(Tutorial VERSION 1.0.2)

Configure the header File

Create a new file named TutorialConfig.h.in in the project root directory, and the content is as follows:

#define Tutorial_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @PROJECT_VERSION_MINOR@
#define Tutorial_VERSION_PATCH @PROJECT_VERSION_PATCH@

Add the content to CMakeList.txt as follows:

configure_file(TutorialConfig.h.in TutorialConfig.h)

TutorialConfig.h will be generated in the directory build. It depends on TutorialConfig.h.in. Therefore, it's necessary to add build to the search path list. Add the following content to the end of CMakelists.txt

target_include_directories(${PROJECT_NAME} PUBLIC${PROJECT_BINARY_DIR})

PROJECT_BINARY_DIR represents the binary path of the current project. Now it's the directory build.

Add the header TutorialConfig.h to the file tutorial.cpp, then the Macro defined in TutorialConfig.h.in can be used in tutorial.cpp, including Tutorial_VERSION_MAJOR, Tutorial_VERSION_MINOR and Tutorial_VERSION_PATCH.

Add the Compile Timestamp

Add the following content to CMakeLists.txt:

string(TIMESTAMP COMPILE_TIME %Y%m%d-%H%M%S)

Add the following content to TutorialEConfig.h.in:

#define TIMESTAMP @COMPILE_TIME@

Specify the C++ Standard

Replace atof with std::stod in the file tutorial.cpp.

Add the following content before the command add_executable to CMakeLists.txt:

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

Add the Library

Now we add a library which achieve the function of square root, and use it to replace the function provided by STL.

Create the directory named MathFunctions in the project root directory. It includes 3 files named CMakeLists.txt, MathFunctions.h and mysqrt.cpp, and the content of CMakeLists.txt is as follows:

# MathFunctions/CMakeLists.txt
add_library(MathFunctions mysqrt.cpp)

MathFunctions.h and mysqrt.cpp include a function named mysqrt, which achieves the function of square root.

To use the library MathFunctions, we need to add the following content to top level CMakeLists.txt:

add_subdirectory(MathFunctions)

What's more, in order to use MathFunctions, tutorial.cpp needs to find the library files and the corresponding header file. We can use target_link_libraries and target_include_directories to specify. Add and modify the content in the top level CMakeLists.txt as follows:

target_link_libraries(${PROJECT_NAME} PUBLIC MathFunctions)target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_BINARY_DIR}${PROJECT_SOURCE_DIR}/MathFunctions)

Add the header MathFunctions.h, then we can use mysqrt function.

Make the Library Optional

Now we'll make the library MathFunctions optional.

At first, add an option to the top level CMakeLists.txt as follows:

option(USE_MYMATH "Use tutorial provided math implementation" ON)

option represents providing options that users can choose from, and its command format is option(<variable> "description" [initial value]).

The default value of USE_MYMATH is ON, which can be changed by the user.

The next change is to make the building and linking of the MathFunctions library conditional. Modify the content of the top level CMakeLists.txt as follows:

if(USE_MYMATH)add_subdirectory(MathFunctions)list(APPEND EXTRA_LIBS MathFunctions)list(APPEND EXTRA_INCLUDES ${PROJECT_SOURCE_DIR}/MathFunctions)
endif()target_link_libraries(${PROJECT_NAME} PUBLIC ${EXTRA_LIBS})# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(${PROJECT_NAME} PUBLIC${PROJECT_BINARY_DIR}${EXTRA_INCLUDES})

Then modify the source code of tutoral.cpp as follows:

...
#ifdef USE_MYMATH#include "MathFunctions.h"
#endif
...
#ifdef USE_MYMATHconst double outputValue = mysqrt(inputValue);
#elseconst double outputValue = sqrt(inputValue);
#endif
...

Because of the using of the marco USE_MYMATH in the source code, we can add the following content to TutorialConfig.h.in:

#cmakedefine USE_MYMATH

This project calls the mysqrt function by default, we can specify USE_MYMATH to use the sqrt function of STL:

# Enter the directory build.
$ cmake -G"MinGW Makefiles" ..
$ cmake -DUSE_MYMATH=OFF ..
$ cmake --build .

Add the Requirements for the Use of the Library

Now we use a more modern method to include the header file of the MathFunctions library.

Add the below content to the CMakeLists.txt file in the directory MathFunctions:

target_include_directories(MathFunctionsINTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

Modify the top level CMakeLists.txt as follows:

...
if(USE_MYMATH)add_subdirectory(MathFunctions)list(APPEND EXTRA_LIBS MathFunctions)
endif()target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_BINARY_DIR})
...

[1]:CMake 良心教程,教你从入门到入魂
[2]:CMake Tutorial

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ulsteruni.cn/article/81008638.html

如若内容造成侵权/违法违规/事实不符,请联系编程大学网进行投诉反馈email:xxxxxxxx@qq.com,一经查实,立即删除!

相关文章

Http访问格式刨析

* 概念:Hyper Text Transfer Protocol 超文本传输协议 * 传输协议:定义了,客户端和服务器端通信时,发送数据的格式 参考火狐浏览器,其它浏览器没找到* 特点: 1. 基于TCP/IP的高级协议 2. 默认端口号:80 3. 基于请求/响应模型的:一次请求对应一次响应 4. 无状态的:每次请…

学习笔记三

一、任务详情自学教材第10章,提交学习笔记(10分) 大家学习过Python,C,Java等语言,总结一下一门程序设计语言有哪些必备的要素和技能?这些要素和技能在shell脚本中是如何呈现出来的?知识点归纳以及自己最有收获的内容 (3分)问题与解决思路(2分)实践内容与截图,代码链…

第3次软工作业

肖鹏天 3121005314 李 锷 3121005298 结对项目github链接 一、作业概述这个作业属于哪个课程 计科4班软件工程这个作业要求在哪里 结对项目这个作业的目标 结对设计实现一个自动生成小学四则运算题目的命令行程序二、PSP表格PSP2.1 Personal Software Process Stages 预估耗时(…

前端高频面试题汇总正题+(附答案解析)

正题 1、1 var length = 1;2 function fn() {3 console.log(this.length);4 }5 var obj = {6 length: 100,7 action: function (callback) {8 callback();9 arguments[0](); 10 } 11 } 12 obj.action(fn, ...[1, 2, 3, 4]);2、1 var a = 10; 2…

HIVE增量同步方案2

SELECT student_temp.id,coalesce(student_temp.age,student.age) as age ,student_temp.name,coalesce(student_temp.dt,student.dt) as dt FROM student_temp FULL OUTER JOIN student ON student_temp.id = student.id ; ———————————————— 版权声明…

jstl.jar和standard.jar

使用c标签的时候需要导入jstl.jar和standard.jar 这两个文件位于tomcat的lib目录下需要将他们导入Javaweb项目的lib目录下(lib需要自己创建)