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?
Related
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
I have C code using OpenMP what I want to include in MATLAB as a mex function. I have already done it and works perfectly in MATLAB R2013b under Linux. However, I recently read about OMPi and according to this article it beats most of the commercial compilers, mainly gcc what I currently use. Therefore, I would like make use of it. It works well for C codes, but I cannot make it work with a mex function. I tried it four ways:
Edited the CC='gcc' entry in the mexopts.sh file to CC='ompicc' and called mex
Directly called ompi as ompicc -k -I/opt/MATLAB/R2013b/extern/include mex_sum_openmp.c
Setting the verbose flag for mex, I obtained information about what mex is doing, so I tried to mimic it: ompicc -k -I/opt/MATLAB/R2013b/extern/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread -DMX_COMPAT_32 -O -DNDEBUG mex_sum_openmp.c
Since ompi is a source-to-source compiler, I compiled the mex_sum_openmp.c file with ompicc -k mex_sum_openmp.c to mex_sum_openmp_ompi.c and then called gcc on it: gcc -I/opt/MATLAB/R2013b/extern/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread -DMX_COMPAT_32 -O -DNDEBUG mex_sum_openmp_ompi.c (this is what ompi does without the -k flag: first creates the source compiled file and then calls a regular compiler on it)
Either way, the result is the same, a bunch of warnings and then error messages.
Here is the original C file: http://s000.tinyupload.com/?file_id=70221693843831744745, here the source compiled file :http://s000.tinyupload.com/?file_id=02497191022856307556 and here are the warnings and errors: http://s000.tinyupload.com/?file_id=93602974698898858427
Thanks
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"
I am trying to get OpenGL and Glut running on Eclipse Linux FC13.
After spending two days on it, I admit that help is needed. On FC13 Eclipse, I see
/usr/include/GL and /usr/include/SDL -- so the libs are there. I started Eclipse, and then tried to run a simple program on it, just like suggested here. However, two things were missing in those steps:
Callisto could not be installed --
nothing was found from the
repository
GCC C++ Linker is not found anywhere
for Eclipse 3.5.2.
When trying to run the program, I see this error:
Program does not exist
and sometimes
Binary not found
If I just run the "hello world" it works, but otherwise, those errors happen every time I try to include glut gl or sdl commands.
Here is an excerpt from the compiler error:
make all
g++ -O2 -g -Wall -fmessage-length=0 -c -o tw.o tw.cpp
tw.cpp: In function ‘void main_loop_function()’:
g++ -o tw tw.o
Yes, apparently the compiler is not able to see the glu, gl, sdl and glut libraries.
Some suggestion on how to fix?
You have to tell the compiler that your program uses additional libraries.
Use the -l argument
g++ -O2 -g -Wall -fmessage-length=0 -lglut -lGL -lGLU -lX11 -c -o tw.o tw.cpp
This should help against unsatisfied link errors.
You can set these in the properties of your Project.
Properties->c/c++ Build->Settings->Tool Settings->Linker
Check if the compiler is able to find the appropriate header files or not. If not, you are sure to get compiler errors. Try using the -I option to set the appropriate paths.
Once you've fixed that, check if there are any linker errors (undefined symbols/references or the sort). If you do: Try to set the library paths using the -L option and ask the compiler to link in the specific libraries by using the -l option. Note that the latter expects something like -lmath where in reality the library being linked in is actually called libmath.so or libmath.a (as the case may be).
How can I get the command line GCC uses to invoke ld?
I have a problem for an AVR target where GCC apparently adds a linker option which I am trying to override, so I would like to look at the exact options GCC uses for ld.
Use gcc -v to see what commands it runs. As in,
gcc -v -o hello hello.c
This will print a lot of output, including the linker command. The actual output depends on the platform, but the linking command should be near the end. Alternatively, use
gcc -### -o hello hello.c
This is like -v, but does not actually run any commands and quotes the options.
Another option is
gcc -dumpspecs
Look for the entry for link.
The above command line flags are listed in gcc --help and explained on the man page. Here's GCC documentation for the spec files.