Saturday, June 4, 2016

Getting Stroustrup's FLTK code to work in Code::Blocks for Windows

The following versions were used: fltk v1.3.3, Code::Blocks 16.01, and Windows 7

To get FLTK working with Code::Blocks in Windows, follow instructions found here: http://watashigaseigida.blogspot.com.au/2016/01/using-fltk-with-codeblocks-for-windows.html

Below is the solution to getting the code from '12.3 A first example' to work found in Bjarne's Stroustrup's book: Programming: Principles and Practices using C++, second edition. The solution below is a step by step breakdown of the errors I encountered and how to fix them.


Create a new FLTK project. The following files should be in the project as shown below. All the files can be found here (http://stroustrup.com/Programming/PPP2code/) with the exception of main.cpp, which you create yourself.



Compile and check for errors. Compile after correcting for each error encountered.

Error 1: 'vector' does not name a type
Solution: uncomment line 10 in Graph.h.


Error 2: no matching function for call to 'Graph_lib::Point::Point(int, int)'
Solution: uncomment constructors in line 11 and 12 in Point.h


Error 3: cannot convert std::ifstream to 'bool' in return
Solution: Change line 313 in Graph.cpp to return (bool) ff;


Error 4: redefinition of 'Graph_lib::Menu::Menu(Graph_lib::Point, int, int, Graph_lib::Menu::Kind, const string&)'
Solution: in GUI.h, comment out line 96 and 97 and add a semicolon to the  end of line 95


Error 5: reference to 'Window' is ambiguous
Solution: add Graph_lib:: before Window in line 9 of Simple_window.h


Error 6: reference to Polygon is ambiguous
Solution: add Graph_lib:: before Polygon in line 12 of main.cpp


Error 7: undefined reference to 'Fl_JPEG_Image::Fl_JPEG_Image(char const*)'
Solution: Go to Settings  Compiler  Linker settings  Other linker options.
Add the following lines to Other linker options
-lfltk_images
-lfltk_jpeg


Alternatively, instead of applying the -lfltk_images and -lfltk_jpeg to the global compiler, one can add them to individual projects. Right click the project name   Properties   Project settings tab   Project's build options   Linker settings   Other linker options.


Sources:
1. Almost all the solutions to the compilation errors can be found here: https://groups.google.com/forum/#!topic/ppp-public/BtlzdWGuQpQ
2. https://bewuethr.github.io/installing-fltk-133-under-visual-studio/ This link suggests not to include simple_window.cpp in your project, which gets rid of a lot of errors. 
3. http://epweb2.ph.bham.ac.uk/user/hillier/course3cpp/fltkegs/images/fltkimages.html
Basic example of adding images using FLTK, with the suggestion that you should add -lfltk_images and -lfltk_jpeg to your linker options

No comments:

Post a Comment