gtk-rs / examples

DEPRECATED, use https://github.com/gtk-rs/gtk-rs repository instead!
MIT License
283 stars 76 forks source link

Cannot build basic.rs #327

Closed lattice0 closed 3 years ago

lattice0 commented 3 years ago

I'm trying to build basic.rs: https://github.com/gtk-rs/examples/blob/e7cf9f56e086864ff6f9270216b2ba0416e24648/src/bin/basic.rs

and I'm getting:

error[E0599]: no method named `connect_activate` found for struct `gtk::Application` in the current scope
   --> src/main.rs:26:17
    |
26  |     application.connect_activate(|app| {
    |                 ^^^^^^^^^^^^^^^^ method not found in `gtk::Application`

error[E0599]: no method named `run` found for struct `gtk::Application` in the current scope
   --> src/main.rs:30:17
    |
30  |     application.run(&args().collect::<Vec<_>>());
    |                 ^^^ method not found in `gtk::Application`
    | 

I took off the 2 extern crate because I've read it's old. I don't know what gio is, so I only linked gtk on Cargo:


[dependencies]
gtk = { git = "https://github.com/gtk-rs/gtk.git" }

What is wrong?

sagara- commented 3 years ago

For anyone who runs into this issue, you need to add an explicit dependency on gio to your Cargo.toml, and import gio::prelude::*. The extern crate statements are no longer required in this case when using Rust 2018 edition.

gtk already depends on gio, however it doesn't re-export the crate so it's unavailable unless you explicitly add a dependency in Cargo.toml. The connect_activate() used in the examples is defined in gio::ApplicationExt, and similarly run() is in gio::ApplicationExtManual, which is why this is needed. As noted in https://gtk-rs.org/docs-src/tutorial/gnome_and_rust you need to explicitly import the traits containing various methods you wish to use, which is why this is needed.

It might be a good idea to re-export dependent crates (like gio, gdk, atk, etc) from gtk to avoid having to explicitly specify these libraries in Cargo.toml. rust-qt apparently does this: "Each crate re-exports its dependencies, so, for example, you can access qt_core as qt_widgets::qt_core without adding an explicit dependency." (https://rust-qt.github.io/qt/getting_started/). This would then allow importing gtk::gio::prelude::* in this example without needing the explicit gio dependency in Cargo.toml. In general unless you're writing library code for one of these layers directly I don't think importing them via gtk would really be an issue.

sdroege commented 3 years ago

Yeah that would happen if @lucaszanella didn't just get rid of the two extern crate but also the use gio::prelude::*. That one is required for this to work.

Thanks @sagara- !