Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Compiling C++ MEX including openMP on MAC OS X 10.9 Mavericks

Subject: Compiling C++ MEX including openMP on MAC OS X 10.9 Mavericks

From: Håkon

Date: 19 Feb, 2014 19:35:08

Message: 1 of 7

I need to get some MEX functions running with openMP and I have som problems with the compiling.

I use a 2013b version of matlab on OS X 10.9, and run the gcc compiler with the tweeks described in http://www.mathworks.com/matlabcentral/answers/103904
I know the compiler works on regular C and C++ MEX-functions.

The test code I am trying to compile is the following test:
#include "mex.h"
#include <stdio.h>
#include "omp.h"

void mexFunction(int nlhs,mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
double sum;
    /* Check for proper number of arguments. */
    if(nrhs!=0) {
        mexErrMsgTxt("No input");
    } else if(nlhs!=0) {
        mexErrMsgTxt("No output");
    }
    
    omp_set_num_threads(omp_get_max_threads());
    mexPrintf("Max number of threads %d\n",omp_get_max_threads());
    
#pragma omp parallel for
    for (int i=0;i<omp_get_max_threads();i++){
    mexPrintf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads());
    }
    return;
}

When I try to compile it using the -fopenmp flag, it seems that the compiler ignores it.
Typing:
>> mex openMPtest.c CFLAGS="\$CFLAGS -fopenmp" LDFLAGS="\$LDFLAGS -fopenmp"
Gives:
clang: warning: argument unused during compilation: '-fopenmp'

Why is the -fopenmp flag ignored? I thougth Matlab would use gcc when gcc is the one asked for in Mexopts.sh, why do I get a warning from CLANG?

The program gets compiled btw. without the openMP functionality:
Output:
>> openMPtest
Max number of threads 4
Hello from thread 0, nthreads 1
Hello from thread 0, nthreads 1
Hello from thread 0, nthreads 1
Hello from thread 0, nthreads 1

Subject: Compiling C++ MEX including openMP on MAC OS X 10.9 Mavericks

From: Eric Sampson

Date: 19 Feb, 2014 21:52:08

Message: 2 of 7

"Håkon" wrote in message <le311c$imi$1@newscl01ah.mathworks.com>...
> I need to get some MEX functions running with openMP and I have som problems with the compiling.
>
> I use a 2013b version of matlab on OS X 10.9, and run the gcc compiler with the tweeks described in http://www.mathworks.com/matlabcentral/answers/103904
> I know the compiler works on regular C and C++ MEX-functions.
>
> The test code I am trying to compile is the following test:
> #include "mex.h"
> #include <stdio.h>
> #include "omp.h"
>
> void mexFunction(int nlhs,mxArray* plhs[], int nrhs, const mxArray* prhs[])
> {
> double sum;
> /* Check for proper number of arguments. */
> if(nrhs!=0) {
> mexErrMsgTxt("No input");
> } else if(nlhs!=0) {
> mexErrMsgTxt("No output");
> }
>
> omp_set_num_threads(omp_get_max_threads());
> mexPrintf("Max number of threads %d\n",omp_get_max_threads());
>
> #pragma omp parallel for
> for (int i=0;i<omp_get_max_threads();i++){
> mexPrintf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads());
> }
> return;
> }
>
> When I try to compile it using the -fopenmp flag, it seems that the compiler ignores it.
> Typing:
> >> mex openMPtest.c CFLAGS="\$CFLAGS -fopenmp" LDFLAGS="\$LDFLAGS -fopenmp"
> Gives:
> clang: warning: argument unused during compilation: '-fopenmp'
>
> Why is the -fopenmp flag ignored? I thougth Matlab would use gcc when gcc is the one asked for in Mexopts.sh, why do I get a warning from CLANG?
>
> The program gets compiled btw. without the openMP functionality:
> Output:
> >> openMPtest
> Max number of threads 4
> Hello from thread 0, nthreads 1
> Hello from thread 0, nthreads 1
> Hello from thread 0, nthreads 1
> Hello from thread 0, nthreads 1

