Build OpenMP C++ Program using CMake

Build OpenMP C++ Program using CMake

Parallelizing the C++ programs can significantly boost performance, especially when dealing with computationally intensive tasks. OpenMP (Open Multi-Processing) is a widely used API for achieving parallelism in C, C++, and Fortran programs. Combining OpenMP with CMake, a versatile build system, provides a seamless way to manage the project and incorporate parallelism. This tutorial explains how to build OpenMP C++ program using CMake.

Prepare environment

Prior to getting started, ensure that you have installed both CMake and a C++ compiler with OpenMP support, such as g++.

Build OpenMP C++ program

  • Step 1: Create a new C++ project

Create a new directory for OpenMP C++ project. For instance, let's name the directory test_openmp for a straightforward project that parallelizes a print statement. Once the directory is set up, proceed to navigate into it.

mkdir test_openmp && cd test_openmp
  • Step 2: Create CMakeLists.txt file

Within the project directory, create a CMakeLists.txt file. This file will encompass the directives for CMake to configure and generate the necessary build scripts.

test_openmp/CMakeLists.txt

cmake_minimum_required(VERSION 3.27)
project(myapp)

set(CMAKE_CXX_STANDARD 17)

find_package(OpenMP REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} OpenMP::OpenMP_CXX)

The find_package command is used to locate the OpenMP package. It ensures that OpenMP is installed on the system. The REQUIRED keyword indicates that if OpenMP is not found, the CMake configuration process will terminate with an error.

The add_executable command designates the source files to be compiled, generating an executable.

The target_link_libraries command links the executable target with the OpenMP library.

  • Step 3: Create source file

Inside the project directory, create main.cpp source file.

test_openmp/main.cpp

#include <iostream>
#include <omp.h>

int main()
{
#pragma omp parallel default(none)
    {
        printf("Thread = %d\n", omp_get_thread_num());
    }

    return 0;
}

This example serves as a basic illustration of parallel programming, where multiple threads execute the specified code concurrently, each displaying its associated thread number.

  • Step 4: Configure the project with CMake

In the project directory, execute the following CMake command to generate the build scripts and configure the project.

cmake -S . -B build

The -S option designates the source directory, which, in this case, is the current directory. The -B option specifies the build directory, here named build.

  • Step 5: Build the project with CMake

Run the following CMake command to build a project.

cmake --build build

The --build option tells CMake to build in the specified directory.

  • Step 6: Run the program

After a successful build of the project, execute the program using the following command:

./build/myapp

The output might look like this:

Thread = 10
Thread = 8
Thread = 2
Thread = 7
Thread = 0
...

Leave a Comment

Cancel reply

Your email address will not be published.