gpt4 book ai didi

c++ - 如何在 Rust 中优雅地 #include "gtk-layer-shell.h"

转载 作者:行者123 更新时间:2023-12-03 06:55:00 24 4
gpt4 key购买 nike

我想在我的 Rust 代码中包含一些 C++ 代码,并且我有以下包含语句:

#include "gtk-layer-shell.h"

我在 build.rs 中指定了文件的位置,但由于所有的依赖关系,它有点困惑。我的 build.rs 看起来像这样:

extern crate cpp_build;

fn main() {
let include_layer_shell = "/usr/include/gtk-layer-shell";
let include_gtk = "/usr/include/gtk-3.0";
let include_glib = "/usr/include/glib-2.0";
let include_glib_config = "/usr/lib/glib-2.0/include";
let include_pango = "/usr/include/pango-1.0";
let include_harfbuzz = "/usr/include/harfbuzz";
let include_cairo = "/usr/include/cairo";
let include_gdk_pixbuf = "/usr/include/gdk-pixbuf-2.0";
let include_atk = "/usr/include/atk-1.0";
cpp_build::Config::new()
.include(include_layer_shell)
.include(include_gtk)
.include(include_glib)
.include(include_glib_config)
.include(include_pango)
.include(include_harfbuzz)
.include(include_cairo)
.include(include_gdk_pixbuf)
.include(include_atk)
.build("src/main.rs");
}

有没有一种方法可以更优雅地包含它并且只指定 gtk-layer-shell.h 的位置并自动找到所有其他位置?我还希望能够交叉编译 aarch64 的应用程序。我现在设置它的方式我想我必须根据拱门更改路径,这看起来也有点困惑。

感谢您的帮助! :)

附言:我需要 C++ 代码的原因基本上只是因为我不知道如何在 Rust 中编写 gtk_layer_init_for_window (gtk_window);,所以如果有人知道如何完全避免 C++ 代码,那会更好!

最佳答案

感谢您的建议,我现在使用 gir 来生成绑定(bind)和安全的 Rust 包装器。它要容易得多,但只适用于 GTK 相关的东西,并且需要一个 .gir 文件。可以在此处找到教程:https://github.com/gtk-rs/gir

在此之前,我使用了 bindgen 和 pkg_config 包并将我的 build.rs 更改为以下内容:

extern crate bindgen;
extern crate pkg_config;

use std::env;
use std::path::PathBuf;

fn main() {
let library = pkg_config::Config::new()
.probe("gtk-layer-shell-0")
.expect("No gtk-layer-shell found on your system");
let include_paths = library
.include_paths
.into_iter()
.map(|p| p.into_os_string().into_string().unwrap())
.collect::<Vec<_>>();

for path in library.link_paths {
println!("cargo:rustc-link-path={}", path.to_str().unwrap());
}
for lib in library.libs {
println!("cargo:rustc-link-lib={}", lib);
}

// Tell cargo to invalidate the built crate whenever the wrapper changes
println!("cargo:rerun-if-changed=wrapper.h");

// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
// the resulting bindings.
let mut bindings = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header("wrapper.h")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks));

for path in include_paths {
bindings = bindings.clang_args(&["-F", &path]);
}

let bindings = bindings
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");

// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}

关于c++ - 如何在 Rust 中优雅地 #include "gtk-layer-shell.h",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63498840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com