【问题标题】:Getting lots of error messages like "undefined reference to..." using FLTK with g++使用带有 g++ 的 FLTK 获取大量错误消息,例如“未定义的对...的引用”
【发布时间】:2017-04-24 09:48:24
【问题描述】:

我是按照 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

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

提前非常感谢!

【问题讨论】:

  • 使用应该粘贴你的cpp文件而不是makefile
  • @slawekwin Cpp 文件很大,无法粘贴,所以我最好提供我从以下位置获取的 Stroustroup 网站的链接:link 这是 main.cpp 的样子:#include "Simple_window.h" #include "Graph.h" int main() { using namespace Graph_lib; Point t1{100,100}; Simple_window win{t1,600,400, "Canvas"}; Polygon poly; poly.add(Point{300,200}); poly.add(Point{350,100}); poly.add(Point{400,200}); poly.set_color(Color::red); win.attach(poly); win.wait_for_button(); }

标签: c++ c++11 makefile fltk


【解决方案1】:

您需要将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

【讨论】:

  • 天哪,终于成功了!非常感谢!我现在很高兴:)
猜你喜欢
  • 2017-01-13
  • 2013-09-29
  • 2012-06-22
  • 2014-07-24
  • 2010-12-25
  • 1970-01-01
  • 1970-01-01
  • 2019-02-14
  • 1970-01-01
相关资源
最近更新 更多