undefined symbol error

E

eugene

I'm trying to compile and run some c++ code to be called from Matlab
(mex file)
and I'm getting "Invalid MEX-file ... undefined symbol" error. Anybody
knows
where to look for solution?

-> g++ -c ... abc.cpp
-> gcc -c ... /usr/local/matlab701/extern/src/mexversion.c
-> g++ ... -o abc.mexglx abc.o mexversion.o ... -L/home/eugene/lib
.... -lxyz ...

No errors so far.

??? Invalid MEX-file '/home/eugene/matlab/abc.mexglx':
/home/eugene/lib/libxyz.so: undefined symbol: xyz_debug.

Code for libxyz.so has:
extern int xyz_debug;

abc.cpp has:
int xyz_debug = 0;
 
M

mlimber

eugene said:
I'm trying to compile and run some c++ code to be called from Matlab
(mex file)
and I'm getting "Invalid MEX-file ... undefined symbol" error. Anybody
knows
where to look for solution?

A MATLAB newsgroup or on the MathWorks website would probably be better
places to start. This group is for discussing C++ language issues, not
third-party tools.

Cheers! --M
 
E

eugene

mlimber said:
A MATLAB newsgroup or on the MathWorks website would probably be better
places to start. This group is for discussing C++ language issues, not
third-party tools.

Cheers! --M

Been there, done that :(
MathWorks website: do c++ at your own risk
MATLAB newsgroup: this is c/c++ problem

- symbol xyz_debug is defined in abc.cpp as "int xyz_debug = 0;"
- symbol xyz_debug is referenced in libxyz.so as "extern int
xyz_debug;"
The question is why after
-> g++ -c ... abc.cpp
-> gcc -c ... /usr/local/matlab701/extern/src/mexversion.c
-> g++ ... -o abc.mexglx abc.o mexversion.o ... -L/home/eugene/lib
.... -lxyz ...
try to run and got "undefined symbol xyz_debug" error?
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

eugene said:
- symbol xyz_debug is defined in abc.cpp as "int xyz_debug = 0;"
- symbol xyz_debug is referenced in libxyz.so as "extern int
xyz_debug;"
The question is why after
-> g++ -c ... abc.cpp
-> gcc -c ... /usr/local/matlab701/extern/src/mexversion.c
-> g++ ... -o abc.mexglx abc.o mexversion.o ... -L/home/eugene/lib
... -lxyz ...
try to run and got "undefined symbol xyz_debug" error?

Try putting abc.o after -lxyz. If I'm not mistaken, the linker looks for
symbols in the successive files.

Ali
 
E

eugene

Ali said:
Try putting abc.o after -lxyz. If I'm not mistaken, the linker looks for
symbols in the successive files.

Ali

Still no luck.

g++ ... -Wl,-y,xyz_debug ... -lxyz ... abc.o ...
/home/eugene/lib/libxyz.so: reference to xyz_debug
abc.o: definition of xyz_debug

with same runtime error "undefined symbol xyz_debug".

Any ideas?
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

Ali said:
Try putting abc.o after -lxyz. If I'm not mistaken, the linker looks for
symbols in the successive files.

Ali

Still no luck.

g++ ... -Wl,-y,xyz_debug ... -lxyz ... abc.o ...
/home/eugene/lib/libxyz.so: reference to xyz_debug
abc.o: definition of xyz_debug

with same runtime error "undefined symbol xyz_debug".

Any ideas?

I wonder whether you are running against C vs. C++ linkage issues. If
xyz_debug has C linkage in libxyz.so, like because of being declared in a C
file, you have to define it as

extern "C" int xyz_debug = 0;

in abc.cpp.

Ali
 
E

eugene

Ali said:
I wonder whether you are running against C vs. C++ linkage issues. If
xyz_debug has C linkage in libxyz.so, like because of being declared in a C
file, you have to define it as

extern "C" int xyz_debug = 0;

in abc.cpp.

Ali

I was trying this. Does not work :(
Because libxyz.so is based on c++
Thanks anyway
 
J

Jay Nabonne

mlimber wrote:
- symbol xyz_debug is defined in abc.cpp as "int xyz_debug = 0;"
- symbol xyz_debug is referenced in libxyz.so as "extern int
xyz_debug;"
The question is why after
-> g++ -c ... abc.cpp
-> gcc -c ... /usr/local/matlab701/extern/src/mexversion.c
-> g++ ... -o abc.mexglx abc.o mexversion.o ... -L/home/eugene/lib
... -lxyz ...
try to run and got "undefined symbol xyz_debug" error?

This seems like a gcc problem, not a MATLAB one. It's having problems
resolving a symbol in the .so file when the .so is being loaded at
runtime. You could probably reproduce this without MATLAB.

So, I would suggest running this by a gcc newgroup.

- Jay
 
J

Jay Nabonne

Another thought: what happens if you reverse where the int is defined
(i.e. define it in the .so and make it extern from abc.cpp)?

- Jay
 
E

eugene

Jay said:
This seems like a gcc problem, not a MATLAB one. It's having problems
resolving a symbol in the .so file when the .so is being loaded at
runtime. You could probably reproduce this without MATLAB.

Program like abc.cpp (same definition / reference for xyz_debug) was
successfully
compiled and ran outside Matlab.

It looks as the problem is due to the way Matlab loads external code.
My hope
was that c++ folks would point out how to link the code so that
definition and
reference for xyz_debug would be in order.

- Eugene
 
E

eugene

Jay said:
Another thought: what happens if you reverse where the int is defined
(i.e. define it in the .so and make it extern from abc.cpp)?

- Jay

Cannot do that, libxyz.so is not mine and it's not open source :(

- Eugene
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top