Search code examples
c++c++11makefilefltk

Getting lots of error messages like "undefined reference to..." using FLTK with g++


I learn c++ following The Stroustroup's book (Programming: Principles and Practice), so I've been trying to run an example using FLTK libraries (I use Ubuntu 14.04). Actually, when I compiled the programm with Code::Blocks IDE everything worked fine. But then, sort of for learning purposes, I decided to write a makefile, which causes many errors when trying to start it:

>     g++ main.o Graph.o GUI.o Simple_window.o Window.o -o hello main.o: In function `fl_color()':
>     main.cpp:(.text._Z8fl_colorv[_Z8fl_colorv]+0x7): undefined reference
>     to `fl_graphics_driver' main.o: In function
>     `Graph_lib::Window::~Window()':
>     main.cpp:   (.text._ZN9Graph_lib6WindowD2Ev[_ZN9Graph_lib6WindowD5Ev]+0x31):
>     undefined reference to `Fl_Window::~Fl_Window()'
>     main.o:(.rodata._ZTV13Simple_window[_ZTV13Simple_window]+0x28):
>     undefined reference to `Fl_Window::handle(int)'
>     main.o:(.rodata._ZTV13Simple_window[_ZTV13Simple_window]+0x30):
>     undefined reference to `Fl_Window::resize(int, int, int, int)'
>     main.o:(.rodata._ZTV13Simple_window[_ZTV13Simple_window]+0x38):
>     undefined reference to `Fl_Window::show()'
>     main.o:(.rodata._ZTV13Simple_window[_ZTV13Simple_window]+0x40):
>     undefined reference to `Fl_Window::hide()'
>     main.o:(.rodata._ZTV13Simple_window[_ZTV13Simple_window]+0x60):
>     undefined reference to `Fl_Window::flush()' Graph.o: In function
>     `Graph_lib::Text::draw_lines() const': Graph.cpp:(.text+0x9e5):
>     undefined reference to `fl_draw(char const*, int, int)' Graph.o: In
>     function `Graph_lib::draw_mark(Graph_lib::Point, char)':
>     Graph.cpp:(.text+0x16e2): undefined reference to `fl_draw(char const*,
>     int, int)' Graph.o: In function
>     `Graph_lib::Image::Image(Graph_lib::Point, std::string,
>     Graph_lib::Suffix::Encoding)': Graph.cpp:(.text+0x1f95): undefined
>     reference to `Fl_JPEG_Image::Fl_JPEG_Image(char const*)'
>     Graph.cpp:(.text+0x1fc9): undefined reference to
>    `Fl_GIF_Image::Fl_GIF_Image(char const*)' Graph.cpp:(.text+0x1ffd):
>     undefined reference to `Fl_BMP_Image::Fl_BMP_Image(char const*)'
>     Graph.o: In function `Fl_Image::Fl_Image(int, int, int)':
>     Graph.cpp:(.text._ZN8Fl_ImageC2Eiii[_ZN8Fl_ImageC5Eiii]+0x18):
>     undefined reference to `vtable for Fl_Image' Graph.o: In function
>     `fl_color(unsigned int)':
>     Graph.cpp:(.text._Z8fl_colorj[_Z8fl_colorj]+0xe): undefined reference
>     to `fl_graphics_driver'
>     Graph.cpp:(.text._Z8fl_colorj[_Z8fl_colorj]+0x21): undefined reference
>     to `fl_graphics_driver' Graph.o: In function `fl_line_style(int, int,
>     char*)':
>     Graph.cpp:(.text._Z13fl_line_styleiiPc[_Z13fl_line_styleiiPc]+0x15):
>     undefined reference to `fl_graphics_driver'
>     Graph.cpp:(.text._Z13fl_line_styleiiPc[_Z13fl_line_styleiiPc]+0x26):
>     undefined reference to `fl_graphics_driver' Graph.o: In function
>     `fl_rect(int, int, int, int)':
>     Graph.cpp:(.text._Z7fl_rectiiii[_Z7fl_rectiiii]+0x17): undefined
>     reference to `fl_graphics_driver'
>     Graph.o:Graph.cpp:(.text._Z7fl_rectiiii[_Z7fl_rectiiii]+0x28): more
>     undefined references to `fl_graphics_driver' follow Graph.o: In
>     function `Graph_lib::Bad_image::draw(int, int, int, int, int, int)':
>     Graph.cpp:(.text._ZN9Graph_lib9Bad_image4drawEiiiiii[_ZN9Graph_lib9Bad_image4drawEiiiiii]+0x2d): undefined reference to `Fl_Image::draw_empty(int, int)'
>     Graph.o:(.rodata._ZTVN9Graph_lib9Bad_imageE[_ZTVN9Graph_lib9Bad_imageE]+0x20):
>     undefined reference to `Fl_Image::copy(int, int)'
>     Graph.o:(.rodata._ZTVN9Graph_lib9Bad_imageE[_ZTVN9Graph_lib9Bad_imageE]+0x28):
>     undefined reference to `Fl_Image::color_average(unsigned int, float)'
>     Graph.o:(.rodata._ZTVN9Graph_lib9Bad_imageE[_ZTVN9Graph_lib9Bad_imageE]+0x30):
>     undefined reference to `Fl_Image::desaturate()'
>     Graph.o:(.rodata._ZTVN9Graph_lib9Bad_imageE[_ZTVN9Graph_lib9Bad_imageE]+0x38):
>     undefined reference to `Fl_Image::label(Fl_Widget*)'
>     Graph.o:(.rodata._ZTVN9Graph_lib9Bad_imageE[_ZTVN9Graph_lib9Bad_imageE]+0x40):
>     undefined reference to `Fl_Image::label(Fl_Menu_Item*)'
>     Graph.o:(.rodata._ZTVN9Graph_lib9Bad_imageE[_ZTVN9Graph_lib9Bad_imageE]+0x50):
>     undefined reference to `Fl_Image::uncache()' Graph.o: In function
>     `Graph_lib::Bad_image::~Bad_image()':
>     Graph.cpp:(.text._ZN9Graph_lib9Bad_imageD2Ev[_ZN9Graph_lib9Bad_imageD5Ev]+0x1f):
>     undefined reference to `Fl_Image::~Fl_Image()'
>     Graph.o:(.rodata._ZTIN9Graph_lib9Bad_imageE[_ZTIN9Graph_lib9Bad_imageE]+0x10):
>     undefined reference to `typeinfo for Fl_Image' GUI.o: In function
>     `Graph_lib::Button::attach(Graph_lib::Window&)': GUI.cpp:(.text+0x5b):
>     undefined reference to `Fl_Button::Fl_Button(int, int, int, int, char
>     const*)' GUI.o: In function
>     `Graph_lib::In_box::attach(Graph_lib::Window&)':
>     GUI.cpp:(.text+0x1ed): undefined reference to `Fl_Input::Fl_Input(int,
>     int, int, int, char const*)' GUI.o: In function
>     `Graph_lib::Out_box::put(int)': GUI.cpp:(.text+0x2cf): undefined
>     reference to `Fl_Input_::value(char const*)' GUI.o: In function
>     `Graph_lib::Out_box::put(std::string const&)': GUI.cpp:(.text+0x365):
>     undefined reference to `Fl_Input_::value(char const*)' GUI.o: In
>     function `Graph_lib::Out_box::attach(Graph_lib::Window&)':
>     GUI.cpp:(.text+0x3cb): undefined reference to
>     `Fl_Output::Fl_Output(int, int, int, int, char const*)'
>     Simple_window.o: In function `Simple_window::wait_for_button()':
>     Simple_window.cpp:(.text+0x31): undefined reference to `Fl::wait()'
>     Simple_window.cpp:(.text+0x48): undefined reference to `Fl::redraw()'
>     Window.o: In function `Graph_lib::Window::Window(int, int, std::string
>     const&)': Window.cpp:(.text+0x34): undefined reference to
>     `Fl_Window::Fl_Window(int, int, char const*)' Window.cpp:(.text+0x9f):
>     undefined reference to `Fl_Window::~Fl_Window()' Window.o: In function
>     `Graph_lib::Window::Window(Graph_lib::Point, int, int, std::string
>     const&)': Window.cpp:(.text+0xf8): undefined reference to
>     `Fl_Window::Fl_Window(int, int, int, int, char const*)'
>     Window.cpp:(.text+0x163): undefined reference to
>     `Fl_Window::~Fl_Window()' Window.o: In function
>     `Graph_lib::Window::draw()': Window.cpp:(.text+0x1c7): undefined
>     reference to `Fl_Window::draw()' Window.o: In function
>     `Graph_lib::Window::attach(Graph_lib::Widget&)':
>     Window.cpp:(.text+0x23a): undefined reference to `Fl_Group::begin()'
>     Window.cpp:(.text+0x264): undefined reference to `Fl_Group::end()'
>     Window.o: In function `Graph_lib::gui_main()':
>     Window.cpp:(.text+0x485): undefined reference to `Fl::run()'
>     Window.o:(.rodata._ZTVN9Graph_lib6WindowE[_ZTVN9Graph_lib6WindowE]+0x28):
>     undefined reference to `Fl_Window::handle(int)'
>     Window.o:(.rodata._ZTVN9Graph_lib6WindowE[_ZTVN9Graph_lib6WindowE]+0x30):
>     undefined reference to `Fl_Window::resize(int, int, int, int)'
>     Window.o:(.rodata._ZTVN9Graph_lib6WindowE[_ZTVN9Graph_lib6WindowE]+0x38):
>     undefined reference to `Fl_Window::show()'
>     Window.o:(.rodata._ZTVN9Graph_lib6WindowE[_ZTVN9Graph_lib6WindowE]+0x40):
>     undefined reference to `Fl_Window::hide()'
>     Window.o:(.rodata._ZTVN9Graph_lib6WindowE[_ZTVN9Graph_lib6WindowE]+0x60):
>     undefined reference to `Fl_Window::flush()'
>     Window.o:(.rodata._ZTIN9Graph_lib6WindowE[_ZTIN9Graph_lib6WindowE]+0x10):
>     undefined reference to `typeinfo for Fl_Window' collect2: error: ld
>     returned 1 exit status make: *** [hello] error 1

