Search code examples
c++g++fltk

Undefined reference using FLTK with g++


So I have been trying to set up the FLTK library for use with g++. I have installed FLTK and I can compile the test example in its documentation without error. I am following Bjarne Stroustrups Programming: Principles and Practice, 2nd edition, and for the 12th chapter you need to get FLTK going.

I had a large number of issues that had to be resolved since the code Stroustrup provides (and that you need to make work for his examples) doesn't seem to function with current versions of FLTK, and I think I managed to do that, but currently I seem to have a linker problem that my probably somewhat under qualified guess suggests has to do with FLTK and how I installed it rather than any errors in the by Stroustrup provided code. The line I am using the compile my test code (Helloworld.cpp) is the following:

 g++ -std=c++11 -Wextra -pedantic -I/usr/local/include -I/usr/local/include/FL/images -I/usr/include/freetype2 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT Helloworld.cpp src/Graph.cpp src/GUI.cpp src/Window.cpp -L/usr/local/lib -lfltk -lXfixes -lXext -lXft -lfontconfig -lpthread -ldl -lm -lX11 -ljpeg -Os -Wall -Wunused -Wno-format-y2k -fno-exceptions -fno-strict-aliasing -ffunction-sections -fdata-sections -fexceptions -o HelloWorld

which is just using the fltk-config --cxxflags, --use-images,--ldflags and --optim options. I added the -fexceptions at the end because the code makes use of exceptions but the --optim flag doesen't seem to like them since it adds -fno-exceptions.

But when I run that line I get the following error message:

/tmp/ccD2Ev5l.o: In function `Graph_lib::Image::Image(Graph_lib::Point, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Graph_lib::Suffix::Encoding)':
Graph.cpp:(.text._ZN9Graph_lib5ImageC2ENS_5PointENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_6Suffix8EncodingE+0x119): undefined reference to `Fl_JPEG_Image::Fl_JPEG_Image(char const*)'
Graph.cpp:(.text._ZN9Graph_lib5ImageC2ENS_5PointENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_6Suffix8EncodingE+0x137): undefined reference to `Fl_GIF_Image::Fl_GIF_Image(char const*)'
collect2: error: ld returned 1 exit status

Those Fl_JPEG_Image/Fl_GIF_Image functions are declared in the FLTK included headerfiles, but my guess is that there might be a library component missing where the actual definition would reside. But I don't know where to get it. Any suggestions on how to fix this?


Solution

  • Add -lfltk_images. Those image related functions have their own library.