Compile and Run OpenMP Programs with GCC and Clang
Build and run OpenMP C programs with GCC 14 and Clang 19 on Linux, set the thread count, check that the pragma took effect, and fix the common link errors.

An OpenMP program fails in two quiet ways. The compiler can ignore your pragmas and hand you a correct serial program that never uses a second core, or the link step can fail because the runtime library is missing. Both come from the same place: OpenMP is a language extension plus a runtime library, and the build has to wire up both halves. This post covers GCC 14 and Clang 19 on Linux with OpenMP 4.5 and 5.x features, shows how to confirm the pragma took effect, and lists the errors you will hit and what each one means. If you want the loop side first, parallelizing a loop with a pragma is the companion.
One program to build
Everything below uses this file. It reports how many threads ran and which OpenMP version the compiler implements.
#include <stdio.h>
#include <omp.h>
int main(void) {
printf("_OPENMP = %d\n", _OPENMP);
#pragma omp parallel num_threads(4)
{
#pragma omp single
printf("team size = %d\n", omp_get_num_threads());
}
return 0;
}_OPENMP is a macro the compiler defines only when OpenMP is enabled. Its value is the year and month of the specification the compiler implements: 201511 means OpenMP 4.5, 201811 means 5.0, 202011 means 5.1. The single construct lets exactly one thread print, so the output is one line instead of four.
Build with GCC
GCC needs one flag, and the flag does two jobs: it enables the pragmas at compile time and links the runtime, libgomp, at link time.
gcc -fopenmp -O2 -Wall hello-omp.c -o hello-omp
./hello-omp_OPENMP = 201511
team size = 4GCC 14 reports 201511, which is OpenMP 4.5, with much of 5.0 and 5.1 implemented incrementally. The GCC project tracks which features of each OpenMP version it supports on its OpenMP implementation status page, and that page is the authority for whether a specific clause works in your compiler. Do not assume a clause exists because the spec lists it.
If you compile and link in separate steps, pass -fopenmp to both. The compile step needs it to recognize the pragmas, and the link step needs it to add -lgomp and the thread library.
gcc -fopenmp -O2 -c hello-omp.c -o hello-omp.o
gcc -fopenmp hello-omp.o -o hello-ompA common mistake is gcc -O2 -c file.c followed by gcc file.o -o prog with -fopenmp only on the first. That links with an undefined reference to GOMP_parallel.
Build with Clang
Clang uses the same flag name. The difference is the runtime: Clang links LLVM's libomp, which ships separately from the compiler on many distributions.
clang -fopenmp -O2 -Wall hello-omp.c -o hello-omp
./hello-omp_OPENMP = 201811
team size = 4Clang 19 reports 201811 (OpenMP 5.0) as its default. If the build stops with fatal error: 'omp.h' file not found, the compiler was installed without the OpenMP runtime package. On Debian and Ubuntu that package is libomp-dev, on Fedora it is libomp-devel. Check what the runtime thinks it is with clang -fopenmp -v and look at the link line it prints.
On macOS the Apple-supplied clang does not accept -fopenmp directly. You must pass -Xpreprocessor -fopenmp and link -lomp from a Homebrew libomp install. This post is Linux-only, so treat that as a pointer, not a recipe.
Prove the pragma took effect
A build that silently ignored your pragmas is the most expensive failure, because everything works and nothing speeds up. There are three checks, in order of cost.
First, ask the compiler. Both GCC and Clang accept -fopenmp-simd for SIMD-only pragmas, but the macro check is the cheap test: put #ifdef _OPENMP around a message or an #error.
#ifndef _OPENMP
#error "built without -fopenmp: pragmas are being ignored"
#endif
int main(void) { return 0; }check-omp.c:2:2: error: #error "built without -fopenmp: pragmas are being ignored"Second, turn on the diagnostics that name an unknown pragma. By default a misspelled pragma is skipped with no complaint, so #pragma omp paralel for produces serial code. Compile with -Wall, which includes -Wunknown-pragmas, and the typo gets reported.
int main(void) {
int a[4];
#pragma omp paralel for
for (int i = 0; i < 4; i++) a[i] = i;
return a[3] - 3;
}typo.c:3: warning: ignoring '#pragma omp paralel' [-Wunknown-pragmas]That wording is GCC's. Clang also diagnoses an unrecognized directive when -fopenmp is on, in its own words. Treat the GCC warning as an error in your build with -Werror=unknown-pragmas and the typo can never ship.
Third, look at the runtime. The runtime can print its own configuration if you set OMP_DISPLAY_ENV.
OMP_DISPLAY_ENV=true ./hello-ompOPENMP DISPLAY ENVIRONMENT BEGIN
_OPENMP = '201511'
OMP_DYNAMIC = 'FALSE'
OMP_NESTED = 'FALSE'
OMP_NUM_THREADS = '16'
OMP_SCHEDULE = 'DYNAMIC'
OMP_PROC_BIND = 'FALSE'
OMP_PLACES = ''
OMP_STACKSIZE = '0'
OMP_WAIT_POLICY = 'PASSIVE'
OPENMP DISPLAY ENVIRONMENT END
_OPENMP = 201511
team size = 4The exact block depends on the runtime version and the machine, and the thread count shown is the machine's logical CPU count, which will differ from the example. The lines worth reading are OMP_NUM_THREADS and OMP_PROC_BIND. This is the fastest way to see what a CI container actually got.
Control the thread count
Three mechanisms set it, and they have a fixed priority. The num_threads clause on a single construct wins over omp_set_num_threads(), which wins over the OMP_NUM_THREADS environment variable, which wins over the default.
#include <stdio.h>
#include <omp.h>
int main(void) {
printf("max threads = %d\n", omp_get_max_threads());
omp_set_num_threads(2);
#pragma omp parallel
{
#pragma omp single
printf("after set: %d\n", omp_get_num_threads());
}
#pragma omp parallel num_threads(3)
{
#pragma omp single
printf("with clause: %d\n", omp_get_num_threads());
}
return 0;
}OMP_NUM_THREADS=8 ./threadsmax threads = 8
after set: 2
with clause: 3Setting the count from the environment is the right habit for benchmarks and CI, because you change it without recompiling. Hard-coding num_threads(4) in a library function means a caller on a 64-core machine cannot use the cores.
Errors you will meet
| Error text | Cause | Fix |
|---|---|---|
undefined reference to 'GOMP_parallel' | Linked without -fopenmp | Add -fopenmp to the link command |
undefined reference to 'omp_get_thread_num' | Called a runtime function, linked without the runtime | Same, or add -lgomp |
fatal error: omp.h: No such file or directory | No OpenMP headers for this compiler | Install libomp-dev for Clang; GCC ships omp.h |
| Program runs on one core | Pragma ignored or OMP_NUM_THREADS=1 | -Wall, OMP_DISPLAY_ENV=true |
Mixing runtimes is the one that bites after the build works. Linking a binary with GCC's libgomp and a shared library built against LLVM's libomp puts two thread pools in one process. They compete for cores, and the symptom is a slowdown that disappears when you drop one library. Build everything in a process against one runtime.
Make it part of the build file
A Makefile that gets it right once saves you from the ordering mistake above.
CC ?= gcc
CFLAGS ?= -O2 -Wall -Wextra -Werror=unknown-pragmas -fopenmp
LDFLAGS ?= -fopenmp
hello-omp: hello-omp.o
$(CC) $(LDFLAGS) $^ -o $@
hello-omp.o: hello-omp.c
$(CC) $(CFLAGS) -c $< -o $@Switching to Clang is make CC=clang. The point of putting -fopenmp into LDFLAGS explicitly is that it makes the link step carry the flag, which is the half people forget. With CMake the equivalent is find_package(OpenMP REQUIRED) and target_link_libraries(app PRIVATE OpenMP::OpenMP_C), which adds the flag to both steps for whichever compiler it finds.
Flags for debug and release builds
Treat the OpenMP flag as one part of a larger set. For development, build with -O1 -g -fopenmp, which keeps stack traces and source lines readable when a debugger or a race detector looks at the threads. For release, -O2 -fopenmp is the usual choice, and -O3 helps loops that vectorize. Be careful with -ffast-math: it lets the compiler reorder floating-point arithmetic, and inside a parallel reduction that compounds the reordering the parallel version already does, so results can drift further from the serial ones than you expect. If you want only the vectorization pragmas and no threads, -fopenmp-simd enables #pragma omp simd while ignoring the thread-level directives, which is a clean way to ship a library that must not create threads behind its caller's back.
A last practical rule is to keep one build directory per configuration. Mixing objects compiled with and without -fopenmp links fine and behaves in confusing ways, since the unflagged objects ignore their pragmas while the flagged ones call into the runtime. A clean rebuild after changing the flag costs seconds and saves an afternoon.


