【问题标题】:Compile FLTK with g++用 g++ 编译 FLTK
【发布时间】:2016-01-12 22:04:42
【问题描述】:

我正在使用 C++ 阅读 Stroustrup 的原则和实践。我正在尝试编译以下程序。

#include <FL/Fl.H>
#include <FL/Fl_Box.H>
#include <Fl/Fl_Window.H>

int main()
{
    Fl_Window window(200, 200, "Window title");
    Fl_Box box(0,0,200,200,"Hey, I mean, Hello, World!");
    window.show();
    return Fl::run();
}

我尝试使用g++ -std=c++11 trial.cpp -o trial 编译它,但随后它抛出了以下错误

    /tmp/ccaLRS7L.o: In function `main':
trial.cpp:(.text+0x26): undefined reference to `Fl_Window::Fl_Window(int, int, char const*)'
trial.cpp:(.text+0x50): undefined reference to `Fl_Box::Fl_Box(int, int, int, int, char const*)'
trial.cpp:(.text+0x5f): undefined reference to `Fl_Window::show()'
trial.cpp:(.text+0x64): undefined reference to `Fl::run()'
trial.cpp:(.text+0x84): undefined reference to `Fl_Window::~Fl_Window()'
trial.cpp:(.text+0xae): undefined reference to `Fl_Window::~Fl_Window()'
/tmp/ccaLRS7L.o: In function `Fl_Box::~Fl_Box()':
trial.cpp:(.text._ZN6Fl_BoxD2Ev[_ZN6Fl_BoxD5Ev]+0x13): undefined reference to `vtable for Fl_Box'
trial.cpp:(.text._ZN6Fl_BoxD2Ev[_ZN6Fl_BoxD5Ev]+0x1f): undefined reference to `Fl_Widget::~Fl_Widget()'
collect2: error: ld returned 1 exit status

我从终端安装了 FLTK 1.3 版。我在我的电脑上运行 Linux mint 17。如何编译这段代码?

【问题讨论】:

    标签: c++ fltk


    【解决方案1】:

    您必须将其与库链接:

    g++ -std=c++11 trial.cpp -lfltk -o trial
    

    对于您的代码,这个库就足够了,但根据您使用的类,您可能还需要添加:-lfltk_forms -lfltk_gl -lfltk_images。

    您也可以使用fltk-config 提到的here:

    g++ -std=c++11 `fltk-config --cxxflags` trial.cpp  `fltk-config --ldflags` -o trial
    

    注意:在代码文件(cpp 和包含)之后添加链接参数 (-l) 很重要,否则会出现编译错误。

    【讨论】:

    • 救生小贴士!
    【解决方案2】:

    fltk-config 是一个不错的选择,但如果你想使用更多标志或更改它们,试试这个。

    $ fltk-config --compile your_file >> makefile

    现在你拥有了所有的标志。您可以使用 --cxxflags 但这种方式很有效。

    【讨论】:

      猜你喜欢
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      • 2014-03-08
      • 2018-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多