Hi, I am struggling with c++ linking problem in Linux...
The C++ code has MATLAB engine inside, and it has no problem when compiling under Windows(same MATLAB version, MATLAB 6.5R13), but it can not be compiled with Linux, I always get the following error:

/usr/bin/ld: warning: libstdc++-libc6.1-2.so.3, needed by /s2bin/matlabR13/bin/glnx86/libmx.so, not found (try using -rpath or -rpath-link)
/s2bin/matlabR13/bin/glnx86/libmx.so: undefined reference to `__throw'
collect2: ld returned 1 exit status
make: *** [rz_dpsk] Error 1
make: Target `all' not remade because of errors.

I have included the directory $(MATLAB)/sys/os/glnx86 in the Makefile, which has the file of libstdc++-libc6.1-2.so.3 that libmx.so is needed, but I still get the warning. The Makefile I am using is shown below:

MATLAB  =     /s2bin/matlabR13
CC      =       g++
FLAGS   = 	-I$(MATLAB)/extern/include -I$(MATLAB)/extern/include/cpp
LIBS    =	-L$(MATLAB)/bin/glnx86 -L$(MATLAB)/extern/lib/glnx86 -L$(MATLAB)/sys/os/glnx86 -lut -lmx -leng -lmex -lmat

CFLAGS	=	-Wall -g 
LINK	=	$(CC)
LFLAGS  =       
INCPATH =         
LIBPATH =      

SOURCES =       Mat_ex.cpp
OBJECTS =	Mat_ex.o
TARGETS =       Mat_ex

LINKOBJECTS =   
#######Implicit rules
.SUFFIXES:.cpp
#.cpp.o:
#$(CC) -c $(FLAGS) $(INCPATH) -o $@ $<
.cpp.o:
	$(CC) -c $(FLAGS) -o $@ $<
####### Build rules

all: $(TARGETS)

Mat_ex:Mat_ex.o $(LINKOBJECTS)
	$(LINK) $(LIBPATH) $(LFLAGS) -o $@ $@.o $(LINKOBJECTS) $(LIBS)

depend:
	@makedepend $(SOURCES) 2> /dev/null

showfiles:
	@echo $(HEADERS) $(SOURCES) Makefile

clean:
	rm *.o
	rm $(TARGETS)

<< moderator edit: added [code][/code] tags >>


The compiler version is:
gcc version 3.3.2 20031022 (Red Hat Linux 3.3.2-1)

Can anyone help? Thanks a lot!

It seems that your version of the MatLab dynamic library relies on a version of the C library that is more recent than the one installed with your redhat.

I see 2 solutions

The simplest : install a the right version of the C library (libc6.1-2.so.3)

The dirty one (if are not able to change the version of the C library on this system):
- verify that your libc.xx.so exports the needed symbol :

nm --demangle libc.so | grep __throw

In this case, you MIGHT be able to use it instead of the newer one (but you have to remember about this dirty trick if you encounter other problems afterwards) :
just create a symlink !

ln -s /usr/lib/<your>libc.so /usr <somewhere>/libc6.1-2.so.3

and then modify your makefile in order to search for this symlink during link :

LIBS    =   -L<somewhere>-L$(MATLAB)/bin/glnx86 -L$(MATLAB)/extern/lib/glnx86 -L$(MATLAB)/sys/os/glnx86 -lut -lmx -leng -lmex -lmat

Edited 3 Years Ago by diafol: fixed formatting

Thanks CrazyDieter for your kindly answer. Now I have installed the right C library, and the file can be compiled successfully. But it has problem when I run it, the error is(I compiled the engdemo.cpp given by mathwork):

./engdemo: error while loading shared libraries: libmat.so: cannot open shared object file: No such file or directory.

I have checked the MATLAB directories, the file libmat.so is located in $(MATLAB)/bin/glnx86, file 'libmx.so' is also in this directory. File 'libut.so' is in $(MATLAB)/extern/lib/glnx86, and all those two directories are linked in my Makefile, why they can not be loaded? Thanks in advance!

Not sure about linux, but on solaris, you need to update your LD_LIBRARY_PATH environment variable to include the directory with the shared objects you want to load... (and on AIX, it's LIBPATH)... it's probably something similar..

Hi,
Thanks all your guy's help, now I solve the problem :lol:

The reason that it can not be run is due to the dynamic MATLAB library need to be added to ld.so.conf, which is located in directory of /etc(need to be root in this case). Then run command 'ldconfig' so that the 'ld.so.cache' is updated, and the MATLAB dynamic library can be called when run the program.

Thanks again and wish you all my best!

This question has already been answered. Start a new discussion instead.