gtk-rs / gtk4-rs

Rust bindings of GTK 4
https://gtk-rs.org/gtk4-rs/
MIT License
1.9k stars 174 forks source link

[HELP] Can't get Hello World to work on macOS Monterey 12.51 #1395

Closed 7stud closed 1 year ago

7stud commented 1 year ago

I'm following the Hello World tutorial here:

https://gtk-rs.org/gtk4-rs/stable/latest/book/hello_world.html

But no window is displayed when I do cargo run--instead I get these warnings:

~/rust_programs/gtk-app1% cargo run   
warning: unused import: `glib`
 --> src/main.rs:2:11
  |
2 | use gtk::{glib, Application, ApplicationWindow};
  |           ^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: `gtk-app1` (bin "gtk-app1") generated 1 warning (run `cargo fix --bin "gtk-app1"` to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 0.13s
     Running `target/debug/gtk-app1`

(gtk-app1:26426): Gdk-WARNING **: 15:24:37.097: No such backend: *

(gtk-app1:26426): Gtk-WARNING **: 15:24:37.097: Failed to open display
~/rust_programs/gtk-app1% 

Here's my main.rs:

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

const APP_ID: &str = "org.gtk_rs.HelloWorld";

fn main() {
    //Create new application:
    let app = Application::builder().application_id(APP_ID).build();

    //Connect to "activate" signal of "app":
    app.connect_activate(build_ui);
    //Run application:
    app.run();
}

fn build_ui(app: &Application) {
    //Create a window and set title:
    let window = ApplicationWindow::builder()
        .application(app)
        .title("My GTK App")
        .build();

    //Present window:
    window.present();
}

I installed gtk4 with MacPorts (instead of Homebrew ):

% sudo port install gtk4
Password:
Warning: port definitions are more than two weeks old, consider updating them by running 'port selfupdate'.
--->  Computing dependencies for gtk4
The following dependencies will be installed: 
 Xft2
 cairo
 db48
 fontconfig
 freetype
 fribidi
 gdbm
 gdk-pixbuf2
 glib2
 gobject-introspection
 graphene
 graphite2
 harfbuzz
 hicolor-icon-theme
 libepoxy
 libpixman
 libxkbcommon
 libxkbcommon-x11
 mesa
 ossp-uuid
 pango
 pcre
 perl5.34
 py310-mako
 py310-markdown
 py310-markupsafe
 py310-setuptools
 readline
 shared-mime-info
 xkbcomp
 xkeyboard-config
 xorg-libX11
 xorg-libXau
 xorg-libXcursor
 xorg-libXdamage
 xorg-libXdmcp
 xorg-libXext
 xorg-libXfixes
 xorg-libXi
 xorg-libXinerama
 xorg-libXrandr
 xorg-libpthread-stubs
 xorg-libxcb
 xorg-libxkbfile
 xorg-xcb-proto
 xorg-xcb-util
 xorg-xorgproto
 xrender
Continue? [Y/n]: Y

...
...

--->  Attempting to fetch gtk4-4.8.2_0+x11.darwin_21.arm64.tbz2.rmd160 from https://packages.macports.org/gtk4
--->  Installing gtk4 @4.8.2_0+x11
--->  Activating gtk4 @4.8.2_0+x11
--->  Cleaning gtk4
--->  Updating database of binaries
--->  Scanning binaries for linking errors
--->  No broken files found.
--->  No broken ports found.

I checked the version:

~/rust_programs% pkg-config --modversion gtk4
4.8.2

Then I created a new app:

~/rust_programs% cargo new gtk-app1  
     Created binary (application) `gtk-app1` package

~/rust_programs% cd gtk-app1 

Then:

~/rust_programs/gtk-app1% cargo add gtk4 --rename gtk --features v4_8  
    Updating crates.io index
      Adding gtk4 v0.6.6 to dependencies.
             Features:
             + v4_2
             + v4_4
             + v4_6
             + v4_8
             - blueprint
             - dox
             - gnome_42
             - gnome_43
             - gnome_44
             - unsafe-assume-initialized
             - v4_10
             - xml_validation

My Cargo.toml:

[package]
name = "gtk-app1"
version = "0.1.0"
edition = "2021"

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

[dependencies]
gtk = { version = "0.6.6", package = "gtk4", features = ["v4_8"] }

cargo run worked fine before I added any of the gtk code:

~/rust_programs/gtk-app1% cargo run
  Downloaded proc-macro-error v1.0.4
  Downloaded memoffset v0.8.0
  Downloaded pango v0.17.4
  Downloaded serde v1.0.163
  Downloaded version-compare v0.1.1
  Downloaded cairo-sys-rs v0.17.0
  Downloaded field-offset v0.3.5
  Downloaded gsk4-sys v0.6.3
  Downloaded proc-macro-error-attr v1.0.4
  Downloaded proc-macro-crate v1.3.1
  Downloaded serde_spanned v0.6.1
  Downloaded toml_datetime v0.6.1
  Downloaded graphene-sys v0.17.0
  Downloaded gdk-pixbuf-sys v0.17.0
  Downloaded target-lexicon v0.12.7
  Downloaded gdk-pixbuf v0.17.0
  Downloaded graphene-rs v0.17.1
  Downloaded gobject-sys v0.17.4
  Downloaded pkg-config v0.3.27
  Downloaded gtk4-macros v0.6.6
  Downloaded quote v1.0.27
  Downloaded heck v0.4.1
  Downloaded rustc_version v0.4.0
  Downloaded semver v1.0.17
  Downloaded anyhow v1.0.71
  Downloaded gsk4 v0.6.3
  Downloaded pango-sys v0.17.0
  Downloaded cfg-expr v0.15.1
  Downloaded toml v0.7.3
  Downloaded system-deps v6.1.0
  Downloaded cairo-rs v0.17.0
  Downloaded gdk4-sys v0.6.3
  Downloaded glib-macros v0.17.9
  Downloaded glib-sys v0.17.4
  Downloaded gio-sys v0.17.4
  Downloaded gdk4 v0.6.3
  Downloaded gtk4-sys v0.6.3
  Downloaded toml_edit v0.19.8
  Downloaded winnow v0.4.6
  Downloaded gio v0.17.9
  Downloaded glib v0.17.9
  Downloaded gtk4 v0.6.6
  Downloaded libc v0.2.144
  Downloaded 43 crates (3.0 MB) in 1.93s
   Compiling autocfg v1.1.0
   Compiling serde v1.0.163
   Compiling hashbrown v0.12.3
   Compiling winnow v0.4.6
   Compiling target-lexicon v0.12.7
   Compiling heck v0.4.1
   Compiling smallvec v1.10.0
   Compiling version-compare v0.1.1
   Compiling pkg-config v0.3.27
   Compiling proc-macro2 v1.0.56
   Compiling quote v1.0.27
   Compiling indexmap v1.9.3
   Compiling unicode-ident v1.0.8
   Compiling libc v0.2.144
   Compiling version_check v0.9.4
   Compiling proc-macro-error-attr v1.0.4
   Compiling cfg-expr v0.15.1
   Compiling syn v1.0.109
   Compiling futures-core v0.3.28
   Compiling proc-macro-error v1.0.4
   Compiling slab v0.4.8
   Compiling anyhow v1.0.71
   Compiling futures-task v0.3.28
   Compiling futures-util v0.3.28
   Compiling syn v2.0.15
   Compiling once_cell v1.17.1
   Compiling pin-utils v0.1.0
   Compiling futures-channel v0.3.28
   Compiling pin-project-lite v0.2.9
   Compiling thiserror v1.0.40
   Compiling memchr v2.5.0
   Compiling bitflags v1.3.2
   Compiling gio v0.17.9
   Compiling semver v1.0.17
   Compiling futures-io v0.3.28
   Compiling memoffset v0.8.0
   Compiling toml_datetime v0.6.1
   Compiling serde_spanned v0.6.1
   Compiling toml_edit v0.19.8
   Compiling rustc_version v0.4.0
   Compiling field-offset v0.3.5
   Compiling futures-macro v0.3.28
   Compiling thiserror-impl v1.0.40
   Compiling toml v0.7.3
   Compiling proc-macro-crate v1.3.1
   Compiling system-deps v6.1.0
   Compiling glib-macros v0.17.9
   Compiling gtk4-macros v0.6.6
   Compiling glib-sys v0.17.4
   Compiling gobject-sys v0.17.4
   Compiling gio-sys v0.17.4
   Compiling pango-sys v0.17.0
   Compiling cairo-sys-rs v0.17.0
   Compiling gdk-pixbuf-sys v0.17.0
   Compiling gdk4-sys v0.6.3
   Compiling graphene-sys v0.17.0
   Compiling gsk4-sys v0.6.3
   Compiling gtk4-sys v0.6.3
   Compiling futures-executor v0.3.28
   Compiling glib v0.17.9
   Compiling cairo-rs v0.17.0
   Compiling graphene-rs v0.17.1
   Compiling gdk-pixbuf v0.17.0
   Compiling pango v0.17.4
   Compiling gdk4 v0.6.3
   Compiling gsk4 v0.6.3
   Compiling gtk4 v0.6.6
   Compiling gtk-app1 v0.1.0 (/Users/7stud/rust_programs/gtk-app1)
    Finished dev [unoptimized + debuginfo] target(s) in 34.56s
     Running `target/debug/gtk-app1`
Hello, world!
sdroege commented 1 year ago

If you run gtk4-demo it will probably fail the same way? This looks like a setup problem on your side. You seemed to have compiled GTK4 with the X11 backend but don't run an X11 server on your machine.

If you're using macOS it would be better to use the macOS backend.

7stud commented 1 year ago

If you run gtk4-demo it will probably fail the same way? This looks like a setup problem on your side. You seemed to have compiled GTK4 with the X11 backend but don't run an X11 server on your machine.

If you're using macOS it would be better to use the macOS backend.

I followed the install instructions, except I installed GTK with MacPorts instead of Homebrew. I guess I'll try QT instead.

sdroege commented 1 year ago

Why did you use MacPorts instead of Homebrew though? Apparently MacPorts is using the X11 backend of GTK, while Homebrew would use the proper macOS backend. There's a reason why the install instructions mention Homebrew :)