Thread: Undefined symbol errors in MEX compilation

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    78

    Undefined symbol errors in MEX compilation

    Hi there!

    I am currently trying to run someone else's code on my machine (Link).
    The precompiled MEX files are available for a Linux 64-bit machine. However, I am trying to run it on a Mac using MATLAB 2012b.

    I adapted the makefile by adjusting the MATLAB directory. However, I still get some compilation errors.
    It seems that certain libraries cannot be located.


    mycomputer:mpi_kmeans-1.5 username$ make matlab
    ar rc libmpikmeans.a mpi_kmeans.o
    ranlib libmpikmeans.a
    g++ -shared -fPIC -O3 -ffast-math -fomit-frame-pointer -fPIC -m64 -o libmpikmeans.so mpi_kmeans.cxx
    gcc mpi_kmeans_mex.o -shared -o mpi_kmeans_mex.mexmaci64 libmpikmeans.a -L/Applications/MATLAB_R2012b.app/bin/maci64 -lmex
    Undefined symbols:
    "_mxGetM", referenced from:
    _mexFunction in mpi_kmeans_mex.o
    _mexFunction in mpi_kmeans_mex.o
    _mexFunction in mpi_kmeans_mex.o
    _mexFunction in mpi_kmeans_mex.o
    "_mxGetN", referenced from:
    _mexFunction in mpi_kmeans_mex.o
    _mexFunction in mpi_kmeans_mex.o
    _mexFunction in mpi_kmeans_mex.o
    _mexFunction in mpi_kmeans_mex.o
    "_mxIsDouble", referenced from:
    _mexFunction in mpi_kmeans_mex.o
    "___gxx_personality_v0", referenced from:
    _mexFunction in mpi_kmeans_mex.o
    CIE in mpi_kmeans_mex.o
    comp_randperm(void const*, void const*)in libmpikmeans.a(mpi_kmeans.o)
    remove_point_from_cluster(unsigned int, double*, double const*, unsigned int*, unsigned int)in libmpikmeans.a(mpi_kmeans.o)
    add_point_to_cluster(unsigned int, double*, double const*, unsigned int*, unsigned int)in libmpikmeans.a(mpi_kmeans.o)
    compute_distance(double const*, double const*, unsigned int)in libmpikmeans.a(mpi_kmeans.o)
    randperm(unsigned int*, unsigned int)in libmpikmeans.a(mpi_kmeans.o)
    kmeans_error(char*) in libmpikmeans.a(mpi_kmeans.o)
    remove_identical_clusters(double*, float*, double const*, unsigned int*, unsigned int*, unsigned int, unsigned int, unsigned int)in libmpikmeans.a(mpi_kmeans.o)
    compute_cluster_distances(float*, float*, double const*, unsigned int, unsigned int, bool const*)in libmpikmeans.a(mpi_kmeans.o)
    compute_sserror(double const*, double const*, unsigned int const*, unsigned int, unsigned int)in libmpikmeans.a(mpi_kmeans.o)
    assign_point_to_cluster(unsigned int, double const*, double const*, unsigned int, unsigned int, unsigned int, double*, float*, float*, float*)in libmpikmeans.a(mpi_kmeans.o)
    assign_point_to_cluster_ordinary(double const*, double const*, unsigned int, unsigned int)in libmpikmeans.a(mpi_kmeans.o)
    init_point_to_cluster(unsigned int, double const*, double const*, unsigned int, unsigned int, double*, float*, float const*)in libmpikmeans.a(mpi_kmeans.o)
    kmeans_run(double*, double const*, unsigned int*, unsigned int, unsigned int, unsigned int, unsigned int)in libmpikmeans.a(mpi_kmeans.o)
    _kmeans in libmpikmeans.a(mpi_kmeans.o)
    CIE in libmpikmeans.a(mpi_kmeans.o)
    "_mxCreateNumericArray_730", referenced from:
    _mexFunction in mpi_kmeans_mex.o
    _mexFunction in mpi_kmeans_mex.o
    "_mxIsComplex", referenced from:
    _mexFunction in mpi_kmeans_mex.o
    _mexFunction in mpi_kmeans_mex.o
    "_mxGetNumberOfDimensions_730", referenced from:
    _mexFunction in mpi_kmeans_mex.o
    _mexFunction in mpi_kmeans_mex.o
    "_mxIsInf", referenced from:
    _mexFunction in mpi_kmeans_mex.o
    "_mxIsSingle", referenced from:
    _mexFunction in mpi_kmeans_mex.o
    "_mxIsNaN", referenced from:
    _mexFunction in mpi_kmeans_mex.o
    "_mxGetPr", referenced from:
    _mexFunction in mpi_kmeans_mex.o
    _mexFunction in mpi_kmeans_mex.o
    _mexFunction in mpi_kmeans_mex.o
    _mexFunction in mpi_kmeans_mex.o
    _mexFunction in mpi_kmeans_mex.o
    _mexFunction in mpi_kmeans_mex.o
    _mexFunction in mpi_kmeans_mex.o
    _mexFunction in mpi_kmeans_mex.o
    "_mxCreateDoubleScalar", referenced from:
    _mexFunction in mpi_kmeans_mex.o

    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    make: *** [mpi_kmeans_mex.mexmaci64] Error 1

    Any ideas where the problem could be?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > g++ -shared -fPIC -O3 -ffast-math -fomit-frame-pointer -fPIC -m64 -o libmpikmeans.so mpi_kmeans.cxx
    So why are you overwriting your library with the result of a module compilation?

    In fact, the whole sequence seems wrong - you create the library, then build the source.

    Try
    g++ -shared -fPIC -O3 -ffast-math -fomit-frame-pointer -fPIC -m64 -o mpi_kmeans.o mpi_kmeans.cxx
    ar rc libmpikmeans.a mpi_kmeans.o
    ranlib libmpikmeans.a


    > gcc mpi_kmeans_mex.o -shared -o mpi_kmeans_mex.mexmaci64 libmpikmeans.a -L/Applications/MATLAB_R2012b.app/bin/maci64 -lmex
    If you're compiling separate files using g++, then the final executable needs to be linked by invoking g++ as well.
    Seeing ___gxx_personality_v0 is a sign you've mixed up the C and C++.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    Hey Salem!

    Thanks for your willingness to help!

    I've removed the .so file command and changed g++ to gcc. Unfortunately though, I still seem to be getting an error on the g++ reference:

    ar rc libmpikmeans.a mpi_kmeans.o
    /usr/bin/ranlib: file: libmpikmeans.a(mpi_kmeans.o) is a dynamic library, not added to the static library
    ranlib libmpikmeans.a
    ranlib: file: libmpikmeans.a(mpi_kmeans.o) is a dynamic library, not added to the static library
    ranlib: empty library: libmpikmeans.a (no table of contents added)
    gcc -shared -fPIC -O3 -ffast-math -fomit-frame-pointer -fPIC -m64 -DKMEANS_VERBOSE=0 -o mpi_kmeans.o mpi_kmeans.cxx

    Undefined symbols:
    "___gxx_personality_v0", referenced from:
    comp_randperm(void const*, void const*)in ccWhobPa.o
    remove_point_from_cluster(unsigned int, double*, double const*, unsigned int*, unsigned int)in ccWhobPa.o
    add_point_to_cluster(unsigned int, double*, double const*, unsigned int*, unsigned int)in ccWhobPa.o
    compute_distance(double const*, double const*, unsigned int)in ccWhobPa.o
    randperm(unsigned int*, unsigned int)in ccWhobPa.o
    kmeans_error(char*) in ccWhobPa.o
    remove_identical_clusters(double*, float*, double const*, unsigned int*, unsigned int*, unsigned int, unsigned int, unsigned int)in ccWhobPa.o
    compute_cluster_distances(float*, float*, double const*, unsigned int, unsigned int, bool const*)in ccWhobPa.o
    compute_sserror(double const*, double const*, unsigned int const*, unsigned int, unsigned int)in ccWhobPa.o
    assign_point_to_cluster(unsigned int, double const*, double const*, unsigned int, unsigned int, unsigned int, double*, float*, float*, float*)in ccWhobPa.o
    assign_point_to_cluster_ordinary(double const*, double const*, unsigned int, unsigned int)in ccWhobPa.o
    init_point_to_cluster(unsigned int, double const*, double const*, unsigned int, unsigned int, double*, float*, float const*)in ccWhobPa.o
    kmeans_run(double*, double const*, unsigned int*, unsigned int, unsigned int, unsigned int, unsigned int)in ccWhobPa.o
    _kmeans in ccWhobPa.o
    CIE in ccWhobPa.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    make: *** [libmpikmeans] Error 1


  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    Here's an edit from my side:

    I tried what you recommended, i.e. I used a different order for the 3 compile commands and replaced g++ with gcc
    Code:
    gcc -shared -fPIC -O3 -ffast-math -fomit-frame-pointer -fPIC -m64  -DKMEANS_VERBOSE=0  -o mpi_kmeans.o   mpi_kmeans.cxx
    ar rc libmpikmeans.a mpi_kmeans.o
    ranlib libmpikmeans.a
    Unfortuntely though, I still get some error!
    Undefined symbols:
    "___gxx_personality_v0", referenced from:
    comp_randperm(void const*, void const*)in ccs3czSH.o
    remove_point_from_cluster(unsigned int, double*, double const*, unsigned int*, unsigned int)in ccs3czSH.o
    add_point_to_cluster(unsigned int, double*, double const*, unsigned int*, unsigned int)in ccs3czSH.o
    compute_distance(double const*, double const*, unsigned int)in ccs3czSH.o
    randperm(unsigned int*, unsigned int)in ccs3czSH.o
    kmeans_error(char*) in ccs3czSH.o
    remove_identical_clusters(double*, float*, double const*, unsigned int*, unsigned int*, unsigned int, unsigned int, unsigned int)in ccs3czSH.o
    compute_cluster_distances(float*, float*, double const*, unsigned int, unsigned int, bool const*)in ccs3czSH.o
    compute_sserror(double const*, double const*, unsigned int const*, unsigned int, unsigned int)in ccs3czSH.o
    assign_point_to_cluster(unsigned int, double const*, double const*, unsigned int, unsigned int, unsigned int, double*, float*, float*, float*)in ccs3czSH.o
    assign_point_to_cluster_ordinary(double const*, double const*, unsigned int, unsigned int)in ccs3czSH.o
    init_point_to_cluster(unsigned int, double const*, double const*, unsigned int, unsigned int, double*, float*, float const*)in ccs3czSH.o
    kmeans_run(double*, double const*, unsigned int*, unsigned int, unsigned int, unsigned int, unsigned int)in ccs3czSH.o
    _kmeans in ccs3czSH.o
    CIE in ccs3czSH.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    make: *** [libmpikmeans] Error 1
    I have not seen the ccs3czSH.o file ever before?

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    I change the gcc command in the first line to g++. The references error vanished, but now I seem to have library connection problem! :-(
    The code in the Makefile is bold and the errors are highlighted in red.

    Code:
    gcc -O3 -ffast-math -fomit-frame-pointer -fPIC -m64  -DKMEANS_VERBOSE=0   -c -o mpi_kmeans.o mpi_kmeans.cxx
    g++ -shared -fPIC -O3 -ffast-math -fomit-frame-pointer -fPIC -m64  -DKMEANS_VERBOSE=0  -o mpi_kmeans.o   mpi_kmeans.cxx
    ar rc libmpikmeans.a mpi_kmeans.o
    /usr/bin/ranlib: file: libmpikmeans.a(mpi_kmeans.o) is a dynamic library, not added to the static library
    ranlib libmpikmeans.a
    ranlib: file: libmpikmeans.a(mpi_kmeans.o) is a dynamic library, not added to the static library
    ranlib: empty library: libmpikmeans.a (no table of contents added)
    gcc -O3 -ffast-math -fomit-frame-pointer -fPIC -m64  -I/Applications/MATLAB_R2012b.app/extern/include -DKMEANS_VERBOSE=0   -c mpi_kmeans_mex.cxx -o mpi_kmeans_mex.o
    g++ mpi_kmeans_mex.o -shared -m64 -o mpi_kmeans_mex.mexmaci64 libmpikmeans.a -L/Applications/MATLAB_R2012b.app/bin/maci64 -lmex 
    ld: warning: in libmpikmeans.a, file was built for unsupported file format which is not the architecture being linked (x86_64)
    Undefined symbols:
      "_mxGetM", referenced from:
          _mexFunction in mpi_kmeans_mex.o
    ........

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    IIRC, The "-shared" means make a dynamic library/object file.

    I have no idea if you want a dynamic or static library.

    The "-static" should make a static library/object file.

    http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

    Tim S.
    Last edited by stahta01; 12-06-2012 at 02:54 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    Hey stahta01!

    Thanks for the heads-up!

    I did try that too. Unfortunately though, I still get errors:
    gcc -O3 -ffast-math -fomit-frame-pointer -fPIC -m64 -DKMEANS_VERBOSE=0 -c -o mpi_kmeans.o mpi_kmeans.cxx
    g++ -static -shared -fPIC -O3 -ffast-math -fomit-frame-pointer -fPIC -m64 -DKMEANS_VERBOSE=0 -o mpi_kmeans.o mpi_kmeans.cxx
    ld: warning: in /usr/lib/gcc/i686-apple-darwin10/4.2.1/libgcc_static.a, file was built for unsupported file format which is not the architecture being linked (x86_64)

    Undefined symbols:
    "___assert_rtn", referenced from:
    -----
    I understand that there is an incompatibility problem, but which library must be rebuilt and how?

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Doing both makes no sense to me "-static -shared". I could be wrong on this.

    The "-m64" means 64 bit; "x86_64" means 64 bit.
    The i686-apple-darwin10 implies 32 bit.

    You might try adding "-shared-libgcc" in case your compiler only has 64 bit Dynamic of libgcc.


    Tim S.
    Last edited by stahta01; 12-06-2012 at 04:07 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    Hey Tim!

    There is still some issue here.

    gcc -O3 -ffast-math -fomit-frame-pointer -fPIC -m64 -DKMEANS_VERBOSE=0 -c -o mpi_kmeans.o mpi_kmeans.cxx
    g++ -shared-libgcc -fPIC -O3 -ffast-math -fomit-frame-pointer -fPIC -m64 -DKMEANS_VERBOSE=0 -o mpi_kmeans.o mpi_kmeans.cxx

    Undefined symbols:
    "_main", referenced from:
    start in crt1.10.6.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    make: *** [libmpikmeans] Error 1
    I used the library path on my system to be '/usr/lib/gcc/i686-apple-darwin10/4.2.1/'
    Could this be an issue?

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by in_ship View Post
    Hey Tim!

    There is still some issue here.

    Code:
    Undefined symbols:
    "_main", referenced from:
    start in crt1.10.6.o
    I used the library path on my system to be '/usr/lib/gcc/i686-apple-darwin10/4.2.1/'
    Could this be an issue?
    I am guessing your code is missing the main function.

    If your code is not supposed to have a main you might need to add another option.
    I can not remember the right option; not even sure I ever know about the right option.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #11
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    mpi_kmeans does not really have a main-file.
    However, mpi_kmeans_main.cxx does!
    Can I somehow link them together?


    Here's the Makefile (compile with make matlab)
    Code:
    #
    # Choose your compiler
    #
    # 
    
    CC = gcc
    CPP = g++
    CFLAGS=-O3 -ffast-math -fomit-frame-pointer -fPIC -m64 #-Werror
    
    
    
    
    
    #
    # MPI KMEANS FLAGS
    #
    VERBOSEFLAG=-DKMEANS_VERBOSE=0 # 0: silent, 1:iteration counter, 2:everything
    #PRECISION=-DINPUT_TYPE=0 # 0: double, 1:float 
    
    #
    # MATLAB
    #
    
    MATLABDIR=/Applications/MATLAB_R2012b.app
    MATLAB_INCLUDE=-I$(MATLABDIR)/extern/include
    
    
    
    
    #
    # ARCHITECURE
    # 
    
    
    
    # 64 bit
    
    
    SUFFIX=mexmaci64
    MATLAB_LIB=-L$(MATLABDIR)/bin/maci64 -lmex
    
    LIBS=/usr/lib/gcc/i686-apple-darwin10/4.2.1/ /usr/lib/
    
    
    matlab:     mpi_kmeans_mex.$(SUFFIX) mpi_assign_mex.$(SUFFIX)
    
    
    mpi_kmeans.o:    mpi_kmeans.cxx mpi_kmeans.h
        $(CC) $(CFLAGS) $(VERBOSEFLAG) $(PRECISION) -c -o $@ mpi_kmeans.cxx
    
    
    mpi_kmeans_main.o:    mpi_kmeans_main.cxx
        $(CC) $(CFLAGS) $(BOOST_INCLUDE) -c -o mpi_kmeans_main.o mpi_kmeans_main.cxx 
    
    libmpikmeans:    mpi_kmeans.o
        ar rc libmpikmeans.a mpi_kmeans.o
        ranlib libmpikmeans.a
        $(CPP) -shared-libgcc -fPIC $(CFLAGS) $(VERBOSEFLAG) -o libmpikmeans.so $(VERBOSEFLAGS) $(PRECISION) mpi_kmeans.cxx
    
    mpi_assign_main.o:    mpi_assign_main.cxx
        $(CC) $(CFLAGS) $(BOOST_INCLUDE) -c -o mpi_assign_main.o mpi_assign_main.cxx 
    
    mpi_kmeans_main:    libmpikmeans mpi_kmeans_main.o
        $(CC) mpi_kmeans_main.o  $(CFLAGS) -L/usr/lib/ -static -o mpi_kmeans -lm libmpikmeans.a \
            $(BOOST_LIB) $(LIBS)
    
    mpi_assign_main:    libmpikmeans mpi_assign_main.o
        $(CC) mpi_assign_main.o  $(CFLAGS) -L/usr/lib/ -static -o mpi_assign -lm libmpikmeans.a \
            $(BOOST_LIB) $(LIBS)
    
    
    mpi_kmeans_mex.$(SUFFIX):    libmpikmeans mpi_kmeans_mex.o 
        $(CPP) mpi_kmeans_mex.o -shared -m64 -o mpi_kmeans_mex.$(SUFFIX) libmpikmeans.a $(MATLAB_LIB) 
    
    mpi_assign_mex.$(SUFFIX):    libmpikmeans mpi_assign_mex.o
        $(CC) mpi_assign_mex.o -shared -o mpi_assign_mex.$(SUFFIX) libmpikmeans.a $(MATLAB_LIB)
    Last edited by in_ship; 12-07-2012 at 01:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. conditional compilation based on existance of a symbol?
    By cyberfish in forum C++ Programming
    Replies: 6
    Last Post: 12-13-2007, 07:35 PM
  2. Undefined symbol error??
    By Jedijacob in forum C Programming
    Replies: 10
    Last Post: 03-18-2005, 04:46 PM
  3. Undefined Symbol error
    By Eavan Hyde in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2004, 03:39 AM
  4. undefined symbol
    By sworc66 in forum C Programming
    Replies: 3
    Last Post: 09-10-2003, 06:29 AM
  5. Undefined symbol 'stdprn'
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-03-2002, 02:05 PM