Using the fltk.rs crate

My project is going to need a GUI interface, so I've been searching for a crate to help me with that. I found the fltk crate that looks like it might do the trick. On Github they even have examples that may help kickstart my efforts. However, I can't seem to get the crate to work for me. I used cargo new to create a new project, copied the first example from the first page of the fltk book, modified my cargo.toml file with fltk = "^1.4", and then tried to compile. All kinds of errors and after a lot of experimentation, I find myself confused as to why it won't work. So, help??

Here's the code for my main.rs:

use fltk::{prelude::*, window::Window};

fn main() {
    let wind = window::Window::new(100, 100, 400, 300, "My Window");
    wind.end();
    wind.show();
}

This is just copied from the fltk book so there shouldn't be any typos. The cargo.toml file looks like this:

[package]
name = "deleteme"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
fltk = "^1.4"

Here's what the compiler says:

error[E0433]: failed to resolve: use of undeclared crate or module `window`
 --> src/main.rs:4:24
  |
4 |     let wind = window::Window::new(100, 100, 400, 300, "My Window");
  |                        ^^^^^^ not found in `window`
  |
help: consider importing this type alias
  |
1 | use fltk::window::Window;
  |
help: if you import `Window`, refer to it directly
  |
4 -     let wind = window::Window::new(100, 100, 400, 300, "My Window");
4 +     let wind = Window::new(100, 100, 400, 300, "My Window");

So, any suggestions as to how to get this working?
Thanks.

So, you're importing window::Window, so you can use it via Window. But you're trying to use it via window::Window, which only works if you import window.

Weird to write down (it's not specific to fltk-rs, too). So either make your first line

use fltk::{prelude::*, window};

or make line 4

    let wind = Window::new(100, 100, 400, 300, "My Window");

Note that the book you cite imports fltk::* which includes fltk::window.

Hi,
as KillTheMule said, you should make some changes.
here is a working main file :

use fltk::{app, prelude::*, window::Window};

fn main() {
    let app = app::App::default();
    let mut wind = Window::new(100, 100, 400, 300, "My Window");
    wind.end();
    wind.show();
    app.run().unwrap();
}

didn't work recently with fltk so can't say that it's all perfect, but it's working.
without the 'app', the window open and close immediately...

@KillTheMule Ok, so I did that and then had to make the wind variable mutable, but even then I get errors and can't compile. Here's the new error:

error: linking with `cc` failed: exit status: 1

followed by a note that is about a hundred lines long and a second note copied below:

  = note: /usr/bin/ld: cannot find -lXft
          /usr/bin/ld: cannot find -lfontconfig
          /usr/bin/ld: cannot find -lpango-1.0
          /usr/bin/ld: cannot find -lpangoxft-1.0
          /usr/bin/ld: cannot find -lcairo
          /usr/bin/ld: cannot find -lpangocairo-1.0
          collect2: error: ld returned 1 exit status

I guess I shouldn't trust this fltk Book. Anyway, any thoughts? I've been hoping that this fltk crate will meet my needs for working with GUI, but if their documentation is inaccurate, I wonder if I should be looking elsewhere.

Only that I found fltk very pleasurable to work with, in particular that it "just worked" both on linux and windows without me putting any thoughts into building it :slight_smile:

Guess you should use

[dependencies]
fltk = { version = "^1.4", features = ["fltk-bundled"] }

well fltk-rs could be trusted. eventually you could go on fltk-rs github discussion pages. MoAlyoussef is always helping.
but right now you should do as KillTheMule said and try to use the 'bundled' feature or you should go read the setup page of the fltk-rs book and be sure to install all the dependencies.

I only used fltk-rs on Windows, but it's working well and it's the simplest GUI I could find for Rust.

Consider opening an issue/PR; I'm sure they would appreciate it!

I took your advice and went to setup. It said that Linux: X11 and OpenGL development headers need to be installed for development. So, I copied and ran this from the command line:

sudo apt-get install libx11-dev libxext-dev libxft-dev libxinerama-dev libxcursor-dev libxrender-dev libxfixes-dev libpango1.0-dev libgl1-mesa-dev libglu1-mesa-dev

To be honest, I really have no idea what that did for me, but it seems to have worked. (Keep in mind that I likely still fit in the newbe category.) The fltk examples are working fine now and the editor example I pulled off of Github looks like it could be a good base for me to start with.

I think they need to revise the examples they put in the Fltk Book's intro, since they don't work until you install those libraries. All they accomplish is a lot of confusion.

Thanks for your help! :>)

1 Like

ok, good..

FLTK is a C and C++ library. In order for it to compile, it needs to have access to the operating system's primitive graphical interfaces, which on Linux is the de facto standard X11. Installing the "dev" packages installs the corresponding header files.

That makes sense. Thanks for the explanation. :>)