×

Search anything:

[Fixed] undefined reference to omp_get_max_threads / omp_get_thread_num

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

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:

  1. Error: undefined reference to omp_get_max_threads / omp_get_thread_num
  2. 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.

Geoffrey Ziskovin

Geoffrey Ziskovin

Geoffrey Ziskovin is an American Software Developer and Author with an experience of over 30 years. He started his career with Haskell and has interviewed over 700 candidates for Fortune 500 companies

Read More

Improved & Reviewed by:


OpenGenus Foundation OpenGenus Foundation
[Fixed] undefined reference to omp_get_max_threads / omp_get_thread_num
Share this