compile c++ code using mex

10 views (last 30 days)
Zhenjia
Zhenjia on 16 Oct 2013
Answered: Zhenjia on 21 Oct 2013
Hi, I want to compile my c++ code using mex. The error information is as follows. My friend compiled it successfully using his matlab and computer. So I am wondering what my problem is.
In Matlab command window:
>> mex main.cpp -o test Undefined symbols: "_mexFunction", referenced from: -exported_symbol[s_list] command line option ld: symbol(s) not found collect2: ld returned 1 exit status
mex: link of ' "test.mexmaci64"' failed.
Error using mex (line 206) Unable to complete successfully. >>
My computer: Mac OS X 10.6.8
Xcode: Version 3.2.6
Matlab: R2012a
My c++ code:
#include iostream
int main (int argc, char * const argv[]) { // insert code here... std::cout << "Hello, World!\n"; return 0; }
Thanks.

Answers (3)

Jan
Jan on 17 Oct 2013
Edited: Jan on 17 Oct 2013
Your code does not have a the required gateway function mexFunction. So you can compile it from the shell or in the IDE of your compiler, but it is not useful and valid for a compiled MEX function.
You can find tutorials for creating and using MEX files e.g. at http://www.mathworks.com/help/matlab/create-mex-files.html.

Zhenjia
Zhenjia on 17 Oct 2013
I used -v -g to output more compiling information. It looks my error is related to 64 bit Mac system. I am still looking for answers to solve this problem. Thanks.
mex -v -g -largeArrayDims main.cpp
************************************************************************ Warning: Neither -compatibleArrayDims nor -largeArrayDims is selected. Using -compatibleArrayDims. In the future, MATLAB will require the use of -largeArrayDims and remove the -compatibleArrayDims option. For more information, see: http://www.mathworks.com/help/techdoc/matlab_external/bsflnue-1.html ************************************************************************
-> mexopts.sh sourced from directory (DIR = $PREF_DIR) FILE = /Users/zhenjia/.matlab/R2012a/mexopts.sh ---------------------------------------------------------------- -> MATLAB = /Applications/MATLAB_R2012a.app -> CC = gcc-4.2 -> CC flags: CFLAGS = -fno-common -no-cpp-precomp -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.5 -fexceptions CDEBUGFLAGS = -g COPTIMFLAGS = -O2 -DNDEBUG CLIBS = -L/Applications/MATLAB_R2012a.app/bin/maci64 -lmx -lmex -lmat -lstdc++ arguments = -> CXX = g++-4.2 -> CXX flags: CXXFLAGS = -fno-common -no-cpp-precomp -fexceptions -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.5 CXXDEBUGFLAGS = -g CXXOPTIMFLAGS = -O2 -DNDEBUG CXXLIBS = -L/Applications/MATLAB_R2012a.app/bin/maci64 -lmx -lmex -lmat -lstdc++ arguments = -> FC = gfortran -> FC flags: FFLAGS = -fexceptions -m64 -fbackslash FDEBUGFLAGS = -g FOPTIMFLAGS = -O FLIBS = -L/Applications/MATLAB_R2012a.app/bin/maci64 -lmx -lmex -lmat -L -lgfortran -L -lgfortranbegin arguments = -> LD = gcc-4.2 -> Link flags: LDFLAGS = -Wl,-twolevel_namespace -undefined error -arch x86_64 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.5 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2012a.app/extern/lib/maci64/mexFunction.map LDDEBUGFLAGS = -g LDOPTIMFLAGS = -O LDEXTENSION = .mexmaci64 arguments = -> LDCXX = -> Link flags: LDCXXFLAGS = LDCXXDEBUGFLAGS = LDCXXOPTIMFLAGS = LDCXXEXTENSION = arguments = ----------------------------------------------------------------
-> g++-4.2 -c -I/Applications/MATLAB_R2012a.app/extern/include -I/Applications/MATLAB_R2012a.app/simulink/include -DMATLAB_MEX_FILE -fno-common -no-cpp-precomp -fexceptions -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.5 -g "main.cpp"
-> gcc-4.2 -g -Wl,-twolevel_namespace -undefined error -arch x86_64 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.5 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2012a.app/extern/lib/maci64/mexFunction.map -o "main.mexmaci64" main.o -L/Applications/MATLAB_R2012a.app/bin/maci64 -lmx -lmex -lmat -lstdc++
Undefined symbols: "_mexFunction", referenced from: -exported_symbol[s_list] command line option ld: symbol(s) not found collect2: ld returned 1 exit status
mex: link of ' "main.mexmaci64"' failed.

Zhenjia
Zhenjia on 21 Oct 2013
Thanks. I compiled the example on the Matlab website successfully. Should I write the mexFunction for each c++ code? My c++ code can be compiled successfully in Linux system without writing any mexFunction. I am wondering whether my Matlab in Mac has missed some thing when I installed it. Is there any good tutorial about writing a mexFuction? Thanks.

Categories

Find more on Get Started with MATLAB Compiler SDK in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!