Thread: Issue with .so (Shared Object) Files

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

    Issue with .so (Shared Object) Files

    Hello there!

    I'm dealing with a piece of C code today, which is supposed to run in MATLAB. Thus, I intend to make use of the MEX interface.

    However, first - the c code must be compiled using

    1) make clean all
    2) make matlab


    My make-File looks the following way:

    Code:
    #
    # Choose your compiler
    #
    
    # 
    #CC = gcc-4.2
    #CPP = g++-4.2
    #CFLAGS=-O3 -ffast-math -fomit-frame-pointer -fPIC -mtune=k8 -march=k8 -Werror
    CC = gcc
    CPP = g++
    CFLAGS=-O3 -ffast-math -fomit-frame-pointer -fPIC -Werror 
    #CC=/agbs/share/sw/icc/bin/icc
    #CFLAGS= -fast -DCOMPILE_WITH_ICC -Werror
    
    
    #
    # MATLAB
    #    
    MATLABDIR=/usr/pack/matlab-7.10r2010a-sd/
    MATLAB_INCLUDE=-I$(MATLABDIR)/extern/include
    
    
    #
    # ARCHITECTURE
    # 
    
    # 64 bit
    SUFFIX=mexa64
    MATLAB_LIB=-L$(MATLABDIR)/bin/glnxa64 -lmex
    
    #LIBS=/usr/lib/gcc/x86_64-linux-gnu/4.3/libstdc++.a /usr/lib/
    
    #
    # MPI KMEANS FLAGS
    #
    #VERBOSEFLAG=-DKMEANS_VERBOSE=0 # 0: silent, 1:iteration counter, 2:everything
    #PRECISION=-DINPUT_TYPE=0 # 0: double, 1:float 
    
    
    
    
    matlab: mpi_kmeans_mex.$(SUFFIX) mpi_assign_mex.$(SUFFIX)
    
    
    libmpikmeans:    mpi_kmeans.o
        ar rc libmpikmeans.a mpi_kmeans.o
        ranlib libmpikmeans.a
        $(CC) -shared -Wl,-soname=libmpikmeans.so -fPIC $(CFLAGS) $(VERBOSEFLAG) -o libmpikmeans.so $(VERBOSEFLAGS) $(PRECISION) mpi_kmeans.cxx
    
    
    mpi_kmeans_mex.$(SUFFIX):    libmpikmeans mpi_kmeans_mex.o 
        $(CC) mpi_kmeans_mex.o -shared -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)
    
    test:    
        matlab -nojvm -r "test_code;exit"
    
    clean:    
        rm -f *.o
        rm -f *.mexglx
        rm -f *.mexa64
        rm -f libmpikmeans.so
        rm -f libmpikmeans.a
        rm -f mpi_assign mpi_kmeans
    After Step 1) I receive the following error message:
    ld: unknown option: -soname=libmpikmeans.so
    collect2: ld returned 1 exit status
    make: *** [libmpikmeans] Error 1
    I understand the problem here. the .so file extension is used for Linux only. However, I'm using a Mac.

    Is there an equivalent to the command -soname= for Mac?


    Looking forward to your replies.

    Thanks!
    Last edited by in_ship; 09-14-2012 at 03:22 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Quote Originally Posted by manual
    -soname=name
    When creating an ELF shared object, set the internal DT_SONAME field to the specified name. When an executable is linked with a shared object which has a DT_SONAME
    field, then when the executable is run the dynamic linker will attempt to load the shared object specified by the DT_SONAME field rather than the using the file
    name given to the linker.
    Since the names are the same, perhaps you could just delete -Wl,-soname=libmpikmeans.so and see what happens.

    It seems a moot point given that the names are the same to begin with.
    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
    Quote Originally Posted by Salem View Post
    Since the names are the same, perhaps you could just delete -Wl,-soname=libmpikmeans.so and see what happens.
    This is what I tried to do, but probably it's not correct. I'm receiving even more errors than before. The error lies in the very same line!

    gcc -shared -fPIC -O3 -ffast-math -fomit-frame-pointer -fPIC -Werror -o libmpikmeans.so mpi_kmeans.cxx

    Undefined symbols:
    "___gxx_personality_v0", referenced from:
    comp_randperm(void const*, void const*)in ccv2MOby.o
    remove_point_from_cluster(unsigned int, double*, double const*, unsigned int*, unsigned int)in ccv2MOby.o
    remove_point_from_cluster_w(unsigned int, double*, double const*, double const*, double*, unsigned int, unsigned int*, unsigned int)in ccv2MOby.o
    add_point_to_cluster(unsigned int, double*, double const*, unsigned int*, unsigned int)in ccv2MOby.o
    add_point_to_cluster_w(unsigned int, double*, double const*, double const*, double*, unsigned int, unsigned int*, unsigned int)in ccv2MOby.o
    kmeans_error(char*) in ccv2MOby.o
    compute_distance(double const*, double const*, unsigned int)in ccv2MOby.o
    assign_point_to_cluster(unsigned int, double const*, double const*, unsigned int, unsigned int, unsigned int, double*, float*, float*, float*)in ccv2MOby.o
    assign_point_to_cluster_w(unsigned int, double const*, double const*, unsigned int, unsigned int, unsigned int, double*, float*, float*, float*, unsigned int)in ccv2MOby.o
    assign_point_to_cluster_ordinary(double const*, double const*, unsigned int, unsigned int)in ccv2MOby.o
    init_point_to_cluster(unsigned int, double const*, double const*, unsigned int, unsigned int, double*, float*, float const*)in ccv2MOby.o
    init_point_to_cluster_w(unsigned int, double const*, double const*, unsigned int, unsigned int, double*, float*, unsigned int, float const*)in ccv2MOby.o
    compute_cluster_distances(float*, float*, double const*, unsigned int, unsigned int, bool const*)in ccv2MOby.o
    compute_sserror_w(double const*, double const*, double const*, unsigned int const*, unsigned int, unsigned int)in ccv2MOby.o
    compute_sserror(double const*, double const*, unsigned int const*, unsigned int, unsigned int)in ccv2MOby.o
    randperm(unsigned int*, unsigned int)in ccv2MOby.o
    remove_identical_clusters(double*, float*, double const*, unsigned int*, unsigned int*, unsigned int, unsigned int, unsigned int)in ccv2MOby.o
    kmeans_run(double*, double const*, unsigned int*, unsigned int, unsigned int, unsigned int, unsigned int)in ccv2MOby.o
    remove_identical_clusters_w(double*, float*, double const*, double const*, double*, unsigned int*, unsigned int*, unsigned int, unsigned int, unsigned int)in ccv2MOby.o
    kmeans_run_w(double*, double const*, double const*, unsigned int*, unsigned int, unsigned int, unsigned int, unsigned int)in ccv2MOby.o
    kmeans_run_master(double*, double const*, double const*, unsigned int*, unsigned int, unsigned int, unsigned int, unsigned int)in ccv2MOby.o
    _kmeans in ccv2MOby.o
    CIE in ccv2MOby.o

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

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I think -install_name is used instead of -soname on OSX. Try -install_name libmpikmeans.so and see if it works.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > Undefined symbols:
    > "___gxx_personality_v0", referenced from:
    You need to invoke the linker through g++, not gcc.

    As in
    g++ -shared -fPIC -O3 -ffast-math -fomit-frame-pointer -fPIC -Werror -o libmpikmeans.so mpi_kmeans.cxx
    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.

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    Quote Originally Posted by Subsonics View Post
    I think -install_name is used instead of -soname on OSX. Try -install_name libmpikmeans.so and see if it works.
    Did now help - I got an the same error again, this time for -install_name.

  7. #7
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    Quote Originally Posted by Salem View Post
    >
    As in
    g++ -shared -fPIC -O3 -ffast-math -fomit-frame-pointer -fPIC -Werror -o libmpikmeans.so mpi_kmeans.cxx
    I see. Very interesting. I tried it out - the old error message is gone.

    Instead, I have another main error now.

    ar rc libmpikmeans.a mpi_kmeans.o
    ranlib libmpikmeans.a
    g++ -shared -fPIC -O3 -ffast-math -fomit-frame-pointer -fPIC -Werror -o libmpikmeans.so mpi_kmeans.cxx
    gcc -O3 -ffast-math -fomit-frame-pointer -fPIC -Werror -I/usr/pack/matlab-7.10r2010a-sd//extern/include -c mpi_kmeans_mex.cxx -o mpi_kmeans_mex.o
    mpi_kmeans_mex.cxx:2:17: error: mex.h: No such file or directory
    mpi_kmeans_mex.cxx:6: error: ‘mxArray’ has not been declared
    mpi_kmeans_mex.cxx:6: error: expected ‘,’ or ‘...’ before ‘*’ token
    mpi_kmeans_mex.cxx:6: error: ISO C++ forbids declaration of ‘mxArray’ with no type
    mpi_kmeans_mex.cxx: In function ‘void mexFunction(int, int**, int, int)’:
    mpi_kmeans_mex.cxx:12: error: ‘mwSize’ was not declared in this scope
    mpi_kmeans_mex.cxx:12: error: expected `;' before ‘dims’
    mpi_kmeans_mex.cxx:16: error: ‘mexPrintf’ was not declared in this scope
    mpi_kmeans_mex.cxx:21: error: ‘prhs’ was not declared in this scope
    mpi_kmeans_mex.cxx:21: error: ‘mxGetPr’ was not declared in this scope
    mpi_kmeans_mex.cxx:25: error: ‘prhs’ was not declared in this scope
    mpi_kmeans_mex.cxx:25: error: ‘mxGetPr’ was not declared in this scope
    mpi_kmeans_mex.cxx:30: error: ‘prhs’ was not declared in this scope
    mpi_kmeans_mex.cxx:30: error: ‘mxGetPr’ was not declared in this scope
    mpi_kmeans_mex.cxx:40: error: ‘mexPrintf’ was not declared in this scope
    mpi_kmeans_mex.cxx:46: error: ‘prhs’ was not declared in this scope
    mpi_kmeans_mex.cxx:46: error: ‘mxIsDouble’ was not declared in this scope
    mpi_kmeans_mex.cxx:46: error: ‘mxIsComplex’ was not declared in this scope
    mpi_kmeans_mex.cxx:47: error: ‘mxGetNumberOfDimensions’ was not declared in this scope
    mpi_kmeans_mex.cxx:49: error: ‘mexPrintf’ was not declared in this scope
    mpi_kmeans_mex.cxx:62: error: ‘prhs’ was not declared in this scope
    mpi_kmeans_mex.cxx:62: error: ‘mxGetM’ was not declared in this scope
    mpi_kmeans_mex.cxx:63: error: ‘mxGetN’ was not declared in this scope
    mpi_kmeans_mex.cxx:67: error: ‘mxGetPr’ was not declared in this scope
    mpi_kmeans_mex.cxx:72: error: ‘mexErrMsgTxt’ was not declared in this scope
    mpi_kmeans_mex.cxx:75: error: ‘mexErrMsgTxt’ was not declared in this scope
    mpi_kmeans_mex.cxx:78: error: ‘mexErrMsgTxt’ was not declared in this scope
    mpi_kmeans_mex.cxx:84: error: ‘mxIsComplex’ was not declared in this scope
    mpi_kmeans_mex.cxx:85: error: ‘mxGetNumberOfDimensions’ was not declared in this scope
    mpi_kmeans_mex.cxx:87: error: ‘mexPrintf’ was not declared in this scope
    mpi_kmeans_mex.cxx:94: error: ‘mexErrMsgTxt’ was not declared in this scope
    mpi_kmeans_mex.cxx:96: error: ‘mexErrMsgTxt’ was not declared in this scope
    mpi_kmeans_mex.cxx:99: error: ‘dims’ was not declared in this scope
    mpi_kmeans_mex.cxx:105: error: ‘mxDOUBLE_CLASS’ was not declared in this scope
    mpi_kmeans_mex.cxx:105: error: ‘mxREAL’ was not declared in this scope
    mpi_kmeans_mex.cxx:105: error: ‘mxCreateNumericArray’ was not declared in this scope
    mpi_kmeans_mex.cxx:113: error: ‘mxGetPr’ was not declared in this scope
    mpi_kmeans_mex.cxx:121: error: ‘mxIsNaN’ was not declared in this scope
    mpi_kmeans_mex.cxx:121: error: ‘mxIsInf’ was not declared in this scope
    mpi_kmeans_mex.cxx:122: error: ‘mexErrMsgTxt’ was not declared in this scope
    mpi_kmeans_mex.cxx:130: error: ‘mxUINT32_CLASS’ was not declared in this scope
    mpi_kmeans_mex.cxx:149: error: ‘mxCreateScalarDouble’ was not declared in this scope
    make: *** [mpi_kmeans_mex.o] Error 1
    Clearly, the error is coming from the fact, that the c-Files are not being interpreted as mex, since mex.h is not localizable on the system.

    The mpi_kmeans_mex.cxx - Filesays: #include "mex.h"
    I tried using #include <mex.h>, since there was no mex.h file in the folder.

    I still get the same error!
    What's going on here?
    Last edited by in_ship; 09-15-2012 at 08:29 AM.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm pretty sure that the code already includes all the headers that you need, but for some reason mex.h is misplaced/gone.

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    I have the feeling that the hardcoded MATLAB directory does not exist on my system (I'm using a different version of MATLAB).

    Here is the relevant part of makefile again:
    #
    # Choose your compiler
    #

    #
    #CC = gcc-4.2
    #CPP = g++-4.2
    #CFLAGS=-O3 -ffast-math -fomit-frame-pointer -fPIC -mtune=k8 -march=k8 -Werror
    CC = gcc
    CPP = g++
    CFLAGS=-O3 -ffast-math -fomit-frame-pointer -fPIC -Werror
    #CC=/agbs/share/sw/icc/bin/icc
    #CFLAGS= -fast -DCOMPILE_WITH_ICC -Werror


    #
    # MATLAB
    #
    MATLABDIR=/usr/pack/matlab-7.10r2010a-sd/
    MATLAB_INCLUDE=-I$(MATLABDIR)/extern/include




    #
    # ARCHITECURE
    #

    # 32 bit
    #SUFFIX=mexglx
    #MATLAB_LIB=-L$(MATLABDIR)/bin/glnx86 -lmex

    # 64 bit
    SUFFIX=mexa64
    MATLAB_LIB=-L$(MATLABDIR)/bin/glnxa64 -lmex

    #LIBS=/usr/lib/gcc/x86_64-linux-gnu/4.3/libstdc++.a /usr/lib/libm.a




    matlab: mpi_kmeans_mex.$(SUFFIX) mpi_assign_mex.$(SUFFIX)

    %_mex.o: %_mex.cxx
    $(CC) $(CFLAGS) $(VERBOSEFLAG) $(MATLAB_INCLUDE) $(PRECISION) -c $^ -o $@

    mpi_kmeans_mex.$(SUFFIX): libmpikmeans mpi_kmeans_mex.o
    $(CC) mpi_kmeans_mex.o -shared -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)
    The path for MATLAB_LIB seems more appropriate for Linux, rather than Mac. Additionally, MATLABDIR has to be changed - how to find it on my system?
    Last edited by in_ship; 09-15-2012 at 08:50 AM.

  10. #10
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    Quote Originally Posted by whiteflags View Post
    I'm pretty sure that the code already includes all the headers that you need, but for some reason mex.h is misplaced/gone.

    So - I'm finally upload the entire 'original' version of the code: Link
    Let me know if anyone managed to compile this thing using 'make matlab'.

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by in_ship View Post
    Did now help - I got an the same error again, this time for -install_name.
    Same error as what? ld: Unknown option?

    Quote Originally Posted by man ld
    -install_name name
    Sets an internal "install path" (LC_ID_DYLIB) in a dynamic library. Any clients linked against the library will record that path as the way dyld should locate this library. If this option is not
    specified, then the -o path will be used. This option is also called -dylib_install_name for compatibility.
    You did ask for a -soname equivalent, there is no -soname on OSX so it would make sense that it gives you a "Unknown option" error.
    Last edited by Subsonics; 09-15-2012 at 09:58 AM.

  12. #12
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    So, I figured out the problem with the mex.h file finally!

    As suspected, the path to the MATLAB directory was wrong (it was hardcoded in the makefile). Instead, I changed it to my directory.

    But now I get an error from within one of the .cxx files:
    gcc mpi_kmeans_mex.o -shared -o mpi_kmeans_mex.mexa64 libmpikmeans.a -L/Applications/MATLAB_R2011a.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
    _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
    _mexFunction in mpi_kmeans_mex.o
    .....

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

  13. #13
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    Quote Originally Posted by Subsonics View Post
    Same error as what? ld: Unknown option?
    Yes,
    ar rc libmpikmeans.a mpi_kmeans.o
    ranlib libmpikmeans.a
    g++ -shared -Wl,-install_name=libmpikmeans.so -fPIC -O3 -ffast-math -fomit-frame-pointer -fPIC -Werror -o libmpikmeans.so mpi_kmeans.cxx
    ld: unknown option: -install_name=libmpikmeans.so
    collect2: ld returned 1 exit status
    make: *** [libmpikmeans] Error 1

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by in_ship View Post
    Yes,
    I posted a copy/pasted section from the OSX ld manual! You are not suppose to use '=' like this is dd, but a normal "-flag argument" as you can see in the man page section and my first post.

  15. #15
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    Quote Originally Posted by Subsonics View Post
    I posted a copy/pasted section from the OSX ld manual! You are not suppose to use '=' like this is dd, but a normal "-flag argument" as you can see in the man page section and my first post.
    Indeed - I had tested that too previously (based on a stackoverflow entry), but unfortunately, I still get an error. Once I completely remove it, this part of the file does not yield any error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-22-2011, 04:06 AM
  2. Detecting path to shared object
    By dev.fb0 in forum Linux Programming
    Replies: 5
    Last Post: 04-07-2011, 04:48 PM
  3. Global arrays shared between multiple source files
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 08-14-2008, 06:29 PM
  4. Monitering Open Network Shared Files
    By AdmiralKirk in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2006, 07:09 AM
  5. Makefiles, Object files, Source files, OH MY!
    By Inquirer in forum Linux Programming
    Replies: 2
    Last Post: 04-29-2003, 10:36 PM