Skip to Content

FLTK Help: MacOS and Windows

FLTK is a C++ cross-platform GUI framework that is decently object-oriented and is very nice for introducing GUI programming in a C++ OOP way (e.g., CS 271!).

Although it is cross-platform, is is not always straightforward to install on some platforms. Below are some things we have learned.

MacOS

Assuming that you already have g++ installed on your Mac, we got FLTK working by doing:

  1. Use Homebrew to install FLTK: Brew/FLTK

  2. Then you have to find where it installed FLTK. You can use the Finder to do this. It is likely something like /opt/brew/Cellar/fltk/version

  3. Using compilation flags directly or in these Makefile variables:

CXXFLAGS = -Wall -std=c++11 -I/opt/brew/Cellar/fltk/version/include
LDLIBS = -L/opt/brew/Cellar/fltk/version/lib -lfltk -lstdc++

The -I on the first line tells the compiler where to find the header files, and the -L on the second line tells the linker where to find the libraries. The version is some version string, and your entire path may be different than the above is an example, so do not cut and paste it and try it as it is. On the -I option do not add in the final “FL” folder name, because that is used in the code itself.

Windows

The best option for Gnu C++ (g++) on Windows is MinGW, and it has FLTK either built-in or as an option. I think most students used the MSYS2 pre-built package. FTLK is listed in the MSYS2 “base packages” list, but I do not know if this means it is included by default or just that it is available.

I think that no special compile or link flags are needed, as MinGW will install FLTK in the “system” area of its libraries, so they will be found by default.

Oddly enough, we found that the Windows MinGW version of g++ does not have the srandom and random functions in it. You can replace them with “srand” and “rand”.

Native File Chooser

FTLK has a built-in simple file chooser, which can be used like

    char* filename = fl_file_chooser("Open File?", "*.csv", 0);

but it is ugly. A native file chooser window can be programmed like:

    const char* filename;
    Fl_Native_File_Chooser fnfc;
    fnfc.title("Select a data file");
    fnfc.type(Fl_Native_File_Chooser::BROWSE_FILE);
    fnfc.filter("CSV\t*.csv");
    fnfc.directory("."); // default directory to use
    // Show native chooser
    switch (fnfc.show()) {
    case -1:
        //std::cerr << "ERROR: " << fnfc.errmsg() << "\n";
        return;
    case 1:
        //std::cerr << "CANCEL:\n";
        return;
    default: // File is chosen
        //std::cerr << "PICKED: " << fnfc.filename() << "\n";
        break;
    }
    filename = fnfc.filename();

This opens up a native file chooser dialog box on whatever platform you are on, and should work on MacOS and Windows.

Other Notes: MacOS FLTK timing and color pallette

We had some issues getting a game (battlebots) working on MacOS. Finally this is what worked:

  1. Add fl_color(FL_FOREGROUND_COLOR); as a first statement in the MainWindow::MainWindow() constructor (about line 21 in GUI.cpp)

  2. Add the statement std::cout « “repeating timer\n”; just before the call to FL::repeat_timeout(), and after the game->update() line (about lines 100-101 in GUI.cpp)

Rebuild with a “make clean” and then a “make”, and then run “./game”. It can still crash, but it seems to mostly work! If it crashes, just try again. It seems that if it is able to draw the arena with all the obstacles and prizes, then it will run the game.

I was googling about FLTK on MacOS and saw two things, one saying the color palette needs set before any drawing, and that is why I added #1 above in the code, and then I saw another saying that the “repeat_timeout” function was crashing (thus #2). Printing something out before calling it should not affect anything, but internally the FLTK library appears to be using threads, and so printing can affect the timing of multiple threads enough to where something that was going wrong before doesn’t anymore (or mostly doesn’t anymore). It’s the best I can do without consulting a good MacOS developer, which I am not!

Other Notes: GDB on MacOS

One student said: “I was able to install gdb for the lab by following the guide below, it may be helpful to other students.”

Jason Elwood GDB on Mac

Note that MacOS does have lldb which is a good debugger, it is just a bit different.