Question

I'm getting this linker error. I know a way around it, but it's bugging me because another part of the project's linking fine and it's designed almost identically.

First, I have namespace LCD. Then I have two separate files, LCDText.h and LCDGraphic.h.

LCDText.h:

//[snip]
void TextDraw(Widget *w);

void TextBarDraw(Widget *w);

void TextHistogramDraw(Widget *w);

void TextIconDraw(Widget *w);

void TextBignumsDraw(Widget *w);

void TextGifDraw(Widget *w);

}; // End namespace

LCDGraphic.h:

//[snip]
void GraphicDraw(Widget *w);

void GraphicIconDraw(Widget *w);

void GraphicBarDraw(Widget *w);

void GraphicHistogramDraw(Widget *w);

void GraphicBignumsDraw(Widget *w);

void GraphicGifDraw(Widget *w);

}; // End namespace

And in WidgetBignums.h I have:

//[snip]
using namespace LCD;

extern void TextBignumsDraw(Widget *w);
extern void GraphicBignumsDraw(Widget *w);

template <class T>
WidgetBignums<T>::WidgetBignums(Generic<T> *v, std::string n, Json::Value *section,
    int row, int col) : Widget(n, section, row, col,
    WIDGET_TYPE_BIGNUMS | WIDGET_TYPE_RC | WIDGET_TYPE_SPECIAL) {

    if( v->GetType() == LCD_TEXT )
        Draw = TextBignumsDraw; // Line 66
    else if( v->GetType() == LCD_GRAPHIC )
        Draw = GraphicBignumsDraw;
    else
        Draw = NULL;
//[snip]

And I get the following linker error:

LCDControl.o: In function `WidgetBignums':
/home/starlon/Projects/LCDControl/WidgetBignums.h:66: undefined reference to `LCD::TextBignumsDraw(LCD::Widget*)'

Now here's one way to fix it, but I don't like it. I can move LCD::TextBignumsDraw outside of the LCD namespace and it works. Strange enough, the linker sees LCD::GraphicBignumsDraw. Any clues?

Edit: I'm using gcc 4.4.1-2 on Fedora 11. Using SCons to compile.

Edit: Here's WidgetBignums, showing Draw.

template <class T>
class WidgetBignums : public Widget {
    Generic<T> *visitor_;
    std::vector<char> FB_;
    std::vector<char> ch_;
    int min_;
    int max_;
    int update_;
    int layer_;
    Property *expression_;
    Property *expr_min_;
    Property *expr_max_;

    QTimer *timer_;

    void (*Draw)(Widget *);

    public:
    WidgetBignums(Generic<T> *visitor, std::string name, Json::Value *section, int row, int col);
    ~WidgetBignums();
    void TextScroll() {};
    void SetupChars();
    void Update();
    void Start();
    void Stop();
    std::vector<char> GetFB() { return FB_; }
    std::vector<char> GetCh() { return ch_; }
    Generic<T> *GetVisitor() { return visitor_; }
};

Edit: Here's TextBignumsDraw's signature.

//[snip]
void TextBignumsDraw(Widget *w) {
//[snip]

Edit: Incidentally, I'm getting the same error for TextHistogramDraw and TextGifDraw as well. TextIconDraw and the others are fine.

Was it helpful?

Solution

Where is the definition for LCD::TextBignumsDraw()? That's what the linker seems to be complaining about. Not the declaration, but the actual definition of the function.

The fact that when you move the declaration out of namespace LCD things start working indicates that the definition for TextBignumsDraw() is in the global namespace, not the LCD namespace.

This (in some .cpp file):

void TextBignumsDraw(Widget *w) {
    // ...
}

Needs to be wrapped in a

namespace LCD {

}

block.

OTHER TIPS

Try dropping the "using namespace LCD", and change that line 66 to:

Draw = LCD::TextBignumsDraw;

That's more explicit, which may help the linker understand what you're asking for.

Besides, you should never say "using namespace Anything" in a header file. It hoists everything in that namespace out into the global space for every user of that header. That almost completely destroys the value of having a namespace in the first place. You should hoist things out like this in the narrowest scope that's practical. Sometimes I put "using namespace foo" at the top of a single function, for instance, if that's the only user of the bits in the namespace within a given .cpp file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top