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

S

Steven T. Hatton

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/projects/rematrix/rematrix/

The tarball:

http://baldur.globalsymmetry.com/projects/rematrix/rematrix-2004-05-25-09-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/projects/rematrix/rematrix/src/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.
 
J

John Harrison

Steven T. Hatton said:
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/projects/rematrix/rematrix/

The tarball:

http://baldur.globalsymmetry.com/projects/rematrix/rematrix-2004-05-25-09-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/projects/rematrix/rematrix/src/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
 
S

Steven T. Hatton

John said:
....

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

Steven T. Hatton

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

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top