undefined symbol error

Discussion in 'C++' started by eugene, Sep 15, 2005.

  1. eugene

    eugene Guest

    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?

    >>mex -v abc.cpp //that's how you "make" in Matlab, and that's what was called:


    -> 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.

    >> abc //try to run and got error:


    ??? 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;
    eugene, Sep 15, 2005
    #1
    1. Advertising

  2. eugene

    mlimber Guest

    eugene wrote:
    > 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
    mlimber, Sep 15, 2005
    #2
    1. Advertising

  3. eugene

    eugene Guest

    mlimber wrote:
    > eugene wrote:
    > > 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


    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?
    eugene, Sep 15, 2005
    #3
  4. "eugene" <> wrote in message
    news:...
    > - 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
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Sep 15, 2005
    #4
  5. eugene

    eugene Guest

    Ali Çehreli wrote:
    > "eugene" <> wrote in message
    > news:...
    > > - 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


    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?
    eugene, Sep 16, 2005
    #5
  6. "eugene" <> wrote in message
    news:...
    Ali Çehreli wrote:
    > "eugene" <> wrote in message
    > news:...
    > > - 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
    >
    > 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
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Sep 16, 2005
    #6
  7. eugene

    eugene Guest

    Ali Çehreli wrote:
    > "eugene" <> wrote in message
    > news:...
    > Ali Çehreli wrote:
    > > "eugene" <> wrote in message
    > > news:...
    > > > - 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
    > >
    > > 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


    I was trying this. Does not work :(
    Because libxyz.so is based on c++
    Thanks anyway
    eugene, Sep 17, 2005
    #7
  8. eugene

    Jay Nabonne Guest

    On Thu, 15 Sep 2005 13:47:45 -0700, eugene wrote:

    > 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
    Jay Nabonne, Sep 17, 2005
    #8
  9. eugene

    Jay Nabonne Guest

    On Sat, 17 Sep 2005 00:42:28 +0000, Jay Nabonne wrote:

    > On Thu, 15 Sep 2005 13:47:45 -0700, eugene wrote:
    >
    >> 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;"


    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
    Jay Nabonne, Sep 17, 2005
    #9
  10. eugene

    eugene Guest

    Jay Nabonne wrote:
    > On Thu, 15 Sep 2005 13:47:45 -0700, eugene wrote:
    >
    > > 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.


    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
    eugene, Sep 19, 2005
    #10
  11. eugene

    eugene Guest

    Jay Nabonne wrote:
    > On Sat, 17 Sep 2005 00:42:28 +0000, Jay Nabonne wrote:
    >
    > > On Thu, 15 Sep 2005 13:47:45 -0700, eugene wrote:
    > >
    > >> 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;"

    >
    > 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
    eugene, Sep 19, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. fredy
    Replies:
    2
    Views:
    400
    jimleaton
    Jul 16, 2004
  2. modemer
    Replies:
    0
    Views:
    552
    modemer
    Mar 4, 2005
  3. el prinCipante

    Symbol undefined? Linker error!

    el prinCipante, Jan 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    583
    Mark McIntyre
    Jan 27, 2004
  4. Sean
    Replies:
    8
    Views:
    940
  5. Replies:
    6
    Views:
    1,780
Loading...

Share This Page