And here is my makefile:

CC=g++

CFLAGS=-c -Wall -std=c++11 `fltk-config --cxxflags --use-images`
LIBS=`fltk-config --ldflags` -lfltk -lfltk_images -ljpeg -lstdc++ -lXfixes -lXext -lpthread -ldl -lm -lX11

all: hello

hello: main.o Graph.o GUI.o Simple_window.o Window.o
    $(CC) main.o Graph.o GUI.o Simple_window.o Window.o -o hello

main.o: main.cpp
    $(CC) $(CFLAGS) main.cpp $(LIBS)

Graph.o: Graph.cpp
    $(CC) $(CFLAGS) Graph.cpp $(LIBS)

GUI.o: GUI.cpp
    $(CC) $(CFLAGS) GUI.cpp $(LIBS)

Simple_window.o: Simple_window.cpp
    $(CC) $(CFLAGS) Simple_window.cpp $(LIBS)

Window.o: Window.cpp
    $(CC) $(CFLAGS) Window.cpp $(LIBS)

clean:
    rm -rf *.o hello

Could anyone tell me what I'm doing wrong? I saw in the Web many similar topics, but no answer from them seems to help me.

Thanks so much in advance!


Solution

  • You need to pass LIBS to linker instead of compiler, this would be your makefile:

    CC=g++
    
    CFLAGS=-c -Wall -std=c++11 `fltk-config --cxxflags --use-images`
    LIBS=`fltk-config --ldflags` -lfltk -lfltk_images -ljpeg -lstdc++ -lXfixes -lXext -lpthread -ldl -lm -lX11
    
    all: hello
    
    hello: main.o Graph.o GUI.o Simple_window.o Window.o
        $(CC) main.o Graph.o GUI.o Simple_window.o Window.o -o hello $(LIBS)
    
    main.o: main.cpp
        $(CC) $(CFLAGS) main.cpp
    
    Graph.o: Graph.cpp
        $(CC) $(CFLAGS) Graph.cpp
    
    GUI.o: GUI.cpp
        $(CC) $(CFLAGS) GUI.cpp
    
    Simple_window.o: Simple_window.cpp
        $(CC) $(CFLAGS) Simple_window.cpp
    
    Window.o: Window.cpp
        $(CC) $(CFLAGS) Window.cpp
    
    clean:
        rm -rf *.o hello