2015-10-14 138 views
3

我正在使用Stroustrup的使用C++的原則和實踐。我正試圖獲得下面的程序來編譯。使用g ++編譯FLTK

#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。我如何編譯這段代碼?

回答

6

你必須將其與庫鏈接:

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 

注:對你的代碼文件後的鏈接參數(-l)是很重要的(CPP包括),否則你會得到編譯錯誤。

+0

這工作,謝謝你的鏈接了。 – dpk