Lang Zhou
Lang Zhou

Reputation: 93

Proper way to use omp.h in macOS

When trying to compile ASIFT algorithm from C++ source on macOS, I encountered problems with OpenMP library.

The compiler is Apple Clang, macOS version is 11.3.

First the compiler told me that "omp.h" can't be found. I refer to this question and installed libomp through HomeBrew. After proper installation, I change the following code in source file:

#include "omp.h"

to:

#include "/usr/local/opt/libomp/include/omp.h"

This resolves the previous "omp.h not found" error.

However, the project source can still not be compiled properly after executing make command. Clang throw a linker error. The terminal output is:

Undefined symbols for architecture x86_64:
  "_omp_get_num_procs", referenced from:
      compute_asift_matches(int, int, int, int, int, int, int, std::__1::vector<std::__1::vector<std::__1::vector<keypoint, std::__1::allocator<keypoint> >, std::__1::allocator<std::__1::vector<keypoint, std::__1::allocator<keypoint> > > >, std::__1::allocator<std::__1::vector<std::__1::vector<keypoint, std::__1::allocator<keypoint> >, std::__1::allocator<std::__1::vector<keypoint, std::__1::allocator<keypoint> > > > > >&, std::__1::vector<std::__1::vector<std::__1::vector<keypoint, std::__1::allocator<keypoint> >, std::__1::allocator<std::__1::vector<keypoint, std::__1::allocator<keypoint> > > >, std::__1::allocator<std::__1::vector<std::__1::vector<keypoint, std::__1::allocator<keypoint> >, std::__1::allocator<std::__1::vector<keypoint, std::__1::allocator<keypoint> > > > > >&, std::__1::vector<std::__1::pair<keypoint, keypoint>, std::__1::allocator<std::__1::pair<keypoint, keypoint> > >&, siftPar&) in compute_asift_matches.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [demo_ASIFT] Error 1

I also write a much simpler code in Xcode just for test:

#include "/usr/local/opt/libomp/include/omp.h"
#include <iostream>

int main()
{
    int num = omp_get_num_procs();
    std::cout<<num<<std::endl;
}

The code can't be compiled properly.

It seems that the compiler can't find implementation of the function omp_get_num_procs() declared in omp.h.

For reference, here is the declaration of omp_get_num_procs() in omp.h (line 69):

extern int    __KAI_KMPC_CONVENTION  omp_get_num_procs    (void);

I'm not sure whether it's a compiler parameter issue or OpenMP wasn't installed properly. I have confirmed that libomp.a and libomp.dylib are placed correctly in /usr/local/opt/libomp/lib directory.

Can anyone kindly give some advice on this issue? Thanks.

Upvotes: 4

Views: 1111

Answers (2)

Jim Cownie
Jim Cownie

Reputation: 2859

The Apple compilers do not (at least in public) support OpenMP. The necessary headers and libraries are missing for a reason!

Your best bet is therefore to install a compiler which does (using brew, or macPorts depending on how you are supporting other tools which don't come from Apple).

To answer your specific question: "Proper way to use omp.h in macOS", the answer is "Install a compiler that supports OpenMP."

Upvotes: 1

Borys
Borys

Reputation: 61

Probably, you should link libomp.a to a project. Here example how to link static lib to Xcode C++ project.

Upvotes: 1

Related Questions