I think you're running into a couple of simultaneous issues Håkon:
1) In OSX 10.9 when you call GCC, you're really getting the Clang compiler, see here: http://stackoverflow.com/questions/19535422/os-x-10-9-gcc-links-to-clang
2) Given point 1, the version of Clang supplied with 10.9 Xcode 5 does not support OpenMP.

You have a couple of options; a) install GCC as described in the first link, or b) build/install a version of Clang that supports OpenMP: http://clang-omp.github.io/ http://stackoverflow.com/questions/20807024/trying-to-install-openmp-clang-on-mac

Good luck :)

Subject: Solved - Thanks a lot!

From: Håkon

Date: 20 Feb, 2014 02:29:09

Message: 3 of 7

This answer was right on spot!
Thanks a lot!

I tried both the suggested solutions and encountered in both cases problems with xcode 5 and the linking of the new versions (i am not a ninja mac terminal user =) ).

However after a bit back and forth with the mexopts.sh file i was able to bypass the defualt linking to xcode's compiler.

There were very little information out, on how to tweak the mexopts.sh file, so i thought it might be interesting for others to have a look at my tweaking.

I first installed gcc-4.9.0 in the directory "/usr/local/bin/" using macports and $ sudo port install gcc49.
To bypass xcode i changed the following lines in the mexopts.sh file:
129:
# CC='xcrun -sdk macosx10.7 clang' #old
             CC='xcrun /usr/local/bin/gcc' #new
149
# CXX='xcrun -sdk macosx10.7 clang++' #old
            CXX='xcrun /usr/local/bin/g++' #new

to include the openMP library by defualt i changed the lines:
144
# CLIBS="$CLIBS -lstdc++"
              CLIBS="$CLIBS -lstdc++ -I/usr/local/lib -lgomp" #directory <-I/usr/local/lib> may vary
151
        #CXXLIBS="$MLIBS -lstdc++"
           CXXLIBS="$MLIBS -lstdc++ -I/usr/local/lib -lgomp"

I also had to change all active instances of "MacOSX10.8.sdk"/"MacOSX10.7.sdk" to "MacOSX10.9.sdk" to make the gcc-4.9.0 compiler happy... The openMP flag can be set when compiling, or it may be appended to the $CFLAGS and $CXXFLAGS in the mexopts.sh file.

A copy of my mexopts.sh file now working with Mavericks OS X 10.9 and Matlab 2013b and gcc-4.9.0 installed from macports ($ sudo port install gcc49) is placed at folk.ntnu.no/seljasen/Arbeider/GSL_and_openMP_mexopts.sh
openMP library and flag is included by default.

Again: Thank you very much Eric, for pointing out this solution!

Subject: Solved - Thanks a lot!

From: Avery Berman

Date: 25 Jun, 2014 18:10:11

Message: 4 of 7

"Håkon" wrote in message <le3p9l$jsb$1@newscl01ah.mathworks.com>...
> This answer was right on spot!
> Thanks a lot!
>
> I tried both the suggested solutions and encountered in both cases problems with xcode 5 and the linking of the new versions (i am not a ninja mac terminal user =) ).
>
> However after a bit back and forth with the mexopts.sh file i was able to bypass the defualt linking to xcode's compiler.
>
> There were very little information out, on how to tweak the mexopts.sh file, so i thought it might be interesting for others to have a look at my tweaking.
>
> I first installed gcc-4.9.0 in the directory "/usr/local/bin/" using macports and $ sudo port install gcc49.
> To bypass xcode i changed the following lines in the mexopts.sh file:
> 129:
> # CC='xcrun -sdk macosx10.7 clang' #old
> CC='xcrun /usr/local/bin/gcc' #new
> 149
> # CXX='xcrun -sdk macosx10.7 clang++' #old
> CXX='xcrun /usr/local/bin/g++' #new
>
> to include the openMP library by defualt i changed the lines:
> 144
> # CLIBS="$CLIBS -lstdc++"
> CLIBS="$CLIBS -lstdc++ -I/usr/local/lib -lgomp" #directory <-I/usr/local/lib> may vary
> 151
> #CXXLIBS="$MLIBS -lstdc++"
> CXXLIBS="$MLIBS -lstdc++ -I/usr/local/lib -lgomp"
>
> I also had to change all active instances of "MacOSX10.8.sdk"/"MacOSX10.7.sdk" to "MacOSX10.9.sdk" to make the gcc-4.9.0 compiler happy... The openMP flag can be set when compiling, or it may be appended to the $CFLAGS and $CXXFLAGS in the mexopts.sh file.
>
> A copy of my mexopts.sh file now working with Mavericks OS X 10.9 and Matlab 2013b and gcc-4.9.0 installed from macports ($ sudo port install gcc49) is placed at folk.ntnu.no/seljasen/Arbeider/GSL_and_openMP_mexopts.sh
> openMP library and flag is included by default.
>
> Again: Thank you very much Eric, for pointing out this solution!

