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

int main()
{
  #pragma omp parallel
  std::cout << "Hello from thread "
            << omp_get_thread_num() << ", "
            << "nthreads "
            << omp_get_num_threads()
            << std::endl;

  return 0;
}

I include omp.h, but still says:

  • undefined reference to `omp_get_num_threads'
  • undefined reference to `omp_get_thread_num'

I use g++ 4.71, why still fail to link it?

user3235530
  • 21
  • 1
  • 1
  • I should be more specific, I use code blocks as IDE – user3235530 Jan 25 '14 at 16:31
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – PlasmaHH Jan 25 '14 at 17:15
  • this is about parallel execution, little different, I don't think this is duplicate – user3235530 Jan 25 '14 at 18:43

1 Answers1

2

add -fopenmp compiler parameter, here is how I use it on g++:

g++-4.8 -std=c++11 -O2 -Wall -pedantic -fopenmp -pthread main.cpp && ./a.out
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • @user3235530 if I remember correctly, then you need to right-click on the `project icon` in the `project manager` and in the window that pops up, in the `other options` tab in `compiler settings`, you can type additional command line options you need – jcxz Jan 25 '14 at 16:35
  • the error disappeared, but then it says libgomp.spec no such file or directory – user3235530 Jan 25 '14 at 16:51
  • looks like missing libs or some other compiler/ide configuration failue, you cam experiment with OMP with online compiler: http://coliru.stacked-crooked.com/a/1d4aefce4a9b48b9. – marcinj Jan 25 '14 at 16:56