1

I have a basic gtk-rs project with which I use meson. I have included gtk4 crate in the Cargo.toml file. When I run with cargo, it compiles but outputs "segmentation fault" error (I have searched and I still don't know what that is). However, when I run with meson, it refuses to build at all.

builddir/
 main.p/
 build.ninja
src/
 main.rs
target/
 ...
Cargo.toml
Cargo.lock
meson.build

Cargo.toml

[package]
name = "my-gtk-app"
version = "0.1.0"
edition = "2018"
[dependencies]
gtk = { version = "0.2", package = "gtk4" }

meson.build

project(
  'my-gtk-app',
  'rust',
  version : '0.1',
)
cargo_sources = files(
  'Cargo.toml',
  'Cargo.lock',
)
gtk = dependency('gtk4', version: '>= 4.0.0')
executable('main', 'src/main.rs', dependencies : gtk)

main.rs

use gtk::prelude::*;
use gtk::{Application, ApplicationWindow};

fn main() {
    let app = Application::builder()
        .application_id("org.example.HelloWorld")
        .build();

    app.connect_activate(|app| {
        // We create the main window.
        let window = ApplicationWindow::builder()
            .application(app)
            .default_width(320)
            .default_height(200)
            .title("Hello, World!")
            .build();

        // Show the window.
        window.show();
    });

    app.run();
}

When I run the program using cargo run, I get

Finished dev [unoptimized + debuginfo] target(s) in 0.45s
Running `target/debug/my-gtk-app`
1] 73039 segmentation fault  cargo run

However, when I run with with meson compile -C builddir, I get

ninja: Entering directory `builddir'
[1/1] Compiling Rust source ../src/main.rs
FAILED: main 
rustc -C linker=cc --color=always --crate-type bin --crate-name main -g --emit dep-info=main.d --emit link -o main -L/usr/local/Cellar/gtk4/4.4.0/lib -L/usr/local/Cellar/pango/1.48.10/lib -L/usr/local/Cellar/harfbuzz/3.0.0/lib -L/usr/local/Cellar/gdk-pixbuf/2.42.6/lib -L/usr/local/Cellar/cairo/1.16.0_5/lib -L/usr/local/Cellar/graphene/1.10.6/lib -L/usr/local/Cellar/glib/2.70.0_1/lib -L/usr/local/opt/gettext/lib -l dylib=gtk-4 -l dylib=pangocairo-1.0 -l dylib=pango-1.0 -l dylib=harfbuzz -l dylib=gdk_pixbuf-2.0 -l dylib=cairo-gobject -l dylib=cairo -l dylib=graphene-1.0 -l dylib=gio-2.0 -l dylib=gobject-2.0 -l dylib=glib-2.0 -l dylib=intl ../src/main.rs
error[E0433]: failed to resolve: maybe a missing crate `gtk`?
 --> ../src/main.rs:1:5
  |
1 | use gtk::prelude::*;
  |     ^^^ maybe a missing crate `gtk`?

error[E0432]: unresolved import `gtk`
 --> ../src/main.rs:2:5
  |
2 | use gtk::{Application, ApplicationWindow};
  |     ^^^ maybe a missing crate `gtk`?

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0432, E0433.
For more information about an error, try `rustc --explain E0432`.
ninja: build stopped: subcommand failed.

I am on MacOS if that helps.

  • 1
    It looks like `meson` is calling `rustc` (not `cargo`) and ignoring your `Cargo.toml` (or at least its dependency information). I don't know how `meson` works so I can't say how to fix it, though. – Kevin Reid Oct 17 '21 at 01:42
  • I am open to use any other alternative. it is just the official book proved by gtk-rs (https://gtk-rs.org/gtk4-rs/stable/latest/book/hello_world) recommends it. Do you have others in mind? – Spirit_bird Oct 17 '21 at 06:52
  • Your setup is very different to the [`gtk-rust-template`](https://gitlab.gnome.org/bilelmoussaoui/gtk-rust-template/-/tree/master/) project to which that book directs you? In particular, I'm referring to your root `meson.build` file, the scripts in the `build-aux` folder on which it depends and the `meson.build` files in the subdirectories that it pulls in (data, po and src): `src/meson.build` calls out to cargo as a `custom_target` that was defined in the root file. Is there some reason you haven't started from the template as advised by the book? – eggyal Oct 17 '21 at 09:01
  • Thank you for your comment. That was the first thing I did. However, I can only use the python script to initialize the project. I can't use flatpac because I am not on linux. – Spirit_bird Oct 17 '21 at 09:24
  • 1
    Meson doesn't use cargo by design (it would be like meson calling cmake, or ./configure). There was a branch at one point to handle crates in meson.build (I am the author of that series), but it never landed. Using Meson with rust right now really only works well for projects that don't use crates, since the primary consumers of that feature are C/C++ projects low in the stack dabbling in Rust that can't or wont use crates. – dcbaker Oct 20 '21 at 16:19
  • It sounds like it cannot find GTK4. Unlike other crates, the `gtk4` crate is only for GTK4 Rust language bindings, and not the actual GTK library. Also, while Meson can call rustc directly, I don't think it's fully-baked yet, and the recommended way is to use Cargo by means of script like the gtk-rust-template mentioned above does. – Brian Reading Feb 15 '22 at 07:39
  • Since you're not using Flatpak for building, you can call `cargo` with a shell script that models behavior like the one found [here](https://gitlab.gnome.org/World/Solanum/-/blob/main/build-aux/cargo.sh). You can find the associated `meson.build` [here](https://gitlab.gnome.org/World/Solanum/-/blob/main/src/meson.build). – Brian Reading Feb 15 '22 at 07:48

0 Answers0