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 -_-
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/
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)
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With