用 g++ 编译 FLTK

Compile FLTK with g++

我正在使用 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。如何编译此代码?

你必须 link 它与图书馆:

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 和 includes)之后添加 linking 参数 (-l) 很重要,否则会出现编译错误。

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

$ fltk-config --compile your_file >> makefile

现在你已经拥有了所有的旗帜。您可以使用 --cxxflags,但这种方法很管用。