linux cmake编译_在Linux中构建和编译的CMake教程

本文介绍了Linux环境下使用CMake进行构建和编译的方法,包括CMake的安装、基本命令、全局变量以及如何配置和使用CMake编译项目。
摘要由CSDN通过智能技术生成
linux cmake编译

linux cmake编译

Binaries are created by building or compiling sources like C, C++, etc. In simple applications, we can build by using the compiler like GCC directly. But this is inconvenient if the application is big and has a lot of source code, configuration file, and build options. Developers generally prefer to build systems like make but there is an alternative named CMake which is popular in the Linux ecosystem too.

二进制文件是通过构建或编译C,C ++等源代码来创建的。在简单的应用程序中,我们可以直接使用GCC之类的编译器进行构建。 但是,如果应用程序很大并且具有很多源代码,配置文件和构建选项,那么这将很不方便。 开发人员通常更喜欢构建像make这样的系统,但是还有一个名为CMake的替代方案,它在Linux生态系统中也很流行。

关于cmake命令 (About cmake Command)

CMake is an extensible and opensource build manage software.

CMake是一个可扩展的开源构建管理软件。

cmake安装 (cmake Installation)

We can install CMake for different Linux distributions with the following commands.

我们可以使用以下命令为不同Linux发行版安装CMake。

Ubuntu,Debian,Mint,Kali (Ubuntu, Debian, Mint, Kali)

$ sudo apt-get install cmake
CMake Installation
CMake Installation
CMake安装

Fedora,CentOS,RHEL(Fedora, CentOS, RHEL)

$ sudo yum install cmake

应用范例 (Example Application)

In order to compile an application, we need some source code. We will use the following source code which is written in C++ and have very basic logic. But as stated before CMake is designed to be used complex build processes.

为了编译应用程序,我们需要一些源代码。 我们将使用以下用C ++编写的源代码,它们具有非常基本的逻辑。 但是如前所述,CMake旨在用于复杂的构建过程。

#include <iostream>

using namespace std;

int main(void) {

  cout << "Hello World" << endl;

  return(0);
}

cmake全局变量 (cmake Global Variables)

CMake is a very extensible and flexible build system where we can specify a lot of different options and variables. We will explain some of them below.

CMake是一个非常可扩展且灵活的构建系统,我们可以在其中指定许多不同的选项和变量。 我们将在下面解释其中的一些。

CMAKE_BINARY_DIR (CMAKE_BINARY_DIR)

This variable is used to specify the binary files directory which is generally same as CMAKE_SOURCE_DIR .

此变量用于指定二进制文件目录,该目录通常与CMAKE_SOURCE_DIR相同。

CMAKE_SOURCE_DIR (CMAKE_SOURCE_DIR)

This variable is used to specify the source directory where the source code and other related configurations reside.

此变量用于指定源代码和其他相关配置所在的源目录。

EXECUTABLE_OUTPUT_PATH (EXECUTABLE_OUTPUT_PATH)

After the compile operation the created executable file will be written to the specified directory.

编译操作后,创建的可执行文件将被写入指定的目录。

LIBRARY_OUTPUT_PATH (LIBRARY_OUTPUT_PATH)

If we want to create libraries in a separate path we can use this variable where all created libraries will be put.

如果要在单独的路径中创建库,则可以使用此变量将所有创建的库放在其中。

LEARN MORE  How To Use GCC Compiler Options Like Optimization, Flags?
了解更多信息如何使用GCC编译器选项(如优化,标志)?

CMake配置示例 (Example CMake Configuration)

We will create a sample configuration file to build a given example named app.cpp . We will use the following configuration which is very simple. We will name the configuration CMakeList.txt and this file will be automatically interpreted by CMake.

我们将创建一个示例配置文件,以构建名为app.cpp的给定示例。 我们将使用以下非常简单的配置。 我们将命名配置CMakeList.txt,此文件将由CMake自动解释。

cmake_minimum_required(VERSION 3.9)

add_executable(hello ${PROJECT_SOURCE_DIR}/app.cpp)

用Cmake编译 (Build with Cmake)

We will run cmake command and provide the directory path where CMakeLists.txt configuration file resides.

我们将运行cmake命令并提供CMakeLists.txt配置文件所在的目录路径。

$ cmake .

翻译自: https://www.poftut.com/cmake-tutorial-to-build-and-compile-in-linux/

linux cmake编译

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值