Open In App

How to Use Cmake for Linux

Last Updated : 06 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

CMake is a command used for configuring and generating built systems for software projects. The “CMake” executable is the CMake command-line interface. It may be used to configure projects in scripts. CMake is a cross-platform build system generator. Various projects specify their build process with platform-independent CMake list files included in each directory of a source tree with the name CMakeLists.txt. Users build a project by using CMake to generate a build system for a native tool on their platform.

It is mostly used in the C and C++ programming communities but can be used for a variety of other languages and projects as well. CMake itself is a software program, so it should be invoked with the script file to interpret and generate an actual build file. CMake can generate a native build environment that will compile source code, create libraries, generate wrappers, and build executable binaries in arbitrary combinations.

Features of CMake:

  1. Platform Independence: CMake generates platform-specific build files (e.g., Makefiles for Unix-like systems, Visual Studio project files for Windows) from a single, cross-platform CMake configuration. This approach allows developers to write their build scripts once and then generate builds for various operating systems and development environments, ensuring better portability.
  2. Simplicity and Readability: CMake provides a user-friendly, human-readable scripting language for defining build configurations. CMakeLists.txt files are typically easier to write and maintain in comparison to complex and platform-specific build scripts.
  3. Modularity and Extensibility: CMake supports a modular project structure, making it simple to add new source files, libraries, or dependencies without the need to modify the entire build system. Custom functions and macros can be used to simplify intricate tasks and extend the build process.
  4. Dependency Management: CMake simplifies the management of external dependencies through features like find_package, which can automatically locate and configure required dependencies. Developers specify the libraries and components their project needs, and CMake handles the integration into the build process.
  5. Parallel Building: CMake-generated build systems can efficiently use multi-core processors, resulting in faster parallel builds. This feature can significantly reduce compilation times for extensive projects.

How to install cMake?

Step 1: Installation is done considering the Ubuntu/Debian system, however, if you have any other destro just check the installation command of your destro.

sudo apt update

This command refreshes the package information from software repositories on a Debian-based Linux system.

Refreshing Package Information

Refreshing Package Information

Step 2: Enter the following command to install ‘CMake‘ :

sudo apt install cmake -y

We can use this command regardless of -y, but if we specify -y, then it doesn’t ask for “yes/no” in between, it just proceeds considering all permissions as “yes“.

Installing CMake

Installing CMake

Step 3: To check the installation of the command, we can simply type “cmake –version“, if it says “command not found” then it’s not installed, else if we get some version number then it’s correctly installed.

cmake  --version
Cmake Version

Cmake version

How to Use Cmake for Linux?

Step 1: In the root directory of your project, create a file named CMakeLists.txt. This file will specify how to build your project.

Note: Don’t change this filename or the case of letters, it is a convention used to identify the main build configuration file for a CMake project.

Syntax:

cmake_minimum_required(VERSION 3.00)
project(project_name XX) # Put C,C++ or anything in place of XX
set(STANDARD ) # Specify standard of language in place of STANDARD
set(FLAGSANDLIBRARIES) # Specify flags and libraries of corresponding language in place of FLAGSANDLIBRARIES
include_directories(HEADER_FILES)
file(GLOB MAIN
sourcefile.extension# Specify files in the project with extension , eg : abc.c , xyz.cpp
)
add_executable(executable ${MAIN})

Step 2: Add the following code in “CMakeLists.txt” for this example by executing the command touch CMakeLists.txt. The “Touch” command creates a new file.

cmake_minimum_required(VERSION 3.00)
project(project_name C)
set(CMAKE_C_CSTANDARD 99)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread -lm")
include_directories(HEADER_FILES)
file(GLOB MAIN
"main.c"
"globals/globals.c"
"Encryption/cipher.c"
"Backend/document.c"
"nosql.c"
"DocumentHashmap/DocumentHashMap.c"
)
add_executable(executable ${MAIN})

Make a CMakeLists.txt file

Make a CMakeLists.txt file

Step 3: In your project’s root directory, create a “build” directory, it’s a good practice to create a separate build directory to keep your source directory clean. The “Mkdir” command creates a new directory.

mkdir build

Creating Build Directory

Creating Build Directory

Step 4: Go to the “build” folder and type “cmake .. ” there, Run CMake to configure the project, specifying the path to your project’s root directory (where the CMakeLists.txt is located) which is just a folder behind

cd build
cmake ..

Configuring the Project

Configuring the Project

Step 5: Now, we can view the build directory contents by executing the below command in the terminal.

ls

Listing Directory Contents

Listing Directory Contents

Step 6: Now type the ‘make‘ command in the terminal, the build tool to compile and build your project:

make

Compiling the Project

Compiling the Project

After this, the directory contents would look as follows:

ls

Listing the Directory Contents

Listing the Directory Contents

Step 7: Now execute the executable present in the directory that file would give you the desired output

./executable

Executing the script

Executing the script

Conclusion

From the article, we understood that CMake is a versatile and widely adopted open-source build system and project management tool in the Linux development ecosystem. It has a big community support and has many advantages. CMake is the go-to choice for managing and building projects in the Linux environment. It is a valuable tool for developers of all kinds of software projects, from open-source initiatives to large-scale applications.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads