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
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.
如果要在单独的路径中创建库,则可以使用此变量将所有创建的库放在其中。
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编译