Stroustrup's Simple_window.h
Asked Answered
C

6

8

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 -_-

Calida answered 20/8, 2011 at 19:11 Comment(3)
It looks like you never defined Simple_window::Simple_window(Point, int, int, String const&) Did you define that anywhere?Artificiality
You were correct to use a qualified name. This made progress. Now your code compiles but your project will not link. It looks like you are not providing a definition for Simple_window's constructor, and you have virtual functions in Graph_lib::Window but no virtual destructor defined. Perhaps you just failed to link all the right .o files together. These things have nothing to do with graphics in particular.Fascism
Your 'made progress' made me feel disproportionately happy for some reason. Problem fixed, window works (fiiiiinally after three days). Now to wait 6 hours to answer my own qn so people can find it if they have the same problemsCalida
R
5

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)

Ringer answered 31/3, 2016 at 1:23 Comment(0)
C
2

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

Calida answered 21/8, 2011 at 19:48 Comment(0)
S
1

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.

Subterranean answered 20/8, 2011 at 19:20 Comment(3)
"Included" is misleading here.Fascism
Aah, you might be right - including these I get redefinition errors instead - surely a step forward!Calida
‘strlen’ was not declared in this scope is what I get now... So I need to go add it to the Graph_lib scope then I suppose? Seems unlikely it wasn't picked up on until now. I feel like an idiot for not including all the source files.Calida
Q
1

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.

Quartus answered 10/7, 2014 at 12:9 Comment(1)
I am glad my answer was helpful to you. I can't guess why you had to add single quotes to your code - maybe the terminal was expanding something in them or something...Calida
D
0

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

Dorseydorsiferous answered 11/7, 2017 at 18:39 Comment(2)
Link-only answers are discouraged, since the linked material might disappear. It’s better to copy and adapt the relevant material into the answer, while keeping the link for further reference.Kale
While normally link-only answers are indeed discouraged, I absolutely strongly encourage people to go to that link because it is the most thorough walkthrough of how to fix this mess of code that you can find. To reproduce it here would be very difficult. There are just too many mistakes in Stroustrup's code to turn it into a good answer. I'm working on it right now.Durance
E
0

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.

Eyelash answered 12/6, 2018 at 3:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.