Open-Source Internship opportunity by OpenGenus for programmers. Apply now.
In this article, we have presented the fix for the OpenMP error undefined reference to omp_get_max_threads / omp_get_thread_num / omp_get_num_threads. The issue is with the compilation command.
Table of contents:
- Error: undefined reference to omp_get_max_threads / omp_get_thread_num
- Fix
Error: undefined reference to omp_get_max_threads / omp_get_thread_num
Run the following C++ code:
#include <stdio.h>
#include <omp.h>
int main() {
#pragma omp parallel num_threads(8)
{
int id = omp_get_thread_num();
int data = id;
int total = omp_get_num_threads();
printf("Greetings from process %d out of %d with Data %d\n", id, total, data);
}
printf("parallel for ends.\n");
return 0;
}
Command:
gcc code.c
Compilation Error:
a.o: In function `main':
code.cpp:(.text.startup+0x2): undefined reference to `omp_get_thread_num'
code.cpp:(.text.startup+0x9): undefined reference to `omp_get_num_threads'
collect2: error: ld returned 1 exit status
Fix
The issue is with the compilation command. To use OpenMP methods, one need to link OpenMP dynamically. The option to do so vary across compilers.
For GCC or G++, the compilation command will be:
gcc code.c -fopenmp
OR
g++ code.c -fopenmp
For Intel's compiler, the compilation command will be:
gcc code.c -openmp
For PGI compiler, the compilation command will be:
pgi code.c -mp
The execution command will be same:
./a.out
With this article at OpenGenus, you must have the complete idea of how to fix this undefined reference error with OpenMP.