linux下cmake使用教程,cmake使用教程(一)-起步

【cmake系列使用教程】

这个系列的文章翻译自官方cmake教程:cmake tutorial。

不会仅仅停留在官方教程。本人作为一个安卓开发者,实在是没有linux c程序开发经验,望大佬们海涵。教程是在macos下完成,大部分linux我也测试过,有特殊说明的我会标注出来。本教程基于cmake-3.10.2,同时认为你已经安装好cmake。

基本语法

一个最基本的CmakeLists.txt文件最少需要包含以下三行:

cmake_minimum_required (VERSION 2.6)

project (Tutorial)

add_executable(Tutorial tutorial.cxx)

复制代码

注意:cmake的语法支持大小、小写和大小写混合上边的代码中我们使用的cmake语法是小写的.

cmake_minimum_required

CMAKE_MINIMUM_REQUIRED

cmake_MINUMUM_required

复制代码

上面三种写法是相同的,注意,只有系统指令是不区分大小写的,但是变量和字符串是区分大小写的。

创建一个tutorial.cxx文件,用来计算一个数字的平方根,内容如下:

// A simple program that computes the square root of a number

#include

#include

#include

int main (int argc, char *argv[])

{

if (argc < 2)

{

fprintf(stdout,"Usage: %s number\n",argv[0]);

return 1;

}

double inputValue = atof(argv[1]);

double outputValue = sqrt(inputValue);

fprintf(stdout,"The square root of %g is %g\n",

inputValue, outputValue);

return 0;

}

复制代码

这样就完成一个最简单的cmake程序。

构建程序

用cmake来编译这段代码,进入命令行执行内部构建命令(后边会讲外部构建):

cmake .

复制代码

这是输出一系列的log信息

-- The C compiler identification is AppleClang 9.0.0.9000039

-- The CXX compiler identification is AppleClang 9.0.0.9000039

-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc

-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works

-- Detecting C compiler ABI info

-- Detecting C compiler ABI info - done

-- Detecting C compile features

-- Detecting C compile features - done

-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++

-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works

-- Detecting CXX compiler ABI info

-- Detecting CXX compiler ABI info - done

-- Detecting CXX compile features

-- Detecting CXX compile features - done

-- Configuring done

-- Generating done

-- Build files have been written to: /Users/saka/Desktop/Tutorial/Step1

复制代码

同时生成了三个文件CMakeCache.txt、Makefile、cmake_install.cmake和一个文件夹CmakeFiles,然后执行

make

复制代码

即可生成可执行程序Tutorial。在ubuntu或者centos上可能会提示找不到math.h文件,这时候我们需要在cmakeLists.txt文件中最后添加

target_link_libraries(Tutorial apue.a)

复制代码

然后重新编译即可。需要删除刚才生成的额外的文件。

添加版本号

下面讲解如何为程序添加版本号和带有使用版本号的头文件。

set(KEY VALUE)接受两个参数,用来声明变量。在camke语法中使用KEY并不能直接取到VALUE,必须使用${KEY}这种写法来取到VALUE。

cmake_minimum_required (VERSION 2.6)

project (Tutorial)

# The version number.

set (Tutorial_VERSION_MAJOR 1)

set (Tutorial_VERSION_MINOR 0)

# configure a header file to pass some of the CMake settings

# to the source code

configure_file (

"${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"

"${PROJECT_BINARY_DIR}/TutorialConfig.h"

)

# add the binary tree to the search path for include files

# so that we will find TutorialConfig.h

include_directories("${PROJECT_BINARY_DIR}")

# add the executable

add_executable(Tutorial tutorial.cxx)

复制代码

配置文件将会被写入到可执行文件目录下,所以我们的项目必须包含这个文件夹来使用这些配置头文件。我们需要在工程目录下新建一个TutorialConfig.h.in,内容如下:

// the configured options and settings for Tutorial

#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@

#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@

复制代码

上面的代码中的@Tutorial_VERSION_MAJOR@和@Tutorial_VERSION_MINOR@将会被替换为CmakeLists.txt中的1和0。

然后修改Tutorial.cxx文件如下,用来在不输入额外参数的情况下输出版本信息:

// A simple program that computes the square root of a number

#include

#include

#include

#include "TutorialConfig.h"

int main (int argc, char *argv[])

{

if (argc < 2)

{

fprintf(stdout,"%s Version %d.%d\n",

argv[0],

Tutorial_VERSION_MAJOR,

Tutorial_VERSION_MINOR);

fprintf(stdout,"Usage: %s number\n",argv[0]);

return 1;

}

double inputValue = atof(argv[1]);

double outputValue = sqrt(inputValue);

fprintf(stdout,"The square root of %g is %g\n",

inputValue, outputValue);

return 0;

}

