Menu

Linker error "Undefined reference to:"

Spliter
2008-08-05
2012-09-26
  • Spliter

    Spliter - 2008-08-05

    Hi!
    I'm currently working on a game engine, but once again I came to halt thatnks to the linker errors.
    What is says is this:
    sources/collidableGeometry.o(.text+0xd5):collidableGeometry.cpp: undefined reference to length2d(double, double, double, double)' sources/collidableGeometry.o(.text+0x6dd):collidableGeometry.cpp: undefined reference tosign(double)'
    sources/collidableGeometry.o(.text+0x701):collidableGeometry.cpp: undefined reference to sign(double)' sources/collidableGeometry.o(.text$_Z7getSideR10cgVector2dS0_[getSide(cgVector2d&, cgVector2d&)]+0x38):collidableGeometry.cpp: undefined reference tosign(double)'
    sources/collidableGeometry.o(.text$_ZN10cgVector2d9normalizeEv[cgVector2d::normalize()]+0x26):collidableGeometry.cpp: undefined reference to length2d(double, double, double, double)' sources/collidableGeometry.o(.text$_ZN10cgVector2d7getSizeEv[cgVector2d::getSize()]+0x26):collidableGeometry.cpp: undefined reference tolength2d(double, double, double, double)'
    sources/particles.o(.text+0x331):particles.cpp: undefined reference to lengthDirx(double, double)' sources/particles.o(.text+0x355):particles.cpp: undefined reference tolengthDiry(double, double)'
    collect2: ld returned 1 exit status

    make.exe: *** [openGL.exe] Error 1

    all the missing functions are declared in hMath.h and defined in hMath.cpp, both of that files are included into my project, and are included in the headers of every source file that uses functions defined in hMath. Every single header I use is protected against double inclusion, and it includes everything it needs, so it's independent.
    I can't see what went wrong here...
    Please help me, you're my only hope.

     
    • cpns

      cpns - 2008-08-10

      'inline' functions are visible only in the module in which they are declared, so to share an inline definition, they must be fully defined in a header file. Without the inline qualifier, defining functions in a header file would cause the linker to issue a multiple definition error if use in multiple modules.

      inlining is a trivial optimisation, and seldom worth the effort. Besides unless you compile for optimisation (i.e. apply one of the -O option), GCC will ignore the inlining directive and produce multiple static functions instead. Moreover when you do apply optimisation, the compiler will choose to inline some functions in any case, so I would not generally bother - certainly not in a PC environment in any case.

      One argument for inlining is that you can expose the implementation for potential modification by users of your library. That however can equally be used as an argument against inlining.

      If you do choose to inline class members, I suggest you do it in teh class body rather than as a separate function, thus:

      class cgVector2d
      {
      ...
      void normalize()
      {
      }
      ...
      }

      Clifford

       
    • Wayne Keen

      Wayne Keen - 2008-08-05

      Excerpting the error message threw away some valuable information. Could you please post your "Basic 3", covered in the thread titled (not accidentally) "Please Read Before Posting a Question" - in particular, the compile log will tell us a lot about how things are set up in your project etc that really help with the hard job of remote viewing - I mean remote debugging.

      Wayne

       
      • Spliter

        Spliter - 2008-08-05

        I'm terribly sorry for forgetting that. Here's the Basic 3:
        1-
        Windows XP service Pack 2 32 bit
        Dev C++ 4.9.9.2
        2-
        I'm sorry, but I can't make a sample program since I don't know how in the world this linker error came up, but I'll post the most relevant parts of the code:

        File:hMath.h

        ifndef HMATH

        define HMATH 1

        include <math.h>

        include <stdlib.h>

        include <time.h>

        void seedRand();
        double degToRad(double x);
        double radtToDeg(double x);
        double direction(double x,double y);
        double directionDeg(double x,double y);
        double lengthDirx(double length,double dir);
        double lengthDiry(double length,double dir);
        double sqr(double x);
        signed char sign(double x);
        double random();
        double random(double x);
        double length2d(double x1,double y1,double x2,double y2);

        endif

        File:hMath.cpp
        //hMath.cpp

        include "headers\hMath.h"

        And definitions of All the above mentioned functions (I double checked it)

        File:collidableGeometry.h

        ifndef COLLIDABLEGEOMETRY

        define COLLIDABLEGEOMETRY 1

        include "hMath.h"

        class cgVector2d
        {
        (...)
        }
        and other definitions of classes;

        File:collidableGeometry.cpp

        include "headers\collidableGeometry.h"

        ...definitions of functions...
        inline void cgVector2d::normalize()
        {
        double size=length2d(x,y,0,0);
        if(!isNull())
        {
        x/=size;
        y/=size;
        }
        }

        cgVector2d cgTriangle2d::getCollisionVector(cgTriangle2d const other)//update later into giving a collision information object
        {
        (...)
        cgVector2d returnVector=new cgVector2d();
        for(int i=0;i<3;i++)
        {
        if(offset[i]!=0)
        if(offset[i]
        sign(offset[i])<offset[min]sign(offset[min]))
        {
        min=i;
        returnVector=other->normal[i];
        }
        }
        (returnVector)=offset[min];
        return returnVector;
        }

        (...)
        inline void cgVector2d::normalize()
        {
        double size=length2d(x,y,0,0);
        if(!isNull())
        {
        x/=size;
        y/=size;
        }
        }
        (...)//other functions use the lacking functions as well, the rest is made in the same way as here.

        File: particles.h

        ifndef Particles_h

        define Particles_h

        include "bgeGLFuncs.h"

        include "Global.h"

        include "Sprite.h"

        include "hMath.h"

        class Particle;
        class ParticleGen
        {
        (...)
        }
        (...)

        endif

        3-
        Compile Log:

        Compiler: Default compiler
        Building Makefile: "E:\Moje dokumenty\Spliter\Programming\El Poho\Makefile.win"
        Executing make...
        make.exe -f "E:\Moje dokumenty\Spliter\Programming\El Poho\Makefile.win" all
        windres.exe -i openGL_private.rc --input-format=rc -o openGL_private.res -O coff

        g++.exe sources/State.o sources/bgeGLFuncs.o sources/collidableGeometry.o sources/GameState.o sources/Global.o sources/hMath.o sources/Img.o sources/InputMgr.o sources/main.o sources/MenuButton.o sources/MenuState.o sources/particleGen.o sources/particles.o sources/Sprite.o openGL_private.res -o "openGL.exe" -L"D:/Dev-Cpp/lib" -mwindows -lmingw32 -lglu32 -lopengl32 -lSDLmain -lSDL -lSDL_image

        sources/collidableGeometry.o(.text+0xd5):collidableGeometry.cpp: undefined reference to length2d(double, double, double, double)' sources/collidableGeometry.o(.text+0x6dd):collidableGeometry.cpp: undefined reference tosign(double)'
        sources/collidableGeometry.o(.text+0x701):collidableGeometry.cpp: undefined reference to sign(double)' sources/collidableGeometry.o(.text$_Z7getSideR10cgVector2dS0_[getSide(cgVector2d&amp;, cgVector2d&amp;)]+0x38):collidableGeometry.cpp: undefined reference tosign(double)'
        sources/collidableGeometry.o(.text$_ZN10cgVector2d9normalizeEv[cgVector2d::normalize()]+0x26):collidableGeometry.cpp: undefined reference to length2d(double, double, double, double)' sources/collidableGeometry.o(.text$_ZN10cgVector2d7getSizeEv[cgVector2d::getSize()]+0x26):collidableGeometry.cpp: undefined reference tolength2d(double, double, double, double)'
        sources/particles.o(.text+0x331):particles.cpp: undefined reference to lengthDirx(double, double)' sources/particles.o(.text+0x355):particles.cpp: undefined reference tolengthDiry(double, double)'
        collect2: ld returned 1 exit status

        make.exe: *** [openGL.exe] Error 1

        Execution terminated

        All files are included into project.
        All sources are located in: PROJECT_DIR\sources
        All Headers are located in: PROJECT_DIR\sources\headers

        I hope it will help you to help me ;)

         
    • Wayne Keen

      Wayne Keen - 2008-08-05

      One thing I note right off is that you have your project stored in a directory with spaces in its path name.
      This is a very bad idea. (Not doing that is mentioned in the "Please Read" thread

      I'll look at your code closer here in a bit. I do see some places where you seem to be passing integers to functions which expect floats, which may not matter..

      Wayne

       
      • Spliter

        Spliter - 2008-08-06

        I noticed that as soon as I take out the "inline" type definition from the declaration the linker error disappears.
        So I wanted to ask you, how can I make those functions inline (they're small and used a lot of times) in a way it doesn't interferes with the linker?

         
        • Spliter

          Spliter - 2008-08-06

          Okay, I've found a site saying that the definition of the function must be included in the header file.
          It works fine now, so i guess the problem is pretty much solved ;)
          Anyway, thanks for the help ;)

           
          • Wayne Keen

            Wayne Keen - 2008-08-06

            The header file is important, even though it usually contains no actual working code.
            In most cases, it serves as a "menu", telling the compiler which functions are available
            for use, and what their "signature" is - how many and what kind of arguments they take.

            Best answer to a question, one that you provided. You are going to be good.

            Wayne

             

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.