473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

compiling a c++ class library

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::operator=(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::operator-() /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

Jul 23 '05 #1
5 1823
rs******@mcw.edu wrote:
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::operator=(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::operator-() /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
Jul 23 '05 #2
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

Jul 23 '05 #3
rsparapa wrote:
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.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #4
try a -L. -R. before your -lmatrix and/or compiling the library source
with -fPIC.

Jul 23 '05 #5
> Matrix::operator=(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=.
rs******@mcw.edu wrote:
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::operator=(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::operator-() /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

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mike S. | last post by:
Hello, Has anyone had success compiling the informixdb-1.3 module under python 2.2? It seems the absense of makefile.pre.in in python 2.2 causes the break under py2.2. Is there an easy way...
7
by: Steven T. Hatton | last post by:
Is there anything that gives a good description of how source code is converted into a translation unit, then object code, and then linked. I'm particularly interested in understanding why putting...
3
by: Tron Thomas | last post by:
Given the following code: #include <vector> #include <algorithm> #include <functional> class Value { public: explicit Value(int value) : m_value(value) {}
4
by: Aaron Queenan | last post by:
When I build a C++ library to .NET using the managed C++ compiler, I get the following error message: Linking... LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReport LINK : error...
1
by: pataN | last post by:
I use csc /t:library scanControl.dll. Here I got some errors telling me that the compiler can't find the class library dll:s that I have referenced in my windows control project. I have added...
1
by: Jim Heavey | last post by:
Hello, trying to round out my knowlege here about compiling. To date I have used VS.Net to do all my compiling "majically", but I want to understand how to do it on my own, should the need ever...
3
by: Hugh Janus | last post by:
Hi all, I have added a class library project to my VB solution. All works fine but when I compile the project, the compiler creates an external DLL file. What I would rather it do is compile...
0
by: Pucca | last post by:
Hi I'm using vs2005. I am getting a bunch of compiler warnings after I made some changes to my code that was compiling clean. I'm also getting memory errors when I run my program and it's...
8
by: WebSnozz | last post by:
I have an application written in C that does a lot of low level stuff. It does a lot of things like casting from void*'s. I want to create a new GUI for it in either C# or MC++, but reuse the...
2
by: stevenruiz | last post by:
Hi Everyone, The Strings.h has the function Get_Line which is defined and the error is shown below: Strings.h: void get_line( istream & );
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.