MATLAB Answers

Can't compile matcreat.cpp in linux

18 views (last 30 days)
Michael
Michael on 17 May 2011
Edited: John Kelly on 26 Feb 2015
I can't successfully compile the example file: matcreat.cpp (provided by mathworks.) I am running linux centOS 5.6 and am using g++ version 4.1.2. My matlab version is 2010a and I am on a 64-bit machine.
I can, however, successfully compile and run matcreat.c using gcc.
The command I use to compile is:
g++ -o ~/m matcreat.cpp -I /usr/local/matlab2010a/extern/include/ -L /usr/local/matlab2010a/bin/glnxa64/ -lmat -lmx
Here are the errors I get when using g++ to compile matcreat.cpp
/usr/local/matlab2010a/bin/glnxa64//libmx.so: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)@GLIBCXX_3.4.9'
/usr/local/matlab2010a/bin/glnxa64/libut.so: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::basic_ostream<char, std::char_traits<char> >::_M_insert<void const*>(void const*)@GLIBCXX_3.4.9'
/usr/local/matlab2010a/bin/glnxa64/libmwfl.so: undefined reference to `std::bad_alloc::what() const@GLIBCXX_3.4.9'
/usr/local/matlab2010a/bin/glnxa64/libmwfl.so: undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::basic_istream<char, std::char_traits<char> >::_M_extract<bool>(bool&)@GLIBCXX_3.4.9'
/usr/local/matlab2010a/bin/glnxa64//libmx.so: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::basic_ostream<char, std::char_traits<char> >::_M_insert<double>(double)@GLIBCXX_3.4.9'
/usr/local/matlab2010a/bin/glnxa64/libut.so: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::basic_ostream<char, std::char_traits<char> >::_M_insert<long>(long)@GLIBCXX_3.4.9'
/usr/local/matlab2010a/bin/glnxa64/libut.so: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::basic_ostream<char, std::char_traits<char> >::_M_insert<bool>(bool)@GLIBCXX_3.4.9'
/usr/local/matlab2010a/bin/glnxa64//libmx.so: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::basic_ostream<char, std::char_traits<char> >::_M_insert<unsigned long>(unsigned long)@GLIBCXX_3.4.9'
collect2: ld returned 1 exit status
I would like write c++ code to read and write .mat files. Thanks! -- Michael

  0 Comments

Sign in to comment.

Accepted Answer

Kaustubha Govind
Kaustubha Govind on 17 May 2011
Edited: John Kelly on 26 Feb 2015
Have you tried compiling using the options file as described in the documentation: Building on UNIX Operating Systems. You are likely not linking against some additional C++ libraries.

  3 Comments

Michael
Michael on 17 May 2011
Kaustubha,
No. I have not tried that but it appears that the options file is needed for the "mex" command which is run within matlab.
I'm trying to compile a c++ program from the bash shell.
I think you are right that I'm not linking in all needed libraries, but I can't see what else to link in. As you can see from my description above, I am linking in /usr/local/matlab2010a/bin/glnxa64/ -lmat -lmx
Am I still missing something?
Thanks for your help,
Michael
Kaustubha Govind
Kaustubha Govind on 17 May 2011
Michael: If you follow the MEX command with the -v option, you will see the exact compiler/linker commands that are needed.
Michael
Michael on 20 May 2011
Dear Kaustubha,
Thank you for the answer.
I used the mex command as you suggested and saw the options. This lead me to see that I needed to use g++ version 4.3.4 instead of what I was using (version 4.1.2).
I changed compilers to g++ 4.3.4 and the mex function command worked and so does compiling from a bash shell with "g++".
Problem solved.
Thanks,
Michael

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by