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

Discussion in 'C++' started by Steven T. Hatton, May 25, 2004.

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

    --
    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
    Steven T. Hatton, May 25, 2004
    #1
    1. Advertising

  2. "Steven T. Hatton" <> wrote in message
    news:...
    > 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
    John Harrison, May 25, 2004
    #2
    1. Advertising

  3. John Harrison wrote:

    >
    > "Steven T. Hatton" <> wrote in message
    > news:...
    >> 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
    Steven T. Hatton, May 26, 2004
    #3
  4. Steven T. Hatton wrote:

    > John Harrison wrote:
    >
    >>
    >> "Steven T. Hatton" <> wrote in message
    >> news:...
    >>> 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
    Steven T. Hatton, Jun 5, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kamal
    Replies:
    1
    Views:
    525
  2. ScriptNFool
    Replies:
    0
    Views:
    400
    ScriptNFool
    Jun 30, 2003
  3. Kevin Kenny

    Dsn.Resolve() fails in ASP.NET

    Kevin Kenny, Aug 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    504
    Kevin Kenny
    Aug 29, 2003
  4. TJS
    Replies:
    2
    Views:
    462
    Steve C. Orr [MVP, MCSD]
    Jan 16, 2004
  5. Replies:
    1
    Views:
    10,147
Loading...

Share This Page