使用带有 g++ 的 FLTK 获取大量错误消息,例如 "undefined reference to..."

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

我根据 The Stroustroup 的书(编程:原理与实践)学习了 c++,所以我一直在尝试 运行 使用 FLTK 库的示例(我使用 Ubuntu 14.04)。 实际上,当我用 Code::Blocks IDE 编译程序时一切正常。但是后来,出于学习目的,我决定编写一个 makefile,这在尝试启动它时会导致很多错误:

>     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

这是我的 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

谁能告诉我我做错了什么?我在网上看到很多类似的话题,但是他们的回答似乎对我没有帮助。

提前致谢!

您需要将 LIBS 传递给链接器而不是编译器,这将是您的 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