Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active June 14, 2026 20:46
Show Gist options
  • Select an option

  • Save scivision/16c2ca1dc250f54d34f1a1a35596f4a0 to your computer and use it in GitHub Desktop.

Select an option

Save scivision/16c2ca1dc250f54d34f1a1a35596f4a0 to your computer and use it in GitHub Desktop.
OpenMP with CMake, for macOS, Linux, and Windows

OpenMP simple C example with CMake

This example should 'just work' for your system.

cmake --workflow default

macOS

macOS needs OpenMP installed, for example via Homebrew:

brew install libomp

then set in ~/.zshrc

export OpenMP_ROOT=${HOMEBREW_PREFIX}/opt/libomp

export LDFLAGS="$LDFLAGS -L${OpenMP_ROOT}/lib"
cmake_minimum_required(VERSION 3.19)
project(OpenMPdemo LANGUAGES C CXX)
find_package(OpenMP COMPONENTS C CXX REQUIRED)
enable_testing()
add_executable(hello_c hello_openmp.c)
target_link_libraries(hello_c PRIVATE OpenMP::OpenMP_C)
add_test(NAME HelloC_OpenMP COMMAND hello_c)
add_executable(hello_cxx hello_openmp.cpp)
target_link_libraries(hello_cxx PRIVATE OpenMP::OpenMP_CXX)
add_test(NAME HelloCXX_OpenMP COMMAND hello_cxx)
file(GENERATE OUTPUT .gitignore CONTENT "*")
{
"version": 6,
"configurePresets": [
{
"name": "default",
"binaryDir": "build",
"generator": "Ninja",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
],
"buildPresets": [
{
"name": "default",
"configurePreset": "default",
"configuration": "Release"
}
],
"testPresets": [
{
"name": "default",
"configurePreset": "default",
"configuration": "Release",
"output": {
"outputOnFailure": true,
"verbosity": "verbose"
},
"execution": {
"noTestsAction": "error",
"scheduleRandom": true,
"stopOnFailure": false
}
}
],
"workflowPresets": [
{
"name": "default",
"displayName": "Release build",
"steps": [
{ "type": "configure", "name": "default" },
{ "type": "build", "name": "default" },
{ "type": "test", "name": "default" }
]
}
]
}
/*
// https://computing.llnl.gov/tutorials/openMP/samples/C/omp_hello.c
* FILE: omp_hello.c
* DESCRIPTION:
* OpenMP Example - Hello World - C/C++ Version
* In this simple example, the master thread forks a parallel region.
* All threads in the team obtain their unique thread number and print it.
* The master thread only prints the total number of threads. Two OpenMP
* library routines are used to obtain the number of threads and each
* thread's number.
* AUTHOR: Blaise Barney 5/99
*/
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int tid;
/* Fork a team of threads giving them their own copies of variables */
#pragma omp parallel private(tid)
{
/* Obtain thread number */
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);
/* Only master thread does this */
if (tid == 0)
{
int nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
} /* All threads join master thread and disband */
return EXIT_SUCCESS;
}
#include <omp.h>
#include <iostream>
#include <cstdlib>
int main()
{
int tid = 0;
// Fork a team of threads giving them their own copies of variables
#pragma omp parallel private(tid)
{
// Obtain thread number
tid = omp_get_thread_num();
std::cout << "Hello World from thread = " << tid << '\n';
// Only master thread does this
if (tid == 0)
{
int nthreads = omp_get_num_threads();
std::cout << "Number of threads = " << nthreads << '\n';
}
} // All threads join master thread and disband
return EXIT_SUCCESS;
}
$ cmake -B build
-- The C compiler identification is Clang 16.0.6
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Found OpenMP_C: -fopenmp=libomp (found version "5.0")
-- Found OpenMP: TRUE (found version "5.0") found components: C
-- Configuring done
-- Generating done
-- Build files have been written to: openmp_c/build
$ cmake --build build
[2/2] Linking C executable hello
$ build/hello
Hello World from thread = 0
Hello World from thread = 56
Hello World from thread = 2
Hello World from thread = 3
Hello World from thread = 6
Hello World from thread = 12
Hello World from thread = 26
Hello World from thread = 19
Hello World from thread = 20
Hello World from thread = 31
Hello World from thread = 14
Hello World from thread = 36
Hello World from thread = 48
Number of threads = 64
<... more messages from other threads>
% cmake -Bbuild --fresh
-- The C compiler identification is AppleClang 14.0.3.14030022
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Found OpenMP_C: -Xclang -fopenmp (found version "5.0")
-- Found OpenMP: TRUE (found version "5.0") found components: C
-- Configuring done (0.4s)
-- Generating done (0.0s)
-- Build files have been written to: openmp_c/build
% cmake --build build
[2/2] Linking C executable hello
% build/hello
Hello World from thread = 0
Hello World from thread = 5
Hello World from thread = 4
Hello World from thread = 2
Hello World from thread = 7
Hello World from thread = 1
Hello World from thread = 6
Number of threads = 8
Hello World from thread = 3
@ProExpertProg

Copy link
Copy Markdown

Works great, thanks!

@mattgreen

Copy link
Copy Markdown

Thank you!

@thewh1teagle

Copy link
Copy Markdown

Thank you!

@usptact

usptact commented Jan 28, 2025

Copy link
Copy Markdown

Great! Thanks a lot!

@cjxie

cjxie commented Mar 14, 2025

Copy link
Copy Markdown

It works. Thanks!

@hidal00p

Copy link
Copy Markdown

Life saver!

@devprabal

Copy link
Copy Markdown

thank you, it works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment