Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile FLTK with g++

Tags:

c++

fltk

I am working through Stroustrup's Principles and Practices using C++. I am trying to get the following program to compile.

#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();
}

I tried compiling it with g++ -std=c++11 trial.cpp -o trial but then it threw the following error

    /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

I installed FLTK version 1.3 from terminal. I am running Linux mint 17 on my computer. How do I compile this code?

like image 365
Effective_cellist Avatar asked Oct 14 '15 17:10

Effective_cellist


People also ask

How do I compile a Fltk file?

In most cases you can just type "make". This will run configure with the default of no options and then compile everything. The CC environment variable can also be used to override the default C compiler (cc or gcc), which is used for a few FLTK source files.

How do I use Fltk?

In Visual C++ you will need to tell the compiler where to find the FLTK header files. This can be done by selecting "Settings" from the "Project" menu and then changing the "Preprocessor" settings under the "C/C++" tab. You will also need to add the FLTK ( FLTK. LIB or FLTKD.


1 Answers

You have to link it with the libraries:

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

For your code this library is enough, but depending on what classes you use you might need to add: -lfltk_forms -lfltk_gl -lfltk_images also.

You can also use fltk-config as mentioned here:

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

Note: it is important to have the linking parameters (-l) after your code files (cpp and includes), otherwise you get compile errors.

like image 64
agold Avatar answered Sep 28 '22 06:09

agold