I found this thread very helpful as well, thanks Eric and Hakon.

I would like to add that this fix also worked for me with version R2014a even though the mexopts.sh file is being phased out (as detailed in http://www.mathworks.com/matlabcentral/answers/124597-how-to-setup-gfortran-on-mac-osx-10-9-and-matlab-r2014a). I opted to use gcc-4.9 rather than the clang/OpenMP solution.

Even though R2014a has started using XML files in place of .sh/.bat, they are still allowing the use of mexopts.sh for at least another version or two. By storing the modified mexopts.sh file in ~/.matlab/R2014a/, this file is read over the XML compiler files stored in /Applications/MATLAB_R2014a.app/bin/maci64/mexopts/. So far I have not found a suggested method for updating the XML files, so if anyone knows of a way of implementing these changes in a way that is consistent with the new XML format, I would be happy to hear about those.

Subject: Solved - Thanks a lot!

From: herijunior@gmail.com

Date: 11 Jul, 2014 10:21:01

Message: 5 of 7

I know this post is a bit old but Kudos to Eric and Hakon,

I have been struggling with this issue for two days and both replies help me a lot to solve it.

My original error was:
-> xcrun -sdk macosx10.8 clang -O -arch x86_64 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -mmacosx-version-min=10.8 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2013b.app/extern/lib/maci64/mexFunction.map -fopenmp -lopencv_core -lopencv_imgproc -o "opencv_matlab_mex.mexmaci64" opencv_matlab_mex.o -L/usr/local/lib -L/Applications/MATLAB_R2013b.app/bin/maci64 -lmx -lmex -lmat -lstdc++

ld: library not found for -lgomp
clang: error: linker command failed with exit code 1 (use -v to see invocation)

    mex: link of ' "...mexmaci64"' failed.

I installed gcc 4.9 using the following tutorial: http://solarianprogrammer.com/2013/06/11/compiling-gcc-mac-os-x/

and I my mexopts.sh has the following:
CC='xcrun /usr/gcc-4.9.0/bin/gcc-4.9.0'
CLIBS="$MLIBS -lstdc++ -I/usr/gcc-4.9.0/lib/ -lgomp"

CXX='xcrun /usr/gcc-4.9.0/bin/g++-4.9.0'
CXXLIBS="$MLIBS -lstdc++ -I/usr/gcc-4.9.0/lib/ -lgomp"

Thanks again guys.

Regards,
Javier




On Wednesday, June 25, 2014 8:10:11 PM UTC+2, Avery Berman wrote:
> "Hkon" wrote in message <le3p9l$jsb$1@newscl01ah.mathworks.com>...
>
> > This answer was right on spot!
>
> > Thanks a lot!
>
> >
>
> > I tried both the suggested solutions and encountered in both cases problems with xcode 5 and the linking of the new versions (i am not a ninja mac terminal user =) ).
>
> >
>
> > However after a bit back and forth with the mexopts.sh file i was able to bypass the defualt linking to xcode's compiler.
>
> >
>
> > There were very little information out, on how to tweak the mexopts.sh file, so i thought it might be interesting for others to have a look at my tweaking.
>
> >
>
> > I first installed gcc-4.9.0 in the directory "/usr/local/bin/" using macports and $ sudo port install gcc49.
>
> > To bypass xcode i changed the following lines in the mexopts.sh file:
>
> > 129:
>
> > # CC='xcrun -sdk macosx10.7 clang' #old
>
> > CC='xcrun /usr/local/bin/gcc' #new
>
> > 149
>
> > # CXX='xcrun -sdk macosx10.7 clang++' #old
>
> > CXX='xcrun /usr/local/bin/g++' #new
>
> >
>
> > to include the openMP library by defualt i changed the lines:
>
> > 144
>
> > # CLIBS="$CLIBS -lstdc++"
>
> > CLIBS="$CLIBS -lstdc++ -I/usr/local/lib -lgomp" #directory <-I/usr/local/lib> may vary
>
> > 151
>
> > #CXXLIBS="$MLIBS -lstdc++"
>
> > CXXLIBS="$MLIBS -lstdc++ -I/usr/local/lib -lgomp"
>
> >
>
> > I also had to change all active instances of "MacOSX10.8.sdk"/"MacOSX10.7.sdk" to "MacOSX10.9.sdk" to make the gcc-4.9.0 compiler happy... The openMP flag can be set when compiling, or it may be appended to the $CFLAGS and $CXXFLAGS in the mexopts.sh file.
>
> >
>
> > A copy of my mexopts.sh file now working with Mavericks OS X 10.9 and Matlab 2013b and gcc-4.9.0 installed from macports ($ sudo port install gcc49) is placed at folk.ntnu.no/seljasen/Arbeider/GSL_and_openMP_mexopts.sh
>
> > openMP library and flag is included by default.
>
> >
>
> > Again: Thank you very much Eric, for pointing out this solution!
>
>
>
> I found this thread very helpful as well, thanks Eric and Hakon.
>
>
>
> I would like to add that this fix also worked for me with version R2014a even though the mexopts.sh file is being phased out (as detailed in http://www.mathworks.com/matlabcentral/answers/124597-how-to-setup-gfortran-on-mac-osx-10-9-and-matlab-r2014a). I opted to use gcc-4.9 rather than the clang/OpenMP solution.
>
>
>
> Even though R2014a has started using XML files in place of .sh/.bat, they are still allowing the use of mexopts.sh for at least another version or two. By storing the modified mexopts.sh file in ~/.matlab/R2014a/, this file is read over the XML compiler files stored in /Applications/MATLAB_R2014a.app/bin/maci64/mexopts/. So far I have not found a suggested method for updating the XML files, so if anyone knows of a way of implementing these changes in a way that is consistent with the new XML format, I would be happy to hear about those.

Subject: Solved - Thanks a lot!

From: herijunior@gmail.com

Date: 11 Jul, 2014 10:24:54

Message: 6 of 7

I know this post is a bit old but Kudos to Eric and Hakon,

I have been struggling with this issue for two days and both replies help me a lot to solve it.

My original error was:
-> xcrun -sdk macosx10.8 clang -O -arch x86_64 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -mmacosx-version-min=10.8 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2013b.app/extern/lib/maci64/mexFunction.map -fopenmp -lopencv_core -lopencv_imgproc -o "opencv_matlab_mex.mexmaci64" opencv_matlab_mex.o -L/usr/local/lib -L/Applications/MATLAB_R2013b.app/bin/maci64 -lmx -lmex -lmat -lstdc++

ld: library not found for -lgomp
clang: error: linker command failed with exit code 1 (use -v to see invocation)

    mex: link of ' "...mexmaci64"' failed.

I installed gcc 4.9 using the following tutorial: http://solarianprogrammer.com/2013/06/11/compiling-gcc-mac-os-x/

and I changed my mexopts.sh with the following configuration:
CC='xcrun /usr/gcc-4.9.0/bin/gcc-4.9.0'
CLIBS="$MLIBS -lstdc++ -I/usr/gcc-4.9.0/lib/ -lgomp"

CXX='xcrun /usr/gcc-4.9.0/bin/g++-4.9.0'
CXXLIBS="$MLIBS -lstdc++ -I/usr/gcc-4.9.0/lib/ -lgomp"

Thanks again guys.

Regards,
Javier

On Thursday, February 20, 2014 3:29:09 AM UTC+1, Håkon wrote:
> This answer was right on spot!
>
> Thanks a lot!
>
>
>
> I tried both the suggested solutions and encountered in both cases problems with xcode 5 and the linking of the new versions (i am not a ninja mac terminal user =) ).
>
>
>
> However after a bit back and forth with the mexopts.sh file i was able to bypass the defualt linking to xcode's compiler.
>
>
>
> There were very little information out, on how to tweak the mexopts.sh file, so i thought it might be interesting for others to have a look at my tweaking.
>
>
>
> I first installed gcc-4.9.0 in the directory "/usr/local/bin/" using macports and $ sudo port install gcc49.
>
> To bypass xcode i changed the following lines in the mexopts.sh file:
>
> 129:
>
> # CC='xcrun -sdk macosx10.7 clang' #old
>
> CC='xcrun /usr/local/bin/gcc' #new
>
> 149
>
> # CXX='xcrun -sdk macosx10.7 clang++' #old
>
> CXX='xcrun /usr/local/bin/g++' #new
>
>
>
> to include the openMP library by defualt i changed the lines:
>
> 144
>
> # CLIBS="$CLIBS -lstdc++"
>
> CLIBS="$CLIBS -lstdc++ -I/usr/local/lib -lgomp" #directory <-I/usr/local/lib> may vary
>
> 151
>
> #CXXLIBS="$MLIBS -lstdc++"
>
> CXXLIBS="$MLIBS -lstdc++ -I/usr/local/lib -lgomp"
>
>
>
> I also had to change all active instances of "MacOSX10.8.sdk"/"MacOSX10.7.sdk" to "MacOSX10.9.sdk" to make the gcc-4.9.0 compiler happy... The openMP flag can be set when compiling, or it may be appended to the $CFLAGS and $CXXFLAGS in the mexopts.sh file.
>
>
>
> A copy of my mexopts.sh file now working with Mavericks OS X 10.9 and Matlab 2013b and gcc-4.9.0 installed from macports ($ sudo port install gcc49) is placed at folk.ntnu.no/seljasen/Arbeider/GSL_and_openMP_mexopts.sh
>
> openMP library and flag is included by default.
>
>
>
> Again: Thank you very much Eric, for pointing out this solution!

