Cmake Tutorial To Build and Compile In Linux

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/cmake-tutorial-to-build-and-compile-in-linux/
PINTEREST
PINTEREST
INSTAGRAM

Binaries are created by building or compiling source 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 have a lot of source code, configuration file and build options. Developers generally prefers build systems like make but there is alternative named cmake which is popular in Linux ecosystem too.

About

CMake is an extensible and opensource build manage software.

Installation

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

Ubuntu, Debian, Mint, Kali

CMake Installation
CMake Installation

Fedora, CentOS, RHEL

Example Application

In order to compile an application we need some source code. We will use 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.

Global Variables

CMake is 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_BINARY_DIR

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

CMAKE_SOURCE_DIR

This variables is used to specify the source directory where the source code and other related configurations resides.

LEARN MORE  Pip ffi.h Gcc Error

EXECUTABLE_OUTPUT_PATH

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

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.

Example CMake Configuration

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

Build with Cmake

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

Build with Cmake
Build with Cmake

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/cmake-tutorial-to-build-and-compile-in-linux/
PINTEREST
PINTEREST
INSTAGRAM