Stroustrup 的 Simple_window.h

2022-01-11 00:00:00 header c++ fltk

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

I am trying to get the graphics examples to work from Stroustrup's Principles and Practices ...C++, to no avail (yet). I have installed the fltk stuff, and know that is working fine as I managed to get a window to display using with a program suggested in the appendix of his book:

#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();
}

但是,我自己尝试使用他的 Simple_window.h(可以在他的网站上找到)给出对‘Window’的引用不明确",因为它已经在 usr/include/X11/X.h 中.所以我尝试将命名空间指定给相关的:

However, trying my own using his Simple_window.h (can be found on his site) gives "reference to ‘Window’ is ambiguous", since it's already at usr/include/X11/X.h . So I tried specifying the namespace to the relevant one:

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

.
.
.

但这给了我更多我不明白的错误:

But this gives me a bunch more errors I don't understand:

$ 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'

等等

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

I feel mastering graphics is going to be a long and rocky road -_-

推荐答案

对于处于同样困境的任何人,我把我所做的留在这里,最终能够编译并获得第 12.3 节 FLTK 的第一个程序的窗口Stroustrup 的著作《Programming: Principles and Practice using C++, 2nd Edition》.

To anyone in the same predicament, I leave here what I did to finally be able to compile and get the window of the first program with FLTK on section 12.3 of Stroustrup's book "Programming: Principles and Practice using C++, 2nd Edition".

在 Kubuntu 14.04 上安装 FLTK 后

After installing FLTK on Kubuntu 14.04 with

$ sudo apt install libfltk1.3-dev

我可以使用

$ fltk-config --compile fltkTest.cpp

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

Thanks to this post, I could see how I could finally get it on track with the first example of chapter 12. Comparing the command of cwivagg and Nathan with the command generated with fltk-config, I ended with this command

$ 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

我必须添加 -lfltk_images 和 -std=c++11

I had to add -lfltk_images and -std=c++11

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

However, now I had to deal with the errors that the compiler gave me. To get a working program, I had to do several changes to the sources that Stroustrup gave on http://www.stroustrup.com/Programming/PPP2code/

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

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

With all these changes I finally have the window as it should be, and the window close either with the Next button or the close button of the said window (with wait_for_button I needed to end the program from Konsole with Ctrl-c after I tried to close it with the close button of the window).

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

I hope the next person do not have to spend all the time I had to.

好吧,检查我的系统和编译命令,我意识到有几个重复的地毯......而且它们实际上不存在于我的 Kubuntu 系统中.所以,我必须在我的回答中写下我最终如何让窗口正常工作:

Well, checking at my system and the compiling command, I realized that there are several carpets repeated... and that they actually don't exist in my Kubuntu system. So, I have to write down in my answer what I finally do to get the window working:

获取目标文件:

$ clang++ -O2 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT -g -std=c++11 -c  Simple_window.cpp

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

To get the first program that we wanted

% 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

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

These are a hell lot easier (I almost can write them every time I need them)

相关文章