【问题标题】:FLTK not working with Stroustrup headersFLTK 不适用于 Stroustrup 标头
【发布时间】:2012-01-31 02:00:53
【问题描述】:

我目前正在通过 Stroustrup 的《编程:使用 C++ 的原则和实践》一书学习 C++,目前在第 12 章。我现在一直在尝试让 FLTK 与特定的标头工作。

我已经用 MacPorts 安装了 FLTK。当我尝试编译包含 Simple_window.h 的代码时,出现以下错误:

bash-3.2# fltk-config --compile main.cpp

/usr/bin/g++-4.2 -arch i386 -I/opt/local/include -pipe -arch i386 -arch i386 
-D_THREAD_SAFE -D_REENTRANT -o main main.cpp -arch i386 -arch i386
 /opt/local/lib/libfltk.a -lpthread -framework Carbon -framework 
ApplicationServices 

Undefined symbols:
  "vtable for Graph_lib::Window", referenced from:
      __ZTVN9Graph_lib6WindowE$non_lazy_ptr in cc1oxcSA.o
     (maybe you meant: __ZTVN9Graph_lib6WindowE$non_lazy_ptr)
  "vtable for Graph_lib::Button", referenced from:
      __ZTVN9Graph_lib6ButtonE$non_lazy_ptr in cc1oxcSA.o
 (maybe you meant: __ZTVN9Graph_lib6ButtonE$non_lazy_ptr)
  "Simple_window::Simple_window(Point, int, int, String const&)", referenced from:
  _main in cc1oxcSA.o
  "Graph_lib::Window::draw()", referenced from:
  vtable for Simple_windowin cc1oxcSA.o
  "typeinfo for Graph_lib::Window", referenced from:
  typeinfo for Simple_windowin cc1oxcSA.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

我不知道这意味着什么。我读了答案here (SO)。我创建了 .o 文件。我正在尝试使用 fltk-config 在 Mac OS 上编译它。

【问题讨论】:

  • 您没有正确链接而忘记了目标文件?您需要将它们包含在命令行中。
  • @pmr 可能,但我是初学者,如何包含它们?

标签: c++ fltk


【解决方案1】:

在我看来,当您调用编译器时,/opt/local/lib/libfltk.a 前面应该有一个 -l(破折号)。或者您可以将/opt/local/lib/libfltk.a 替换为-L/opt/local/lib -lfltk,这可能更传统。

【讨论】:

    【解决方案2】:

    我使用以下步骤获得了在 Linux 上运行的 FLTK 程序“12.3 A first example”:

    然后解压到 Programming-code/GUI 文件夹。

    • 在该文件夹中,将 #include <cstdlib> 添加到文件 std_lib_facilities.h 以避免在下一步中出现 atoi not declared 错误
    • 在 Programming-code/GUI 文件夹的命令行中运行 make。这应该会创建文件 libbookgui.a。
    • 假设程序名为 Example.cpp,运行以下命令:

      gcc `fltk-config --use-forms --use-gl --use-images --ldflags`Example.cpp libbookgui.a

    • 运行 a.out 可执行文件

    【讨论】:

      【解决方案3】:

      使用来自 PDF FLTK-Tutorial.pdf 中源代码的示例程序

      我必须添加以下几行才能在我的 Ubuntu Linux 中进行干净的编译。

      // 3 includes just below are not in the example but are required
      // for a clean compile
      #include <Fl/x.H>
      #include <stdlib.h>
      #include <stdio.h>
      

      您必须正确配置您的编译行。 FLTK 有 fltk-config 工具来帮助配置。

      fltk 配置
      获取 fltk-config 的帮助信息。阅读输出以确定您需要为编译、链接以及您正在使用的包的任何兼容性(gl、glut、表单等)输入的内容。

      将此信息复制到您的编译命令中。

      您也可以使用--compile prgrname.cxx 开关直接编译。包括 -g 因为您需要 gdb 支持。

      例如:

      fltk-config --cxxflags --ldflags
      

      给予(对我而言):

      -I/usr/include/freetype2 -g -O2 -D_THREAD_SAFE -D_REENTRANT -Wl,-Bsymbolic-functions -lfltk
      

      添加输出名称和输入程序:

      gcc -I/usr/include/freetype2 -g -O2 -D_THREAD_SAFE -D_REENTRANT -Wl,-Bsymbolic-functions -lfltk mousedrawtest.cpp mousedraw.cpp -o b.out
      

      虽然 FLTK 的学习内容较少,但它不适合胆小的人。 Erco (Greg Ercolano) 的教程非常出色,并且是许多常见挑战的示例代码。 http://seriss.com/people/erco/fltk/

      http://www.fltk.org/documentation.php/doc-1.1/basics.html

      还有很多其他不错的搜索:FLTK教程

      从简单的示例程序转换为真正的面向对象模型时,请注意范围,尤其是顶层窗口及其内容。

      今天,星期三,我对 gdb、范围和命名空间的了解比星期一要多。

      【讨论】:

        猜你喜欢
        • 2015-11-02
        • 2017-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-20
        • 2011-12-18
        • 1970-01-01
        • 2017-03-06
        相关资源
        最近更新 更多