compiling a c++ class library

Discussion in 'C++' started by rsparapa, May 19, 2005.

  1. rsparapa

    rsparapa Guest

    I've been working on vector/matrix/PRNG class libraries for years, but
    I never got around to figuring out how to make them into libraries; I'm
    a statistician, not a computer scientist. So, for this example, I'm
    using g++ 4.0.0 on Solaris(SPARC) 9, if that makes any difference.
    Everything worked fine for the Vector libary, build (Makefile excerpt):

    #create shared library in PWD
    #with GNU ld
    #SHARED=-shared
    #with Sun ld
    SHARED=-Xlinker -G

    #GNU C++ compiler and flags
    CXX=g++-4.0.0 -g

    #GSL/math libraries
    LIBS=-lgsl -lgslcblas -lm

    libvector.so : vector.cxx vector.hxx Makefile
    $(CXX) -nostartfiles $(SHARED) -o $@ vector.cxx $(LIBS)

    vector_main.out : vector_main.cxx libvector.so
    $(CXX) -o $@ vector_main.cxx $(LIBS) -lvector
    ./$@

    But, the same thing does not work for the Matrix library. It's not the
    code as far as I can tell, because simpy doing an #include "matrix.cxx"
    works:


    libmatrix.so : matrix.cxx matrix.hxx Makefile
    $(CXX) -nostartfiles $(SHARED) -o $@ matrix.cxx $(LIBS)

    matrix_main.out : matrix_main.cxx libmatrix.so libvector.so
    $(CXX) -o $@ matrix_main.cxx $(LIBS) -lvector -lmatrix
    ./$@

    make matrix_main.out
    g++-4.0.0 -g -nostartfiles -Xlinker -G -o libmatrix.so matrix.cxx -lgsl
    -lgslcblas -lm
    g++-4.0.0 -g -o matrix_main.out matrix_main.cxx -lgsl -lgslcblas -lm
    -lvector -lmatrix
    Undefined first referenced
    symbol in file
    Matrix::eek:perator=(Matrix) /var/tmp//ccqgGJUa.o
    Matrix::inverse(double) /var/tmp//ccqgGJUa.o
    Matrix::Matrix(unsigned int, unsigned int, double,
    ....)/var/tmp//ccqgGJUa.o
    Matrix::Matrix(Matrix const&) /var/tmp//ccqgGJUa.o
    Matrix::eek:perator-() /var/tmp//ccqgGJUa.o
    Matrix::~Matrix() /var/tmp//ccqgGJUa.o
    operator*(Matrix const&, Matrix const&)/var/tmp//ccqgGJUa.o
    operator<<(std::basic_ostream<char, std::char_traits<char> >&,
    Matrix)/var/tmp//ccqgGJUa.o
    ld: fatal: Symbol referencing errors. No output written to
    matrix_main.out
    collect2: ld returned 1 exit status
    *** Error code 1
    make: Fatal error: Command failed for target `matrix_main.out'

    Any ideas?

    Thanks, Rodney
     
    rsparapa, May 19, 2005
    #1
    1. Advertisements

  2. I think thats not a C++ problem, its a linker problem. Thus its very
    system depending and has not much to do with C++.

    First make sure the libmatrix.so is created. Check for existence and
    check the contents (nm -C libmatrix.so should do this job). Next
    make sure your linker uses the right libs, maybe by adding a -L
    switch.

    And at last check the order of the libs. If a lib uses symbols from
    another lib, it should stand before on the commandline. Does
    libvector make calls into libmatrix? I assume a better order for you
    linker command line would be:

    -lvector -lmatrix -lgsl -lgslcblas -lm

    Mathias
     
    Mathias Waack, May 19, 2005
    #2
    1. Advertisements

  3. rsparapa

    rsparapa Guest

    Hi Mathias:

    Thanks for your response. I agree you with you that it is a linker
    problem and not a C++ problem. However, linking is an important part
    of using C++, so someone must know how this is done. I tried
    everything that you mentioned and alot more. Nothing has helped yet.
    What confuses me is that the vector and matrix linking processes are
    analogous; vector works, matrix fails. What's different? Not sure.

    Thanks, Rodney
     
    rsparapa, May 19, 2005
    #3
  4. rsparapa

    Phlip Guest

    Posting to the narrowest possible newsgroup - one that covers your
    compiler's linker - will get you the best answers. More people on that list
    will be qualified to review any answers you get.
     
    Phlip, May 19, 2005
    #4
  5. rsparapa

    x6c6565 Guest

    try a -L. -R. before your -lmatrix and/or compiling the library source
    with -fPIC.
     
    x6c6565, May 20, 2005
    #5
  6. Matrix::eek:perator=(Matrix) /var/tmp//ccqgGJUa.o

    Please check if in matrix.cxx you really define operator= for Matrix
    object, which takes const Matrix& argument. My guess is you are
    accidentally redefining Vector operator=.
     
    Haro Panosyan, May 20, 2005
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.