Matlab: Cannot compile mex with openmp (undefined reference) - matlab

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!

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.

Related

Linking with GCC doesn't detect -fPIC flag

I am trying to link some files. Here is my command:
gcc -T linker.ld -o Stack\ Berry.bin -ffreestanding -O2 -nostlib kernel.o boot.o -fPIC -lgcc
How ever, I get this error:
/usr/bin/ld: boot.o: relocation R_X86_64_32 against `.multiboot' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
I have included the -fPIC argument, have I just put it in the wrong place? I have tried putting the argument in where it compiles but no luck. I haven't been able to find any thing for the syntax for this flag. Thanks!
You are passing -fPIC to your linkage command. It is a compiler option,
because you need to compile your source code to Position Independent object files.
Remove it from this command and add it to your compilation commands.
(-O2 is likewise a compiler, not linker, option.)

Compiling LIBSVM for Matlab R2016a

I want to use LiBSVM with Matlab R2016a under Windows 7 64 Bit with the MinGW64 compiler and the precompiled mex files didn't work for me.
When I tried to run the standard make file that comes with LibSVM the following error occurs:
*Building with 'MinGW64 Compiler (C)'.
Error using mex
gcc: error: -fexceptions: No such file or directory
*
The makefile looks like this:
mex CFLAGS="\$CFLAGS -std=c99" -largeArrayDims libsvmread.c
mex CFLAGS="\$CFLAGS -std=c99" -largeArrayDims libsvmwrite.c
mex CFLAGS="\$CFLAGS -std=c99" -I.. -largeArrayDims svmtrain.c ../svm.cpp svm_model_matlab.c
mex CFLAGS="\$CFLAGS -std=c99" -I.. -largeArrayDims svmpredict.c ../svm.cpp svm_model_matlab.c
In another answer to a stackoverflow question I saw that somebody used this code for compiling:
mex -O -largeArrayDims libsvmread.c
mex -O -largeArrayDims libsvmwrite.c
mex -O -I.. -largeArrayDims svmtrain.c ../svm.cpp svm_model_matlab.c
mex -O -I.. -largeArrayDims svmpredict.c ../svm.cpp svm_model_matlab.c
This also works for me. What is the difference between the two ways of compiling LibSVM and are there any disadvantages of using the second option?
edit:
this is the so question I mentioned: http://ch.mathworks.com/matlabcentral/newsreader/view_thread/297195
after doing a bit more research I understood that -std=c99 selects a c language version which is not fully implemented for gcc under windows and -O optimizes the object code. In http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html#f213 the developers mention that they actually did not use any c99 languge features.
So compiling the library should have no disadvantages, right?
The "error: -fexceptions: No such file or directory." May also be solved by changing CFLAGS to COMPFLAGS.
Credit https://github.com/cjlin1/libsvm/issues/55

Linking failure using cygwin GCC as matlab mex compiler

I'm trying to compile a piece of code which requires linking to matlab libraries in cygwin.
g++ -o mezcaglue.dll -shared -Wl,--out-implib,mezcaglue.lib -I/cygdrive/e/Matlab/R2014b/extern/include -L/cygdrive/e/EPICS/labca_3_4_2/lib/cygwin-x86_64 /cygdrive/e/Matlab/R2014b/extern/lib/win64/microsoft/libut.lib /cygdrive/e/Matlab/R2014b/extern/lib/win64/microsoft/libmx.lib /cygdrive/e/Matlab/R2014b/extern/lib/win64/microsoft/libmex.lib -m64 ini.o multiEzca.o ctrlC-polled.o mglue.o -lezcamt -lpthread -lm
I'm positive that all the *.o and *.lib files are in place, and yet I get errors like these.
/cygdrive/e/EPICS/labca_3_4_2/glue/O.cygwin-x86_64/../multiEzca.c:876: undefined reference to `mxMalloc'
/cygdrive/e/EPICS/labca_3_4_2/glue/O.cygwin-x86_64/../multiEzca.c:894: undefined reference to `mxFree'
multiEzca.o: In function `nativeType':
/cygdrive/e/EPICS/labca_3_4_2/glue/O.cygwin-x86_64/../multiEzca.c:382: undefined reference to `ca_field_type'
multiEzca.o: In function `multi_ezca_ts_cvt':
/cygdrive/e/EPICS/labca_3_4_2/glue/O.cygwin-x86_64/../multiEzca.c:294: undefined reference to `epicsTimeToTimespec'
multiEzca.o: In function `ezcaSetSeverityWarnLevel':
/cygdrive/e/EPICS/labca_3_4_2/glue/O.cygwin-x86_64/../multiEzca.c:1022: undefined reference to `mexPrintf'
I'm new to these gcc g++ cygwin stuffs so any help will be appreciated.
Thanks in advance!

Ubuntu gcc compile errors

When I compile my program with the command
gcc -o ****** -Xlinker -Bstatic -L/usr/lib -lf2c -lm
an error occurs
usr/bin/ld: cannot find -lgcc_s
Who can tell me the way to solve this error? The version of my system is 32bit Ubuntu 13.04, gcc 4.6. And I had searched the error,somebody said it maybe the error of symlink, but it has the link of /usr/lib/libgcc_s.so to /lib/i386-linux-gnu/libgcc_s.so.1 on my system. I also setted system variable LD_LIBRARY_PATH=/lib/i386-linux-gnu, but it did not work.
It is happening when I add -Bdynamic to the command,gcc compile successfully.The commands of compiling program with archive library and dynamic library are different.Maybe it is the explanation of "cannot find -lgcc_s"

using C++11 in Mex with g++ 4.8 in linux

I'm trying to call a function I wrote in C++ 11 from a mex script. The C++ code requires -std=c++11, and runs fine from the terminal.
Here's g++ -v output: gcc version 4.8.2 20140120 (Red Hat 4.8.2-15) (GCC)
I have Matlab 2013a for Red Hat.
When I first tried calling mex filename.cpp from matlab console I got:
This file requires compiler and library support for the ISO C++ 2011
standard. This support is currently experimental, and must be enabled
with the -std=c++11 or -std=gnu++11 compiler options.
So, I went into the /usr/local/MATLAB/R2013a/bin/mexopts.sh file that matlab uses to get compler options and added -std=c++11. Now I get:
cc1plus: error: unrecognized command line option "-std=c++11"
The full command gotten from mex -v filename.cpp is:
g++ -c -I/usr/local/MATLAB/R2013a/extern/include -I/usr/local/MATLAB/R2013a/simulink/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -fPIC -fno-omit-frame-pointer -pthread -std=c++11 -DMX_COMPAT_32 -O -DNDEBUG "mexMorph.cpp"
So, how can I get this to compile properly?
Try
mex CXXFLAGS="\$CXXFLAGS -std=c++11" simple_example.cpp
Alteratively build your mex-file without directly running Matlab such as using CMake like the following github repo : mex-it
My testing indicates that -ansi and -std=c++11 do conflict, as another responder has speculated. You could edit your mex options file (e.g. ~/.matlab/R2014a/mex_C++_glnxa64.xml in my setup) and remove -ansi.
Also note that mex accepts a -v flag, which dumps a lot of useful debugging info.
It doesn't make sense, but apparently using -std=c++0x will work. I think matlab does some checking beforehand, and since it doesn't support 4.8 officially it doesn't accept it even though the compiler would.
Can anyone back me up on this?