Stroustrup 的 Simple_window.h

发布于 2024-11-30 17:37:37 字数 1215 浏览 1 评论 0原文

我试图从 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();
}

但是,我尝试使用他的 Simple_window.h (可以在他的site) 给出“对 '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

.
.
.

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

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

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'

etc.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

音栖息无 2024-12-07 17:37:37

对于任何处于同样困境的人,我将我所做的事情留在此处,以便最终能够在 Stroustrup 的书“编程:使用 C++ 的原理和实践,第二版”的第 12.3 节中使用 FLTK 编译并获取第一个程序的窗口。

在 Kubuntu 14.04 上安装 FLTK 后,

$ sudo apt install libfltk1.3-dev

我可以使用 编译附录 D 上的示例程序,

$ fltk-config --compile fltkTest.cpp

感谢这篇文章,我可以看到如何最终使其与第 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

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

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

  1. 我在 Graph.h 上取消注释 std_lib_facilities.h
  2. 为了解决 Window 的歧义,我需要在线指定 Graph_lib::Window Simple_window.h std_lib_facilities.h 的第 9 行
  3. 第 107 和 113 行使用 i<0 比较,当 i 为
    未签名(但这些只是警告)。
  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 和下一步。顺便说一句, wait_for_button 不起作用
    任何一种形式。
  7. 在 GUI.cpp 中,对 Menu 的构造函数进行了另一个重新定义。我
    注释掉了。
  8. 我更改了12.3节示例的最后一行

    win.wait_for_button;

    FL::运行();
    这是我从附录 D 中的示例中获取的,因为否则窗口会
    不通过其关闭按钮关闭。

