[NEWBIE] C++ linking problems (Undefined symbols)

From: zapro (zaproheeks_at_yahoo.it)
Date: 12/27/04


Date: Mon, 27 Dec 2004 03:33:57 GMT

Hi, I am learning C++ and I have a very newbie question about some
linking issues.
You can see a demostration of my problem using the code below. When I
try to build it, the compile phase completes correctly but I get the
following error on linking:

Ld /Users/newt/devel/_builds/essentialcpp/essentialcpp
    cd /Users/newt/devel/tutorials_mine/essentialcpp
    /usr/bin/g++-3.3 -o
/Users/newt/devel/_builds/essentialcpp/essentialcpp
-L/Users/newt/devel/_builds/essentialcpp
-F/Users/newt/devel/_builds/essentialcpp -filelist
/Users/newt/devel/_builds/essentialcpp/essentialcpp.build/LessThan_Inline_operator.build/Objects-normal/essentialcpp.LinkFileList
-lstdc++ -arch ppc -Wl,-no_arch_warnings
ld: Undefined symbols:
LessThan::operator()(int) const

The code will build if I remove the inline keyword from the definition
of the operator() or if I remove the call. I can't understand why:
anybody can turn on the light for me? Any insight would be greatly
appreciated.

I am also not sure if this is a linker bug, but since I am new to C++,
I find it difficult to believe that. I am using Apple's Xcode IDE on
Mac OS X 10.3.6 (gcc 3.3). For those of you who are familiar with
Xcode, I am using a "Deployment" build style just to be sure everything
is linked (Zerolink is disabled).

Thank you, and pardon me for the stupidity of this question.

--zapro

ps.
the code below is taken from Stan Lippman's Essential C++ book.

-------------------

//
// LessThan.h
//

#ifndef LESSTHAN_H_
#define LESSTHAN_H_

class LessThan
{
public:
    LessThan(int val) : _val(val) {}
    int comp_val() const { return _val; }
    void comp_val(int nval){ _val = nval; }
    bool operator()(int value) const;
private:
    int _val;
};
#endif

//
// LessThan.cpp
//

#include "LessThan.h"
using namespace std;

inline // #### removing this will link correctly
bool LessThan::operator()(int value) const
{
    return value < _val;
}

//
// main.cpp
//

#include "LessThan.h"
using namespace std;

// invoking overloaded call operator () by applying it to a class object
int main(int argc, char *argv[])
{
    LessThan lt(10);
    lt(5); // #### removing this will link correctly
}



Relevant Pages

  • Issue with STL
    ... I am stuck while linking my application which uses STL map class. ... rt,struct std::pair<unsigned short const,class CInstanceInfo>,struct ... My application is an MFC regular DLL dynamically linking to MFC. ...
    (microsoft.public.windowsce.embedded.vc)
  • Issue with STL
    ... I am stuck while linking my application which uses STL map class. ... rt,struct std::pair<unsigned short const,class CInstanceInfo>,struct ... My application is an MFC regular DLL dynamically linking to MFC. ...
    (microsoft.public.windowsce.embedded)
  • Re: VC6.0 call DLL that built by Visual 2005
    ... Are you linking in the test.lib? ... are built by Visual Studio 2005. ... Display(unsigned short,unsigned short const *,unsigned short const ...
    (microsoft.public.vc.mfc)
  • Re: c equivalence of const_cast?
    ... >> linking it to a third language (I'm not knowledgeable in either c or ... > In C doesn't exsist a const keyword, normally, some compiler support ...
    (comp.lang.c)