【问题标题】:What is missing from the link that results in the undefined reference? [duplicate]导致未定义引用的链接缺少什么? [复制]
【发布时间】:2019-05-15 22:02:23
【问题描述】:

为了调查另一个问题(程序结束时的 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();
}

【问题讨论】:

  • 您需要在代码调用该函数之前的某个时间点实际定义(实现/编写函数体)Fl_Inputc(int, int, int, int, const char*);
  • 如何让 Fl_Inputc 继承 Fl_Input 定义的功能(析构函数除外)?这就是我要问的问题。
  • 这听起来与您发布的问题完全不同。你已经在你的类中声明你自己的构造函数,这会阻止基类的构造函数被使用。如果你只是删除它们,你应该期望基类的构造函数被继承
  • 谢谢@alter 我得到了上面的例子,试图通过参数数量的不匹配来获得。我将使用该示例发布一个新问题。

标签: c++ linker fltk


【解决方案1】:

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

【讨论】:

  • 谢谢@cj.vaughter。我声明它是为了尝试获得一个匹配的函数配置文件。问题的真正解决方案(即使我问错了问题)是使用 using-declaration using Fl_Input::Fl_Input; 从基类继承构造函数来继承构造函数。见上面添加的代码。
  • 啊,我明白了。很难确定你到底想做什么。很高兴你明白了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-23
  • 2013-06-23
  • 1970-01-01
  • 2011-07-27
  • 2011-12-22
  • 1970-01-01
  • 2014-05-29
相关资源
最近更新 更多