复制代码

然后执行

cmake .

make

./Tutorial

复制代码

即可看到输出内容:3b501371dd80e714af473b14df21f52a.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: CMake是一种跨平台的自动化构建工具,它可以帮助程序员在不同的操作系统和编译器上构建和测试软件项目。以下是一个简明的CMake教程: 1. 首先,安装CMake。在Linux系统上,可以使用包管理器进行安装。例如,在Ubuntu系统上,可以使用以下命令安装CMake: ``` sudo apt-get install cmake ``` 2. 创建一个新的工作目录,并进入该目录。假设该目录的名称为“my_project”。 ``` mkdir my_project cd my_project ``` 3. 在工作目录中创建一个CMakeLists.txt文件,并将以下内容添加到文件中: ``` cmake_minimum_required(VERSION 3.5) project(my_project) add_executable(my_executable main.cpp) ``` 4. 在工作目录中创建一个源代码文件main.cpp,该文件将包含应用程序的主要功能。 5. 执行以下命令生成Makefile并编译程序: ``` cmake . make ``` 6. 编译完成后,运行可执行文件: ``` ./my_executable ``` 这是一个非常简单的CMake教程,仅仅介绍了如何使用CMake构建一个简单的应用程序。对于更复杂的项目,可能需要更详细的CMake配置。 ### 回答2: CMake是一种跨平台的编译程序,它可以自动生成Makefile和Visual Studio工程文件,支持Windows、Mac OS X和Linux等操作系统。本文将简要介绍在Linux下使用CMake的方法。 1. 安装CMakeLinux下,你可以通过包管理器来安装CMake。 在Debian/Ubuntu系统中安装CMake sudo apt-get install cmake 在Fedora系统中安装CMake sudo dnf install cmake 在CentOS系统中安装CMake sudo yum install cmake 在Arch Linux系统中安装cmake sudo pacman -S cmake 2. 编写CMakeLists.txt文件 CMakeLists.txt是CMake的配置文件,用于指示CMake如何构建项目。 在你的项目根目录下创建CMakeLists.txt文件。 下面是一个示例CMakeLists.txt文件: cmake_minimum_required(VERSION 2.8) project(myproject) set(CMAKE_CXX_STANDARD 11) add_executable(myapp main.cpp) 3. 构建项目 在终端中,使用cd命令进入到你的项目目录中,然后创建build目录,在build目录下执行cmake命令。 mkdir build cd build cmake .. 执行完cmake命令后,CMake会根据你的CMakeLists.txt文件生成一些Makefile或Visual Studio工程文件。 在Linux系统中,你可以使用make工具来构建项目: make 4. 运行项目 在终端中,进入到build目录下,执行生成的可执行文件即可: cd build ./myapp 这就是在Linux下使用CMake的简单教程。CMake可以帮助你轻松地构建项目,同时还支持多种操作系统和编译器平台,是一款非常方便的编译器工具。 ### 回答3: CMake是一个跨平台的自动化构建系统,提供了一种类似Makefile的配置文件,可以生成针对各种编译器的本地化makefile和其它构建系统文件。它能够简化开发和构建过程,使用户可以在多种不同的平台、编译器和操作系统上实现跨平台的构建。 CMake是在Linux中使用的非常流行的构建系统,下面是CMake的简明教程: 1.安装CMake 使用以下命令安装CMake: sudo apt-get install cmake 2.建立项目目录 在自己的工作目录下新建一个子目录,并进入该目录。 3.创建CMakeLists.txt文件 在该目录下创建一个名为CMakeLists.txt的文件,这是CMake的核心文件,它定义了项目的编译、构建过程和依赖关系等。 4.添加源文件 在CMakeLists.txt文件中添加源文件。源文件是指程序的源代码文件,包括头文件和源文件。 5.指定可执行文件名称 在CMakeLists.txt文件中指定可执行文件的名称,使用add_executable命令即可。例如: add_executable(test test.c) 6.添加库文件和依赖库 在CMakeLists.txt文件中添加需要的库文件及其依赖库,使用target_link_libraries命令即可。例如: target_link_libraries(test pthread) 7.编译项目 在项目目录下执行以下命令: mkdir build cd build cmake .. make 8.运行程序 在build目录下可以找到可执行文件,使用以下命令运行程序: ./test 总结:上述为一个简单的CMake教程,可以帮助开发者理解如何使用CMake管理项目,并且可以通过这个教程的基础上更加深入的学习CMake,在将来的开发中更加灵活、高效的使用CMake来构建项目。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值