#widgets #graphics

fltk-build

Create native fltk/cfltk modules to be used from Rust

2 releases

0.1.1 Dec 3, 2021
0.1.0 Nov 21, 2021

#129 in #widgets

43 downloads per month
Used in 2 crates (via fltk-flow)

MIT license

10KB
100 lines

fltk-build

Allows creating native C/C++ fltk/cfltk modules to be used from Rust. This is done by exposing the include paths and lib paths from the built fltk-sys crate on your system.

Usage

[dependencies] # or [dev-dependencies]
fltk = "1.2.17" # this won't work with fltk-bundled
# You might need fltk-sys and paste if you use fltk's trait macros

[build-dependencies]
fltk-build = "0.1"
fltk-sys = "1.2.17"
cc = "1" # or cmake = "0.1"

Example

build.rs (using cc):

use fltk_build::fltk_out_dir;

fn main() {
    let fltk_out_dir = fltk_out_dir().unwrap();

    cc::Build::new()
        .file("src/my_wid.cpp")
        .cpp(true)
        .flag_if_supported("-w")
        .flag_if_supported("-fno-rtti")
        .include(&fltk_out_dir.join("include"))
        .compile("my_wid");
}

build.rs (using cmake-rs):

use std::env;
use std::path::PathBuf;
use fltk_build::fltk_out_dir;

fn main() {
    let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
    let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
    let fltk_out_dir = fltk_out_dir().unwrap();

    cmake::Config::new("src")
    .define(
        "CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES",
        fltk_out_dir.join("include"),
    )
    .build();

    println!(
        "cargo:rustc-link-search=native={}",
        out_dir.display()
    );
    println!("cargo:rustc-link-lib=static=my_wid");

    match target_os.as_str() {
        "windows" => (), // doesn't need explicit linking to the C++ stdlib
        "macos" => println!("cargo:rustc-link-lib=c++"),
        _ => println!("cargo:rustc-link-lib=stdc++"),
    }
}

In your C/C++ files, you can directly access the FLTK and cfltk headers:

#include <cfl/cfl_window.h>
#include <FL/Fl_Window.H>

If you're using CMake, a minimal CMakeLists.txt example:

cmake_minimum_required(VERSION 3.0)
project(wid)

if (NOT MSVC)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -fno-exceptions")
endif()

add_library(my_wid my_wid.cpp)

install(TARGETS my_wid DESTINATION ${CMAKE_INSTALL_PREFIX})

Example crate (not published on crates.io) using fltk-build: https://github.com/fltk-rs/fltk-flow

No runtime deps

Features