1

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.

Jean
  • 65
  • 6
  • See my answer [here](https://stackoverflow.com/a/61871393/1374437) on how to use OpenMP with Apple's Clang compiler. Ignore that it shows `g++` in the commands - that's still Apple's Clang. The files in `/usr/local/include` are symlinks to the files in `/usr/local/Cellar/libomp/10.0.0/`. They are there because `/usr/local/include` is usually on the compiler's include path. – Hristo Iliev May 22 '20 at 11:54
  • Thank you. From a terminal, `gcc -Xclang -fopenmp main.c -lomp` does work (this is C, not C++). However I am confused as to what to do within Xcode: under `Build phases -> compile sources -> flags` I have added `-Xclang -fopenmp -lomp`, but I get a third error: `Undefined symbol: ___kmpc_fork_call`. Using as flags: `gcc -Xclang -fopenmp main.c -lomp` does not work. I see `Link Binary with Libraries`, but `lopm` is not among the options. – Jean May 22 '20 at 14:36
  • No, no, no, don't involve GCC. Just the regular Clang compiler. You need to put `-Xclang -fopenmp` in the compiler options and `-lomp` in the linker options. I don't have Xcode right now, just the command line tools, so I can't tell you exactly where to put them. I'll install it later and see. – Hristo Iliev May 22 '20 at 15:05
  • But g++ gives me a warning that I'm compiling C with a C++ compiler? Thank you for that, there is a field called `Other Linker Flags` under `Buid Settings`, but adding `-lomp` there (with only `-Xclang -fopenmp` in the compiler options) produces an error: `ld: library not found for -lomp` – Jean May 22 '20 at 15:21

1 Answers1

2

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.

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
  • Let's hope Apple are going to make it easier to use OpenMP in future Xcode versions now that LLVM and Clang fully support it. – Hristo Iliev May 22 '20 at 18:15