2

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.

Lang Zhou
  • 53
  • 1
  • 3
  • 7

2 Answers2

1

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

Borys
  • 61
  • 6
  • That works! After linking libomp.a to the project, both Xcode and command line Clang compiles properly now. Thanks! – Lang Zhou Apr 15 '21 at 08:31
1

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."

Jim Cownie
  • 2,409
  • 1
  • 11
  • 20
  • Thanks. The OpenMP library installed through HomeBrew works, and Clang compiles properly after linking libomp.a to the project, as mentioned in another answer by @Borys. – Lang Zhou Apr 15 '21 at 08:28
  • It is generally safer to use a dynamic version of libomp, rather than statically link llibomp.a. (This can avoid avoid problems is you link against another library [e.g. some BLAS] which was compiled with a different OpenMP compiler...) – Jim Cownie Apr 16 '21 at 09:12
  • Thanks for the suggestion! Will further look into this problem when possible. – Lang Zhou Apr 16 '21 at 12:03