Jean
Jean

Reputation: 95

Xcode: Undefined symbol: _omp_get_max_threads

I'm trying to use openMP in C with Xcode 11.4. I have installed libomp with Homebrew, and homebrew apparently installed it in: /usr/local/Cellar/libomp/10.0.0/

The code below generates a linker error: Undefined symbol: _omp_get_max_threads (likewise for get_thread_num).

#include <stdio.h>
#include "/usr/local/Cellar/libomp/10.0.0/include/omp.h"

int main () {
#pragma omp parallel
{
int threadNum = omp_get_thread_num ();
int maxThreads = omp_get_max_threads(); 
}
return 0; }

I tried adding usr/local/Cellar to the Header Search Paths, and including omp.h directly in my Xcode project, and adding compilation flag -openmp, behavior does not change.

1) I coulnd't find a way to install OpenMP through Xcode directly, would it be possible / preferable?

2) Do I need to do something further for Xcode to recognize a library installed with Homebrew?

3) Why did Homebrew install the library in usr/local/Cellar, is that a bad sign?

4) Why is the missing symbol _omp_get_max_threads and not omp_get_max_threads?

(edit): There is also under usr/local/include the same three headers, omp.h, ompt.h and omp-tools.h, as in /usr/local/Cellar/libomp/10.0.0/. Not sure why.

Upvotes: 1

Views: 1984

Answers (1)

Hristo Iliev
Hristo Iliev

Reputation: 74395

This is what you need to do to make a C or C++ project compile and link with OpenMP support in Xcode. Go to Build Settings in the project editor, select the project node and then the All settings filter. Make the following changes:

  • Header Search Paths (in the Search Paths group) - set to (or add tp the existing value) /usr/local/include, so that the compiler can find /usr/local/include/omp.h
  • Library Search Paths (in the Search Paths group) - set to (or add to the existing value) /usr/local/lib, so that the linker can find /usr/local/lib/libomp.dylib

Select the target executable node and make the following additional changes:

  • Other C Flags (in the Apple Clang - Custom Compiler Flags group) - set to (or add) -Xclang -fopenmp to enable processing of OpenMP pragmas by the compiler
  • Other Linker Flags (in the Linking group) - set to (or add) -lomp to enable linking with the OpenMP runtime library libomp.dylib

You can also make all four changes in the project node to make them project-wide or in the target node, depending on your project structure.

With C++ projects, one has to put -Xclang -fopenmp in Other C++ Flags.

Upvotes: 2

Related Questions