By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,681 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

How to resolve "undefined reference to..." problems?

P: n/a
Scroll to the bottom and read the last part first.

I've been trying very diligently to 'modularize' the code from TC++PL3E
found here: http://www.research.att.com/~bs/matrix.c

I keep getting what I believe are linker errors. For example:

g++ -g -O2 -o rematrix cslice_iter.o main.o matrix.o rematrix.o
slice_iter.o
rematrix.o(.text+0x29b): In function `f(int, int)':
/code/c++/stl/valarray/rematrix/src/rematrix.cpp:43: undefined reference to
`Matrix::column(unsigned)'
rematrix.o(.text+0x3a8):/code/c++/stl/valarray/rematrix/src/rematrix.cpp:51:
undefined reference to `Matrix::row(unsigned)'
collect2: ld returned 1 exit status
make[2]: *** [rematrix] Error 1

The original code compiles and runs just fine. I tried to preserve the
integrity of the overall program while separating it into different files
based on class/template names. You can find the mess I've made here:

http://baldur.globalsymmetry.com/pro...trix/rematrix/

The tarball:

http://baldur.globalsymmetry.com/pro...9-37-24.tar.gz

has the same content as the directory, and if you have a gnu build system on
you box, you too can ./configure it, and watch it fail. The location
indicated in the error message is

for(int x=0; x<x_max; x++) {
cout << "column " << x << ":\n";
for (Slice_iter<double> c = a.column(x); c!=c.end(); ++c) //<<<<<<<
cout << "\t" << *c <<"\n";
}

http://baldur.globalsymmetry.com/pro...c/rematrix.cpp

SOB! I just got it to compile!

I moved the implementations of Matrix::row(size_t i) from the source file
(matrix.cpp) and put them (back) in the header file. I would never have
taken them out of the header had it not been for other errors. ODR
violations IIRC.

I still would like to know how to systematically approach this kind of
situation. What does that error I listed indicate? I usually see that
kind of thing when my library references are wrong.

--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:_c********************@speakeasy.net...
Scroll to the bottom and read the last part first.

I've been trying very diligently to 'modularize' the code from TC++PL3E
found here: http://www.research.att.com/~bs/matrix.c

I keep getting what I believe are linker errors. For example:

g++ -g -O2 -o rematrix cslice_iter.o main.o matrix.o rematrix.o
slice_iter.o
rematrix.o(.text+0x29b): In function `f(int, int)':
/code/c++/stl/valarray/rematrix/src/rematrix.cpp:43: undefined reference to `Matrix::column(unsigned)'
rematrix.o(.text+0x3a8):/code/c++/stl/valarray/rematrix/src/rematrix.cpp:51: undefined reference to `Matrix::row(unsigned)'
collect2: ld returned 1 exit status
make[2]: *** [rematrix] Error 1

The original code compiles and runs just fine. I tried to preserve the
integrity of the overall program while separating it into different files
based on class/template names. You can find the mess I've made here:

http://baldur.globalsymmetry.com/pro...trix/rematrix/

The tarball:

http://baldur.globalsymmetry.com/pro...9-37-24.tar.gz
has the same content as the directory, and if you have a gnu build system on you box, you too can ./configure it, and watch it fail. The location
indicated in the error message is

for(int x=0; x<x_max; x++) {
cout << "column " << x << ":\n";
for (Slice_iter<double> c = a.column(x); c!=c.end(); ++c) //<<<<<<<
cout << "\t" << *c <<"\n";
}

http://baldur.globalsymmetry.com/pro...c/rematrix.cpp
SOB! I just got it to compile!

I moved the implementations of Matrix::row(size_t i) from the source file
(matrix.cpp) and put them (back) in the header file. I would never have
taken them out of the header had it not been for other errors. ODR
violations IIRC.

I still would like to know how to systematically approach this kind of
situation. What does that error I listed indicate? I usually see that
kind of thing when my library references are wrong.


The problem is that the functions are declared inline.

Put non-inline functions in source files

Put inline functions in header files.

Those rules don't apply to template functions, but you don't have any of
those.

Suspect that your other problems (ODR violations) would have been caused by
forgetting those rules.

john
Jul 22 '05 #2

P: n/a
John Harrison wrote:

"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:_c********************@speakeasy.net...
Scroll to the bottom and read the last part first.

I've been trying very diligently to 'modularize' the code from TC++PL3E
found here: http://www.research.att.com/~bs/matrix.c

I keep getting what I believe are linker errors. For example:

g++ -g -O2 -o rematrix cslice_iter.o main.o matrix.o rematrix.o
slice_iter.o
rematrix.o(.text+0x29b): In function `f(int, int)':
/code/c++/stl/valarray/rematrix/src/rematrix.cpp:43: undefined reference to

....
The problem is that the functions are declared inline.
I didn't even give that a thought.
Put non-inline functions in source files
Put inline functions in header files.
I'm not even sure why Stroustrup used the "inline" rather than just putting
the code in the class declaration.

Those rules don't apply to template functions, but you don't have any of
those.

Suspect that your other problems (ODR violations) would have been caused
by forgetting those rules.


I appreciate the advice, and will keep it in mind. I'm still a bit curious
as to what the errors actually signify. My reasoning for putting the
functions back in the header was that the linker couldn't find them in the
object code produced by compiling the source files. But that is based on
far less than a 'scientific' understanding.
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #3

P: n/a
Steven T. Hatton wrote:
John Harrison wrote:

"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:_c********************@speakeasy.net...
Scroll to the bottom and read the last part first.

I've been trying very diligently to 'modularize' the code from TC++PL3E
found here: http://www.research.att.com/~bs/matrix.c

I keep getting what I believe are linker errors. For example:

g++ -g -O2 -o rematrix cslice_iter.o main.o matrix.o rematrix.o
slice_iter.o
rematrix.o(.text+0x29b): In function `f(int, int)':
/code/c++/stl/valarray/rematrix/src/rematrix.cpp:43: undefined reference

to

...
The problem is that the functions are declared inline.


I didn't even give that a thought.


As it turns out, that /wasn't/ the cause of the problems I was having. I
tried to work with the code again, and discovered there is some kind of
mistake in how I set up the build system. Playing with the arrangement of
the code did influence the behavior, but there were several configurations
which /should/ have compiled, but didn't. One of the stages of learing a
programming language is to learn what certain kinds of errors signify. For
me, until I get a pretty good grasp of that, I end up mangling a lot of
code through trial and error without a lot of direction.
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.