经过所有这些更改,我终于有了应有的窗口,并且窗口可以使用“下一步”按钮或所述窗口的关闭按钮关闭(使用 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

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

% 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

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

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".

After installing FLTK on Kubuntu 14.04 with

$ sudo apt install libfltk1.3-dev

I could compile the example program on Appendix D with the use of

$ fltk-config --compile fltkTest.cpp

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

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

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. I uncommented std_lib_facilities.h on Graph.h
  2. To resolve the ambiguity of Window, I needed to specify Graph_lib::Window on line 9 of Simple_window.h
  3. std_lib_facilities.h on lines 107 and 113 uses a i<0 comparison when i is
    unsigned (but these are just warnings).
  4. Graph.h line 159 uses fl_color() but the compiler says it should be Fl_Color
  5. I needed to uncomment the constructors for Point in Point.h
  6. There are several redefinitions on Simple_window.cpp of Simple_window.h
    On Simple_window.cpp I commented out the definitions for the constructor,
    cb_next and wait_for_button (which is not the same as the one on
    Simple_window.h). On Simple_window.h I commented out the definitions of
    wait_for_button and next. By the way, wait_for_button does not work in
    either form.
  7. In GUI.cpp there is another redefinition for the constructor of Menu. I
    commented it out.
  8. I changed the last line of the example of section 12.3
    from
    win.wait_for_button;
    to
    Fl::run();
    which I took from the example on Appendix D, because otherwise the window does
    not close with its close button.

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.

Edit: 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:

To get an object file:

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

音盲 2024-12-07 17:37:37

Tomolak,当你说这“取得了进展”时,我感到非常高兴。不知道你是不是在讽刺,但无论如何。

我已经解决了这个问题(或者至少我已经设法让一个窗口出现,其中有一个三角形)。然而,这只是在注释掉并编辑了 Stroustrup 的大部分代码之后。我觉得他的书不太适合初学者。我也不建议尝试使用 Linux 来编译他的任何示例。

对于任何在谷歌上搜索这些问题的人,我的最终解决方案是这个命令:

$ g++ -Wno-deprecated -I/usr/local/include -I/usr/include/freetype2 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT -o 'windows_working' win_test.cpp Graph.cpp GUI.cpp Simple_window.cpp Window.cpp  /usr/local/lib/libfltk.a -lXext -lXft -lfontconfig -lXinerama -lpthread -ldl -lm -lX11

这包括与 fltk 和 Stroustrup 相关的所有内容。在这里,我的程序是 win_test.cpp,输出是 windows_working。我通过 fltk 文件提供的 shell 脚本获得了这个,并将其放入 /usr/inc/bin 中。它称为 fltk-config。

另外,有用的提示是:从他们的网站下载 fltk 源代码,而不仅仅是从 Stroustrup 网站下载 FL 源代码。然后阅读自述文件并严格按照说明进行操作,然后尝试本书附录 D 中的测试程序。然后反复尝试他的示例代码来修复您发现的错误,直到它起作用为止。
如果您认为我可以提供帮助或想知道我如何获得解决方案,请给我发送电子邮件(但我是新手,因此不太可能有用)。

Tomolak, when you said this 'made progress', it pleased me greatly. Don't know if you were being sarcastic, but whatever.

I have solved this problem (or at least I have managed to get a window to appear with a triangle in it). However, this was only after commenting out and editing large portions of Stroustrup's code. I do not feel his book is very suitable for a beginner. I would also not recommend trying to compile any of his examples using Linux.

To anyone googling these issues, my final solution was this command:

$ g++ -Wno-deprecated -I/usr/local/include -I/usr/include/freetype2 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT -o 'windows_working' win_test.cpp Graph.cpp GUI.cpp Simple_window.cpp Window.cpp  /usr/local/lib/libfltk.a -lXext -lXft -lfontconfig -lXinerama -lpthread -ldl -lm -lX11

This includes everything that is required with respect to the fltk stuff and Stroustrup stuff. Here, my program is win_test.cpp and the output is windows_working. I obtained this looking through the shell script provided with the fltk files and put in /usr/inc/bin. It is called fltk-config.

Also, helpful hints are: download the fltk source from their site, not just the FL one from Stroustrup's site. Then read the readme and follow the instructions exactly before trying the test program in appendix D of the book. Then try his example code repeatedly fixing the errors you find until it works.
If you think I could help or want to know how I got my solution, email me (but I am a newb and so am unlikely to be of use).

森林散布 2024-12-07 17:37:37

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

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

似乎更像是这样。但我没有这方面的实际经验。

Well this doesn't really have anything to do with graphics as such. Problem seems to be that you've only included on your command line one of the source files you need to compile. Judging by his web site

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

seems to be more like it. But I have no actual experience of this.

晚雾 2024-12-07 17:37:37

内森,

您的回答对我在实现斯特劳斯特鲁普的简单三角形程序的过程中非常有帮助。我想根据您的解决方案发布我自己的解决方案。通过对 Stroustrup 的代码进行一处更改并大大扩展编译脚本,我就出现了三角形。

g++ -I/usr/localinclude -I/usr/local/include/FL/images -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_THREAD_SAFE -D_REENTRANT -o 'myimplementationofstroustrup.o' 'myimplementationofstroustrup.cpp' 'Simple_window.cpp' '图.cpp' 'GUI.cpp' 'Window.cpp' /usr/local/lib/libfltk.a -lpthread -ldl -lm -lX11 -L/usr/local/lib -lfltk_images -lfltk_png -lfltk_z -lfltk_jpeg -lfltk

与您的差异如下:

1)我必须在所有 .cpp 文件中添加引号...也许是系统差异?
2)我必须在最后添加六个您没有使用的标志,否则我会收到更多“未定义的引用”错误。
3) 由于不明确的引用错误,我必须在 Simple_window.h 的第 17 行指定“Graph_lib::Window”。这是我必须对 Stroustrup 的代码进行的唯一更改。

Nathan,

Your answer was immensely helpful to me in my own struggle to implement Stroustrup's simple triangle program. I would like to post my own solution based on yours. With one change to Stroustrup's code and a greatly expanded compile script, I got the triangle to appear.

g++ -I/usr/localinclude -I/usr/local/include/FL/images -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_THREAD_SAFE -D_REENTRANT -o 'myimplementationofstroustrup.o' 'myimplementationofstroustrup.cpp' 'Simple_window.cpp' 'Graph.cpp' 'GUI.cpp' 'Window.cpp' /usr/local/lib/libfltk.a -lpthread -ldl -lm -lX11 -L/usr/local/lib -lfltk_images -lfltk_png -lfltk_z -lfltk_jpeg -lfltk

The differences with yours are as follows:

1) I had to add quotes to all my .cpp files... system difference, perhaps?
2) I had to add six flags at the end that you didn't use, or else I got more "undefined reference" errors.
3) I had to specify "Graph_lib::Window" on Line 17 of Simple_window.h because of an ambiguous reference error. This was the only change I had to make to Stroustrup's code.

心如狂蝶 2024-12-07 17:37:37

问题和修复的总结对于使其在不同平台上运行也非常有帮助。实际更正后的代码可供下载:

FLTK问题编译 - PPP2ndEd< /a>