Subject: Solved - Thanks a lot!

From: Stephanie

Date: 29 Jul, 2014 02:21:05

Message: 7 of 7

Thanks for your posts. I am struggling with a similar issue and I am looking for advice.

I've installed gcc49 with Homebrew and made the following changes to my mexopts.sh:

1) CC='xcrun /usr/local/bin/gcc'
2) CXX='xcrun /usr/local/bin/g++'
3) Changed references to 10.9 SDK

When I try to compile my .cpp files, I get the following error message:

dyld: Library not loaded: /usr/local/opt/gmp4/lib/libgmp.3.dylib
  Referenced from: /usr/local/Cellar/gcc49/4.9.0/libexec/gcc/x86_64-apple-darwin13.3.0/4.9.0/cc1plus
  Reason: Incompatible library version: cc1plus requires version 9.0.0 or later, but libgmp.3.dylib provides version 8.0.0
g++: internal compiler error: Trace/BPT trap: 5 (program cc1plus)
/Applications/MATLAB_R2013b.app/bin/mexsh: line 1341: 41110 Abort trap: 6


I'm not sure if this is a Matlab issue or a Homebrew issue. Has anyone encountered this problem?










herijunior@gmail.com wrote in message <62fba5c3-ab03-4765-b7b4-dd0a3fd8302b@googlegroups.com>...
> I know this post is a bit old but Kudos to Eric and Hakon,
>
> I have been struggling with this issue for two days and both replies help me a lot to solve it.
>
> My original error was:
> -> xcrun -sdk macosx10.8 clang -O -arch x86_64 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -mmacosx-version-min=10.8 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2013b.app/extern/lib/maci64/mexFunction.map -fopenmp -lopencv_core -lopencv_imgproc -o "opencv_matlab_mex.mexmaci64" opencv_matlab_mex.o -L/usr/local/lib -L/Applications/MATLAB_R2013b.app/bin/maci64 -lmx -lmex -lmat -lstdc++
>
> ld: library not found for -lgomp
> clang: error: linker command failed with exit code 1 (use -v to see invocation)
>
> mex: link of ' "...mexmaci64"' failed.
>
> I installed gcc 4.9 using the following tutorial: http://solarianprogrammer.com/2013/06/11/compiling-gcc-mac-os-x/
>
> and I changed my mexopts.sh with the following configuration:
> CC='xcrun /usr/gcc-4.9.0/bin/gcc-4.9.0'
> CLIBS="$MLIBS -lstdc++ -I/usr/gcc-4.9.0/lib/ -lgomp"
>
> CXX='xcrun /usr/gcc-4.9.0/bin/g++-4.9.0'
> CXXLIBS="$MLIBS -lstdc++ -I/usr/gcc-4.9.0/lib/ -lgomp"
>
> Thanks again guys.
>
> Regards,
> Javier
>
> On Thursday, February 20, 2014 3:29:09 AM UTC+1, Håkon wrote:
> > This answer was right on spot!
> >
> > Thanks a lot!
> >
> >
> >
> > I tried both the suggested solutions and encountered in both cases problems with xcode 5 and the linking of the new versions (i am not a ninja mac terminal user =) ).
> >
> >
> >
> > However after a bit back and forth with the mexopts.sh file i was able to bypass the defualt linking to xcode's compiler.
> >
> >
> >
> > There were very little information out, on how to tweak the mexopts.sh file, so i thought it might be interesting for others to have a look at my tweaking.
> >
> >
> >
> > I first installed gcc-4.9.0 in the directory "/usr/local/bin/" using macports and $ sudo port install gcc49.
> >
> > To bypass xcode i changed the following lines in the mexopts.sh file:
> >
> > 129:
> >
> > # CC='xcrun -sdk macosx10.7 clang' #old
> >
> > CC='xcrun /usr/local/bin/gcc' #new
> >
> > 149
> >
> > # CXX='xcrun -sdk macosx10.7 clang++' #old
> >
> > CXX='xcrun /usr/local/bin/g++' #new
> >
> >
> >
> > to include the openMP library by defualt i changed the lines:
> >
> > 144
> >
> > # CLIBS="$CLIBS -lstdc++"
> >
> > CLIBS="$CLIBS -lstdc++ -I/usr/local/lib -lgomp" #directory <-I/usr/local/lib> may vary
> >
> > 151
> >
> > #CXXLIBS="$MLIBS -lstdc++"
> >
> > CXXLIBS="$MLIBS -lstdc++ -I/usr/local/lib -lgomp"
> >
> >
> >
> > I also had to change all active instances of "MacOSX10.8.sdk"/"MacOSX10.7.sdk" to "MacOSX10.9.sdk" to make the gcc-4.9.0 compiler happy... The openMP flag can be set when compiling, or it may be appended to the $CFLAGS and $CXXFLAGS in the mexopts.sh file.
> >
> >
> >
> > A copy of my mexopts.sh file now working with Mavericks OS X 10.9 and Matlab 2013b and gcc-4.9.0 installed from macports ($ sudo port install gcc49) is placed at folk.ntnu.no/seljasen/Arbeider/GSL_and_openMP_mexopts.sh
> >
> > openMP library and flag is included by default.
> >
> >
> >
> > Again: Thank you very much Eric, for pointing out this solution!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us