0
votes

When compiling some C++ code in Matlab 2016b using:

mex CXXFLAGS="\$CXXFLAGS -std=c++11 -fopenmp" CXXOPTIMFLAGS='\$CXXOPTIMFLAGS -Ofast -DNDEBUG mexMyFunction.cpp

I got the following errors:

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

System: Ubuntu 16.04, g++ version: 5.4.0.

If I remove -fopenmp from the above command then it worked fine. Compiling directly with g++ (without mex) also worked.

Could you please help me to resolve this?

Thank you so much in advance!

1
I don't know how mex works, but you also have to add -fopenmp to the linking step.Zulan
@Zulan Thanks a lot! I've figured it out.Khue

1 Answers

1
votes

The the following linking flags were missing:

LDOPTIMFLAGS="$LDOPTIMFLAGS -fopenmp -O2" -lgomp 

Complete command:

mex CXXFLAGS="\$CXXFLAGS -std=c++11 -fopenmp" CXXOPTIMFLAGS='\$CXXOPTIMFLAGS -Ofast -DNDEBUG' LDOPTIMFLAGS="$LDOPTIMFLAGS -fopenmp -O2" -lgomp -I"/home/khue/Libs/Eigen" mexMyFunction.cpp 

Thanks to @Zulan for his suggestion.