首页 > 解决方案 > 导致未定义引用的链接缺少什么?

问题描述

为了调查另一个问题(程序结束时的 free() 错误),我尝试覆盖 FLTK Fl_Input 类的析构函数。代码编译但在链接中失败并带有未定义的引用。

我查看了许多示例,但对答案的理解不足以知道我需要更改什么来解决问题。虽然这个程序不会重现 free() 问题,但如果 Fl_Input 对象(和 Fl_Output 对象)产生消息,我可以确定哪个被无效释放。

#include <FL/Fl.H>
#include <FL/Fl_Input.H>
#include <iostream>


class Fl_Inputc:public Fl_Input
{
  public:
    Fl_Inputc();
    Fl_Inputc(int left, int up, int width, int height, const char* label=0);
    ~Fl_Inputc()
    {
//   std::cout <<  " Inputc destroyed " << std::endl;
    };
};

Fl_Inputc input1( 90, 10, 180, 20, "     Input : ");

int main(int argc, char **argv) {
  return Fl::run();
}

我期望一个干净的编译和链接到一个修改过的析构函数和所有其他继承的东西,但是得到了:

cbc:~/Projects/fltk/Tut/Potthast$ fltk-config --compile 07example4b.cxx 
g++ -I/usr/include/cairo -I/usr/include/glib-2.0
-I/usr/lib/x86_64-linux-gnu/glib-2.0/include 
-I/usr/include/pixman-1 -I/usr/include/freetype2 
-I/usr/include/libpng12 -I/usr/include/freetype2 
-I/usr/include/cairo -I/usr/include/glib-2.0 
-I/usr/lib/x86_64-linux-gnu/glib-2.0/include 
-I/usr/include/pixman-1 -I/usr/include/freetype2 
-I/usr/include/libpng12 -g -O2 -fvisibility-inlines-hidden 
-D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT 
-o '07example4b' '07example4b.cxx' -Wl,-Bsymbolic-functions -lfltk 
-lX11
/tmp/cc423i4F.o: In function `__static_initialization_and_destruction_0':
/Projects/fltk/Tut/Potthast/07example4b.cxx:17: undefined reference 
to `Fl_Inputc::Fl_Inputc(int, int, int, int, char const*)' 
collect2: error: ld returned 1 exit status

为了使用@alter igel 的建议解决这个问题,我使用了一个 using 语句从基类中获取构造函数。

#include <FL/Fl.H>
#include <FL/Fl_Input.H>
#include <iostream>


class Fl_Inputc:public Fl_Input
{
    public:
    using Fl_Input::Fl_Input;
    ~Fl_Inputc()
    {
     std::cout <<  " Inputc destroyed " << std::endl;
        };
};

Fl_Input  input0( 90, 10, 180, 20, "     Input0: ");
Fl_Inputc input1( 90, 40, 180, 20, "     Input1: ");

int main(int argc, char **argv) {
  return Fl::run();
}

标签: c++linkerfltk

解决方案


链接器找不到它的定义,Fl_Inputc(int left, int up, int width, int height, const char* label=0);因为您还没有定义它,您只是声明了它。


推荐阅读