Stroustrup的Simple_window.h

Nat*_*ton 6 c++ header fltk

我试图从Stroustrup的原理和实践... C++中获取图形示例,但无济于事.我已经安装了fltk的东西,并知道它工作正常,因为我设法得到一个窗口显示使用他的书的附录中建议的程序:

#include <FL/Fl.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Window.H>

int main(){

    Fl_Window window(200,200, "title here");
    Fl_Box box(0,0,200,200,"Hey, hello wrld");
    window.show();
    return Fl::run();
}
Run Code Online (Sandbox Code Playgroud)

然而,尝试使用他的Simple_window.h(可以在他的网站上找到)给出"对'Window'的引用是模棱两可的",因为它已经在usr/include/X11/Xh.所以我尝试将命名空间指定为相关的命名空间:

struct Simple_window : Graph_lib::Window {  //Changed Window to inc. namespace
    Simple_window(Point xy, int w, int h, const string& title );

    bool wait_for_button(); // simple event loop

.
.
.
Run Code Online (Sandbox Code Playgroud)

但这给了我一堆我不理解的错误:

$ clear; g++ -Wno-deprecated window.cpp -o holz
    /tmp/ccIFivNg.o: In function `main':
    window.cpp:(.text+0x64): undefined reference to `Simple_window::Simple_window(Point, int, int, String const&)'
    /tmp/ccIFivNg.o: In function `Graph_lib::Window::~Window()':
    window.cpp:(.text._ZN9Graph_lib6WindowD2Ev[_ZN9Graph_lib6WindowD5Ev]+0x14): undefined reference to `vtable for Graph_lib::Window'
Run Code Online (Sandbox Code Playgroud)

等等

我觉得掌握图形将是一条漫长而艰难的道路-_-

Gio*_*iES 5

对于处于同样困境的任何人,我在 Stroustrup 的书“编程:使用 C++ 的原则和实践,第 2 版”的第 12.3 节中留下了我为最终能够使用 FLTK 编译并获得第一个程序的窗口所做的工作。

在 Kubuntu 14.04 上安装 FLTK 后

$ sudo apt install libfltk1.3-dev
Run Code Online (Sandbox Code Playgroud)

我可以使用以下命令编译附录 D 中的示例程序

$ fltk-config --compile fltkTest.cpp
Run Code Online (Sandbox Code Playgroud)

多亏了这篇文章,我可以看到如何最终通过第 12 章的第一个示例使其步入正轨。将 cwivagg 和 Nathan 的命令与使用 fltk-config 生成的命令进行比较,我以这个命令结束

$ clang++ -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 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT -Wl,-Bsymbolic-functions -lfltk_images -lfltk -lX11 -std=c++11 -o 's12_3_first' 's12_3_first.cpp' Simple_window.cpp Graph.cpp GUI.cpp Window.cpp
Run Code Online (Sandbox Code Playgroud)

我不得不添加 -lfltk_images 和 -std=c++11

但是,现在我必须处理编译器给我的错误。为了获得一个有效的程序,我不得不对 Stroustrup 在http://www.stroustrup.com/Programming/PPP2code/上提供的资源进行一些更改

  1. 我在 Graph.h 上取消了 std_lib_facilities.h 的注释
  2. 为了解决Window的歧义,我需要在Simple_window.h的第9行指定Graph_lib::Window
  3. 当 i 未签名时,第 107 和 113 行的 std_lib_facilities.h 使用 i<0 比较(但这些只是警告)。
  4. Graph.h 第 159 行使用 fl_color() 但编译器说它应该是 Fl_Color
  5. 我需要取消注释 Point.h 中 Point 的构造函数
  6. Simple_window.h 的 Simple_window.cpp 上有几个重定义 在 Simple_window.cpp 上,我注释掉了构造函数 cb_next 和 wait_for_button(与 Simple_window.h 上的不同)的定义。在 Simple_window.h 上,我注释掉了 wait_for_button 和 next 的定义。顺便说一下,wait_for_button 不能以任何一种形式工作。
  7. 在 GUI.cpp 中,还有另一个对 Menu 构造函数的重新定义。我注释掉了。
  8. 我把 12.3 节例子的最后一行从 win.wait_for_button 改了;到 Fl::run(); 这是我从附录 D 中的示例中获取的,因为否则窗口不会通过其关闭按钮关闭。

通过所有这些更改,我终于拥有了应有的窗口,并且该窗口使用 Next 按钮或上述窗口的关闭按钮关闭(使用 wait_for_button 我需要在我尝试后使用 Ctrl-c 从 Konsole 结束程序用窗口的关闭按钮关闭它)。

我希望下一个人不必花费我不得不花费的所有时间。

编辑:嗯,检查我的系统和编译命令,我意识到有几个地毯重复......而且它们实际上不存在于我的 Kubuntu 系统中。所以,我必须在我的答案中写下我最终为使窗口工作所做的工作:

获取目标文件:

$ clang++ -O2 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT -g -std=c++11 -c  Simple_window.cpp
Run Code Online (Sandbox Code Playgroud)

获得我们想要的第一个程序

% clang++ -O2 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT -Wl,-Bsymbolic-functions -lfltk_images -lfltk -lX11 -g -std=c++11 Simple_window.o Graph.o GUI.o Window.o -o z3 s12_3_first.cpp
Run Code Online (Sandbox Code Playgroud)

这些要容易得多(我几乎可以在每次需要时编写它们)


joh*_*ohn 1

嗯,这与图形本身没有任何关系。问题似乎是您只在命令行中包含了需要编译的源文件之一。从他的网站来看

g++ graph.cpp GUI.cpp Simple_window.cpp Window.cpp

似乎更喜欢它。但我没有这方面的实际经验。