使用g++编译FLTK

11

我正在学习使用C++的Stroustrup的《Programming Principles and Practices》。我试图编译以下程序。

#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操作系统。请问如何编译这段代码?

2个回答

18

你必须将其与库链接:

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

对于您的代码,这个库已经足够了,但是取决于您使用的类,您可能需要添加:-lfltk_forms -lfltk_gl -lfltk_images
您也可以像这里提到的那样使用fltk-config
g++ -std=c++11 `fltk-config --cxxflags` trial.cpp  `fltk-config --ldflags` -o trial

注意:在您的代码文件(cpp和includes)之后具有链接参数(-l)非常重要,否则会出现编译错误。

救命提示! - eric

0

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

$ fltk-config --compile your_file >> makefile

现在您拥有了所有标志。您可以使用--cxxflags,但这种方式更好。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接