使用gcc 4.9在Mac上不能使用OpenMP

时间:2014-11-03 22:44:37

标签: macos gcc openmp gcc4.9

OpenMP网站说:“GCC 4.9支持OpenMP 4.0 for C / C ++”。

我正在使用brew中的gcc 4.9.1,但是当我尝试编译liblinear时,我看到了这个错误:omp.h file not found

具体做法是:

Compiling liblinear version 1.93
Source code page:
   http://www.csie.ntu.edu.tw/~cjlin/liblinear/
external/liblinear-1.93_multicore/matlab/train.cpp:7:10: fatal error: 'omp.h' file not found
#include <omp.h>
         ^
1 error generated.

    mex: compile of ' "external/liblinear-1.93_multicore/matlab/train.cpp"' failed.

这是用于编译liblinear的matlab代码,其中包含一个包含#include <omp.h>的文件:

% Compile liblinear
if ~exist('liblinear_train')
  fprintf('Compiling liblinear version 1.93\n');
  fprintf('Source code page:\n');
  fprintf('   http://www.csie.ntu.edu.tw/~cjlin/liblinear/\n');

  mex -outdir bin ...
      COMPFLAGS="$COMPFLAGS -fopenmp" -largeArrayDims ...
      external/liblinear-1.93_multicore/matlab/train.cpp ...
      external/liblinear-1.93_multicore/matlab/linear_model_matlab.cpp ...
      external/liblinear-1.93_multicore/linear.cpp ...
      external/liblinear-1.93_multicore/tron.cpp ...
      "external/liblinear-1.93_multicore/blas/*.c" ...
      -output liblinear_train;
end`

更新

我在mexopts.sh中更改了gcc版本(旁注:我将其从/Applications/MATLAB_R2013a_Student.app/bin/mexopts.sh复制到~/.matlab/R2013a)。具体来说,我将CC=xcrun -sdk macosx10.9 clang更改为CC='gcc-4.9'

我认为Matlab确实使用了这个编译器,因为当我运行这段代码时:

if ~exist('anigauss')
    fprintf('Compiling the anisotropic gauss filtering of:\n');
    fprintf('   J. Geusebroek, A. Smeulders, and J. van de Weijer\n');
    fprintf('   Fast anisotropic gauss filtering\n');
    fprintf('   IEEE Transactions on Image Processing, 2003\n');
    fprintf('Source code/Project page:\n');
    fprintf('   http://staff.science.uva.nl/~mark/downloads.html#anigauss\n\n');
    mex -Dchar16_t=uint16_T -outdir bin ...
        selective_search/SelectiveSearchCodeIJCV/Dependencies/anigaussm/anigauss_mex.c ...
        selective_search/SelectiveSearchCodeIJCV/Dependencies/anigaussm/anigauss.c ...
        -output anigauss
end

Matlab打印:

dyld: Library not loaded: /usr/local/opt/mpfr2/lib/libmpfr.1.dylib
  Referenced from: /usr/local/Cellar/gcc49/4.9.1/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.1/cc1
  Reason: Incompatible library version: cc1 requires version 4.0.0 or later, but libmpfr.1.dylib provides version 3.0.0
gcc-4.9: internal compiler error: Trace/BPT trap: 5 (program cc1)
/Applications/MATLAB_R2013a_Student.app/bin/mex: line 1343: 77128 Abort trap: 6           gcc-4.9 -c -I/Applications/MATLAB_R2013a_Student.app/extern/include -I/Applications/MATLAB_R2013a_Student.app/simulink/include -DMATLAB_MEX_FILE -fno-common -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -mmacosx-version-min=10.9 -fexceptions -Dchar16_t=uint16_T -DMX_COMPAT_32 -O2 -DNDEBUG "selective_search/SelectiveSearchCodeIJCV/Dependencies/anigaussm/anigauss_mex.c" -o bin/anigauss_mex.o

    mex: compile of ' "selective_search/SelectiveSearchCodeIJCV/Dependencies/anigaussm/anigauss_mex.c"' failed.

然而,当我尝试编译liblinear时,我得到了与往常一样的错误消息。

1 个答案:

答案 0 :(得分:2)

COMPFLAGS="$COMPFLAGS /openmp" -largeArrayDims ...
                      ^^^^^^^

这可能是针对Microsoft Visual C / C ++或Windows上的Intel C / C ++编译器编写的。 Unix系统,包括OS X,传统上使用-来表示命令行标志。

要在GCC中启用OpenMP支持,您应该在编译器标志/openmp中将-fopenmp更改为COMPFLAGS


看来除了传递错误的OpenMP标志外,mex使用了错误的编译器。比较GCC和Clang的错误输出:

GCC

foo.c:1:25: fatal error: nonexistent.h: No such file or directory
 #include <nonexistent.h>
                         ^
compilation terminated.

foo.c:1:10: fatal error: 'nonexistent.h' file not found
#include <nonexistent.h>
         ^
1 error generated.

Clang,或者至少是Apple随Xcode提供的版本,不支持OpenMP。有关如何选择其他编译器的信息,请参阅mex命令的MATLAB文档。基本上,你必须执行:

mex -setup

如果MATLAB检测到几个可用的编译器,它应该能够为您提供选择其中一个编译器的能力。不幸的是,根据this table,MATLAB可能不支持OS X上的GCC(至少它没有在表中列出)。