使用x11 crate时如何正确链接xfixes库?
如何在Rust中链接Xfixes库以使用XFixesHideCursor?
我尝试构建一个隐藏X11光标的Rust程序,相关代码配置如下:
Cargo.toml
[package] name = "xhidecursor" version = "0.1.0" edition = "2021" [dependencies] x11 = { version = "2.20.0", features = ["xlib"] }
src/main.rs
use x11::xfixes::XFixesHideCursor; use x11::xlib::XDefaultRootWindow; use x11::xlib::XOpenDisplay; use x11::xlib::XSync; fn main() { let display; let window; unsafe { display = XOpenDisplay(&0); window = XDefaultRootWindow(display); XFixesHideCursor(display, window); XSync(display, 1); } }
编译时出现链接错误,报错信息如下:
$ cargo run Compiling xhidecursor v0.1.0 (/home/neumann/xhidecursor) error: linking with `cc` failed: exit status: 1 | = note: "cc" "-m64" "/tmp/rustc90arZ7/symbols.o" "/home/neumann/xhidecursor/target/debug/deps/xhidecursor-72a51398118483b1.1mqky27w09vwj4k3.rcgu.o" "/home/neumann/xhidecursor/target/debug/deps/xhidecursor-72a51398118483b1.2hnupqzwx9pp7uzf.rcgu.o" "/home/neumann/xhidecursor/target/debug/deps/xhidecursor-72a51398118483b1.2ncee6h15dx790kk.rcgu.o" "/home/neumann/xhidecursor/target/debug/deps/xhidecursor-72a51398118483b1.3wcgezgg5zbsr34g.rcgu.o" "/home/neumann/xhidecursor/target/debug/deps/xhidecursor-72a51398118483b1.4icvi7j5fuzwog4h.rcgu.o" "/home/neumann/xhidecursor/target/debug/deps/xhidecursor-72a51398118483b1.4l8ef34cbk2a0uy7.rcgu.o" "/home/neumann/xhidecursor/target/debug/deps/xhidecursor-72a51398118483b1.ropukbb1bsg3lid.rcgu.o" "/home/neumann/xhidecursor/target/debug/deps/xhidecursor-72a51398118483b1.37qtt1vkgdsjm1zb.rcgu.o" "-Wl,--as-needed" "-L" "/home/neumann/xhidecursor/target/debug/deps" "-L" "/usr/lib" "-L" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/neumann/xhidecursor/target/debug/deps/libx11-f4fa73bc30743fb8.rlib" "/home/neumann/xhidecursor/target/debug/deps/liblibc-f25e23d2977f32be.rlib" "-Wl,--start-group" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-5670385a2fe8b60b.rlib" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-fd56ba6dbf7aaecc.rlib" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-b8acf8e5c2e85baa.rlib" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-1706edefffdca0a2.rlib" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-f483302e0b13708e.rlib" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-725b0718fc18e1ed.rlib" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-de685fcf2157e6fb.rlib" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-f613ac3eda05b9ff.rlib" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-3de98a7d049af6a1.rlib" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-c12183655bdce152.rlib" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-0fc7beea925de7e3.rlib" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-8c34825485bf59dc.rlib" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-ba5f7e926e729d81.rlib" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-643de0950163a839.rlib" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-539ea2f72ef89687.rlib" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-871432094bb4c885.rlib" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-ef1a8ee61f2e39bf.rlib" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-f1646747442c1c7b.rlib" "-Wl,--end-group" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-ff283b4bf550fa1c.rlib" "-Wl,-Bdynamic" "-lX11" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/home/neumann/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/neumann/xhidecursor/target/debug/deps/xhidecursor-72a51398118483b1" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-nodefaultlibs" = note: /usr/bin/ld: /home/neumann/xhidecursor/target/debug/deps/xhidecursor-72a51398118483b1.ropukbb1bsg3lid.rcgu.o: in function `xhidecursor::main': /home/neumann/xhidecursor/src/main.rs:13: undefined reference to `XFixesHideCursor' collect2: error: ld returned 1 exit status = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified = note: use the `-l` flag to specify native libraries to link = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname) error: could not compile `xhidecursor` due to previous error
我之前通过启用x11 crate的xlib特性解决过类似链接问题,但x11 crate没有xfixes相关特性,请问正确链接xfixes的方法是什么?
解决方法
方式一:直接在Cargo.toml中指定链接库
在Cargo.toml中添加rustc-link-lib配置,告诉Cargo链接Xfixes系统库:
[package] name = "xhidecursor" version = "0.1.0" edition = "2021" [dependencies] x11 = { version = "2.20.0", features = ["xlib"] } # 手动指定链接Xfixes库 rustc-link-lib = ["Xfixes"]
方式二:使用build.rs脚本
在项目根目录创建build.rs文件,内容如下:
fn main() { // 向Cargo传递链接Xfixes库的指令 println!("cargo:rustc-link-lib=Xfixes"); }
额外注意事项
确保系统已安装Xfixes的开发包,以Debian/Ubuntu为例:
sudo apt install libxfixes-dev
两种方式都会让Rust编译器在链接阶段自动加入-lXfixes参数,解决XFixesHideCursor的未定义引用问题。
内容的提问来源于stack exchange,提问作者user3515670
相关产品推荐
相关产品推荐

