compiling a c++ class library

R

rsparapa

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
 
M

Mathias Waack

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'

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
 
R

rsparapa

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
 
P

Phlip

rsparapa said:
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.

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

Haro Panosyan

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

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

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,799
Latest member
Mercury_Dev

Latest Threads

Top