This summary of issues and fixes is also very helpful in getting it to work on the different platforms. Actual corrected code is available for download:

FLTK issue compiling - PPP2ndEd

初熏 2024-12-07 17:37:37

在本书的论坛的一个帖子中,有一个关于如何解决此问题的非常详细的演练:
https://groups.google.com/forum/ #!msg/ppp-public/BtlzdWGuQpQ/KuDN4u-SPKgJ

基于此,我创建了一个 github 存储库,该存储库已实现所有功能这些更改等,并包括有关如何构建项目的详细信息:

https://github.com/cortical -iv/hello_fltk

我在这个该死的问题上花了大约两周的时间,我的知识(很大程度上是从其他人那里获得的)坦率地确实体现在该代码中,并且会不断更新。但这是堆栈溢出,因此就我必须对代码进行的更改而言,大多数都是在该论坛上找到的,但由于给出链接答案不太好,所以它们是:

Simple_window.h

  1. 解决命名空间冲突struct Simple_window : Window { 更改为
    struct Simple_window : Graph_lib::Window {

  2. Simple_window() 转换为声明(删除定义)。

  3. wait_for_button() 从定义转换为声明(在 while 循环中注释掉整个 def),并从 void 更改为 bool,以与 Simple_window.cpp 中的定义保持一致。

  4. cb_next(...) 转变为声明

  5. void next() 转变 为声明 到声明

Graph.h
1. 取消注释#include "std_lib_facilities.h"
2. 将 fl_color 更改为 Fl_Color(~第 159 行)

Graph.cpp
1. 将 can_open 替换为:
bool can_open(const string& s)
// 检查名为 s 的文件是否存在并且可以打开读取
{
ifstream ff(s);
返回 ff.is_open();
}

Point.h
1.取消注释构造函数

Point(int xx, int yy) : x(xx), y(yy) { }    
Point() :x(0), y(0) { }

Gui.h
1. 在 Menu : Widget 中,将 Menu(Point xy ...) 更改为声明而不是 def,注释掉 Widget def 内容。

完成上述操作后,如果您在终端运行以下命令,它应该可以编译:

g++ -w -Wall -std=c++11 Graph.cpp Window.cpp GUI.cpp Simple_window.cpp main.cpp `fltk-config --ldflags --use-images` -o hello_fltk

如果您使用 cmake 安装了 fltk,那么您还可以使用 cmake/make 构建示例:有一个 CMakeLists.txt< /code> 存储库中的文件。

There is a very detailed walkthrough of how to fix this in a thread at the book's forum:
https://groups.google.com/forum/#!msg/ppp-public/BtlzdWGuQpQ/KuDN4u-SPKgJ

Based on that I made a github repository that has implemented all those changes and more, and includes details on how to build the project:

https://github.com/cortical-iv/hello_fltk

I have spent about two weeks on this dang problem, and my knowledge (which is largely gained from others) is really embodied in that code frankly and will be updated constantly. But this is stack overflow, so in terms of changes I had to make to the code, most are found at that forum but since it isn't nice to give link answers, here they are:

Simple_window.h

  1. Resolve namespace clash Change struct Simple_window : Window { to
    struct Simple_window : Graph_lib::Window {

  2. Simple_window() converted to declaration (remove definition).

  3. Convert wait_for_button() from definition to declaration (comment out entire def in while loop) and change from void to bool to be consistent with definition in Simple_window.cpp.

  4. Turned cb_next(...) to declaration

  5. Turn void next() to declaration

Graph.h
1. Uncomment #include "std_lib_facilities.h"
2. Change fl_color to Fl_Color (~ line 159)

Graph.cpp
1. Replace can_open with:
bool can_open(const string& s)
// check if a file named s exists and can be opened for reading
{
ifstream ff(s);
return ff.is_open();
}

Point.h
1. Uncomment constructors

Point(int xx, int yy) : x(xx), y(yy) { }    
Point() :x(0), y(0) { }

Gui.h
1. In Menu : Widget, change Menu(Point xy ...) to declaration rather than def, comment out Widget def stuff.

Once you've done the above, it should compile if you run the following command at your terminal:

g++ -w -Wall -std=c++11 Graph.cpp Window.cpp GUI.cpp Simple_window.cpp main.cpp `fltk-config --ldflags --use-images` -o hello_fltk

If you installed fltk using cmake, then you can also build the example using cmake/make: there is a CMakeLists.txt file at the repository.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文