Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to resolve "undefined reference to..." problems?

Reply
Thread Tools

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

 
 
Steven T. Hatton
Guest
Posts: n/a
 
      05-25-2004
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+0x3a:/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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-25-2004

"Steven T. Hatton" <> wrote in message
news:_cmdnViM0fFvxy7dRVn-...
> 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+0x3a:/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


 
Reply With Quote
 
 
 
 
Steven T. Hatton
Guest
Posts: n/a
 
      05-26-2004
John Harrison wrote:

>
> "Steven T. Hatton" <> wrote in message
> news:_cmdnViM0fFvxy7dRVn-...
>> 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
 
Reply With Quote
 
Steven T. Hatton
Guest
Posts: n/a
 
      06-05-2004
Steven T. Hatton wrote:

> John Harrison wrote:
>
>>
>> "Steven T. Hatton" <> wrote in message
>> news:_cmdnViM0fFvxy7dRVn-...
>>> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
ERROR: Cannot resolve reference Microsoft.VC80.CRT pankajtakawale Windows 64bit 0 04-20-2007 12:37 PM
src-resolve: Cannot resolve the name ... ivanet@gmail.com XML 1 03-23-2007 12:10 PM
Repost: Using EntityResolver to NOT resolve a PE Reference =?ISO-8859-1?Q?Ricardo_Palomares_Mart=EDnez?= Java 0 08-19-2006 10:27 AM
DTD entity reference resolve Per XML 4 02-04-2004 07:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57