How do I fix the error "couldn't load codegen backend" on macOS? - rust

main.rs:
fn main() {
println!("Hello world!");
}
$ rustc main.rs:
error: couldn't load codegen backend "/usr/local/lib/rustlib/i686-apple-darwin/codegen-backends/librustc_codegen_llvm-llvm.dylib": "dlsym(RTLD_DEFAULT, CFURLConnectionCreateWithProperties): symbol not found"
I'm using macOS 10.11.6 with Rust 1.30.1 installed via rustup. gcc is Apple LLVM version 8.0.0 (clang-800.0.42.1).

I've experienced the same problem on a Gentoo system on x86. For some reason, the codegen libs have been installed to
/usr/lib/rust-1.34.2/rust-1.34.2/rustlib/i686-unknown-linux-gnu/codegen-backends
Note the repeated rust-1.34.2 directory.
My solution was to symlink the dir from the expected place; you must do this with root privileges:
# cd /usr/lib/rust-1.34.2/rustlib/i686-unknown-linux-gnu/
# ln -s ../../rust-1.34.2/rustlib/i686-unknown-linux-gnu/codegen-backends .
You may have to repeat that when a new version of Rust is installed.

Related

Rust flo_draw graphics library is not building

The issue I'm having are regarding a copy-pasted flo_draw example program. I am using vscode and the rust-anylizer extension. It tells me failed to run build scripts, check server logs for more information apon copying the code in. I don't know how to check these or what they are. Using cargo run from the terminal yields no better results.
I tried to copy in the code below:
// src.main.rs
use flo_draw::*;
use flo_draw::canvas::*;
fn main() {
with_2d_graphics(|| {
let canvas = create_canvas_window("Hello, world");
});
}
$ cargo run
Compiling futures v0.3.25
Compiling semver v0.11.0
Compiling flo_render_gl_offscreen v0.3.0
Compiling allsorts v0.5.1
Compiling winit v0.24.0
error: failed to run custom build command for `flo_render_gl_offscreen v0.3.0`
Caused by:
process didn't exit successfully: `/<path to program>/target/debug/build/flo_render_gl_offscreen-9d539a79f1cb257c/build-script-build` (exit status: 101)
\--- stderr
thread 'main' panicked at 'Unable to find libclang: "couldn't find any valid shared libraries matching: \['libclang.so', 'libclang-*.so', 'libclang.so.*', 'libclang-*.so.*'\], set the `LIBCLANG_PATH` environment variable to a path where one of these files can be found (invalid: \[\])"', /<path to home>/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.54.0/src/lib.rs:1959:31
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
flo_draw, or, more specifically, one of its dependencies, links to the external C library and therefore have to generate the matching API. This is done using bindgen, which requires clang to be installed in your system.
Installation instructions from the linked page:
Debian-based Linuxes
# apt install llvm-dev libclang-dev clang
Ubuntu 18.04 provides the necessary packages directly.
Arch
# pacman -S clang
Fedora
# dnf install clang-devel

rust glium compile error missing package on ubuntu

I try compile this in Rust with glium on an Ubutnu machine.
#[macro_use]
extern crate glium;
fn main() {
println!("Hello, world!");
}
It results in this error:
--- stderr
thread 'main' panicked at '`"pkg-config" "--libs" "--cflags" "fontconfig" "fontconfig >= 2.11.1"` did not exit successfully: exit status: 1
error: could not find system library 'fontconfig' required by the 'servo-fontconfig-sys' crate
--- stderr
Package fontconfig was not found in the pkg-config search path.
Perhaps you should add the directory containing `fontconfig.pc'
to the PKG_CONFIG_PATH environment variable
No package 'fontconfig' found
Package fontconfig was not found in the pkg-config search path.
Perhaps you should add the directory containing `fontconfig.pc'
to the PKG_CONFIG_PATH environment variable
No package 'fontconfig' found
', /home/matej/.cargo/registry/src/github.com-1ecc6299db9ec823/servo-fontconfig-sys-5.1.0/build.rs:34:17
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error[E0554]: `#![feature]` may not be used on the stable release channel
--> /home/matej/.cargo/registry/src/github.com-1ecc6299db9ec823/thiserror-1.0.38/src/lib.rs:238:34
|
238 | #![cfg_attr(provide_any, feature(provide_any))]
| ^^^^^^^^^^^
The first error says it cannot find the fontconfig library. What should I do here? How do I get that library installed? I do not know what to do with the second error, either.
OS: Ubuntu 22.04 LTS
I have added glium = "*" to cargo dependencies.
Addendum
The first error is solved after running cargo clean -
but the second error still persists. Any ideas?
Using this command in the terminal, install the required dependency:
sudo apt install libfontconfig1-dev

How to cross compile Rust code from Intel Mac to M1 Mac by Zig?

These days I'm curious about the cross-compile in Rust.As I know, it's very simple to do the same thing in Go or Zig.But in Rust, it's very difficult.I write Rust code in my Intel MacBook Pro, want to cross compile to get the executable file which could be run in M1 MacBook Pro. I take some tries and get some errors in linker.
Here is what I have tried.
I install Rust tool and Zig following the official instruction.
$ rustc --version
rustc 1.65.0 (897e37553 2022-11-02)
$
$ zig version
0.10.0-dev.4560+828735ac0
And my rust project structure is just like this:
hello-world -----
|--- .cargo--
|-- config.toml
|--- src-----
|-- main.rs
Cargo.toml
zig
And there are some files look like:
// main.rs
fn main() {
println!("hello world")
}
// config.toml
[target.aarch64-apple-darwin]
rustflags = ["-C", "linker-flavor=ld", "-C", "link-self-contained=no", "-C", "link-arg=-L/usr/local/opt/libiconv/lib"]
linker = "./zig"
// Cargo.toml
[package]
name = "hello-world"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
#[profile.release]
#lto=true
// zig
#!/usr/bin/env zsh
zig cc -target aarch64-macos-none $#
And I install libiconv by brew:
$ brew install libiconv
it puts libiconv in directory /usr/local/opt/libiconv/lib
$ ls /usr/local/opt/libiconv/lib
libcharset.1.dylib libcharset.a libcharset.dylib libiconv.2.dylib libiconv.a libiconv.dylib
I install target aarch64-apple-darwin by rustup and could see:
$ rustup show
Default host: x86_64-apple-darwin
rustup home: /Users/<my name>/.rustup
installed targets for active toolchain
--------------------------------------
aarch64-apple-darwin
x86_64-apple-darwin
x86_64-unknown-linux-gnu
x86_64-unknown-linux-musl
active toolchain
----------------
stable-x86_64-apple-darwin (default)
rustc 1.65.0 (897e37553 2022-11-02)
then I start to build in hello-world directory:
$ CC=./zig cargo build --target aarch64-apple-darwin
...
...
note: error(link): mismatched cpu architecture: expected aarch64, found x86_64
error: MismatchedCpuArchitecture
I find some solutions from stackoverflow, it said I can use macOS sdk to cross-compile.
But I think there are more than one method to finish cross-compile, and really want to know how to solve it using zig.
By the way,I just know the basic steps of assembly, not in details, I don't have experience in linking.
Could anyone give me some suggestions ?
Any answer will be appreciated.
This might not be enough to solve your problem fully, but you need at the very least to pass the cross-compilation flags to Zig, and you also need to use the cc subcommand.
Note also that Zig uses macos instead of darwin.
So you need to set CC="./zig cc -target aarch64-macos", that said, I'm not sure if Rust supports spaces in the CC command now (at least a while ago it did not). If it doesn't you will need to wrap everything in a bash script (zcc.sh):
#!/bin/sh
ZIG_LOCAL_CACHE_DIR="$HOME/tmp" zig cc -target aarch64-macos $#
And then CC=./zcc.sh
Note also that the same will need to be done for zig c++ if you also depend on C++ code. The env variable to set in that case is CXX.
Here you can find somebody doing this to build wasmtime:
https://actually.fyi/posts/zig-makes-rust-cross-compilation-just-work/
Finally, you can probably save yourself all this trouble simply by using cargo-zigbuild:
https://github.com/rust-cross/cargo-zigbuild

Rust Bare-Metal Cross-Compilation for AArch64: can't find crate for `core`

I am trying to cross-compile an example rust code as library for bare-metal AArch64 on Linux (KDE-Neon). Unfortunately it does not work. This is my example rust code (lib.rs):
#![no_std]
#[no_mangle]
pub extern "C" fn double_value (a : u32) -> u32
{
a / 2
}
According to [1] I first installed rustup with:
sudo snap install rustup --classic
Afterwards, I followed [2] and ran:
rustup toolchain list
rustup install stable
rustup default stable
Then I followed [1] and [3] and ran:
rustup target add aarch64-unknown-none
However when I try to compile afterwards, I doesn't work, neither with rustc nor with cargo:
rustc:
rustc --crate-type=lib lib.rs --target=aarch64-unknown-none
error[E0463]: can't find crate for `core`
|
= note: the `aarch64-unknown-none` target may not be installed
error: aborting due to previous error
cargo:
Cargo.toml:
[package]
name = "rust_baremetal_lib"
version = "0.1.0"
edition = "2018"
[lib]
name = "rust_baremetal_lib"
path = "src/lib.rs"
crate-type = ["staticlib"]
[dependencies]
cargo build --lib --target=aarch64-unknown-none
Compiling rust_baremetal_lib v0.1.0 (/home/kilian/code/rust_link/rust_baremetal_lib)
error[E0463]: can't find crate for `core`
|
= note: the `aarch64-unknown-none` target may not be installed
error: aborting due to previous error
For more information about this error, try `rustc --explain E0463`.
error: could not compile `rust_baremetal_lib`
To learn more, run the command again with --verbose.
To me it looks like rustc and cargo cannot find the core library, although it should be installed, as seen when running rustc --print:
rustc --print target-list|grep arch64-unknown-none
aarch64-unknown-none
aarch64-unknown-none-softfloat
I already looked on the internet but didn't find any clues unfortunately. I hope someone can help me find the issue!
[1] https://rust-lang.github.io/rustup/cross-compilation.html
[2] No default toolchain configured after installing rustup
[3] https://doc.rust-lang.org/nightly/rustc/platform-support.html
The problem seemed to result from a broken rust installation. I removed all packages related to rust that I could found via apt and snap. Afterwards I reinstalled rust via the recommended way [1]. Then I again ran:
rustup target add aarch64-unknown-none
Afterwards Cargo complained, that a "panic handler" was missing in my example code, so I inserted one, following [2]. My example code now looks like this:
#![no_std]
use core::{
panic::PanicInfo,
};
#[no_mangle]
pub extern "C" fn double_value (a : u32) -> u32
{
a * 2
}
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {
continue;
}
}
Finally I can now cross-compile this example to an AArch64 bare metal static library with:
cargo build --lib --target=aarch64-unknown-none
[1] https://www.rust-lang.org/tools/install
[2] https://interrupt.memfault.com/blog/zero-to-main-rust-1

Rust compiler can't find crate for 'std'

I recently downloaded and unpacked the Rust Language from this site (Linux 64-bit).
I then installed Rust using the given script in the download install.sh:
root#kali:~# /root/rust-1.9.0-x86_64-unknown-linux-gnu/install.sh
install: uninstalling component 'rustc'
install: creating uninstall script at /usr/local/lib/rustlib/uninstall.sh
install: installing component 'rustc'
install: installing component 'rust-std-x86_64-unknown-linux-gnu'
install: installing component 'rust-docs'
install: installing component 'cargo'
Rust is ready to roll.
I am trying to install a crate with cargo, but I keep running into this error:
root#kali:~# cargo install racer
Updating registry `https://github.com/rust-lang/crates.io-index`
Compiling winapi v0.2.7
Compiling bitflags v0.5.0
error: can't find crate for `std` [E0463]
error: aborting due to previous error
Build failed, waiting for other jobs to finish...
error: can't find crate for `std` [E0463]
error: aborting due to previous error
error: failed to compile `racer v1.2.10`, intermediate artifacts can be found at `/root/target-install`
cargo install cargo-edit failed with the same result as above, so it's not limited to one particular package.
Even putting a simple program:
fn main() {
println!("Hello, world!");
}
in a file named hello.rs and running rustc hello.rs does not compile; it gives the same error: error: can't find crate for 'std' [E0463].
The download came with a directory named rust-std-x86_64-unknown-linux-gnu, which I assume is the std crate. How do I instruct rustc to find this directory when trying to locate the std crate?
The following will work for the simplest of compilations.
Assuming you extracted the tar file to, say
$HOME/rust-1.10.0-x86_64-unknown-linux-gnu
Then run
arch=x86_64-unknown-linux-gnu
dl=$HOME/rust-1.10.0-$arch
$dl/rustc/bin/rustc -L $dl/rustc/lib \
-L $dl/rust-std-$arch/lib/rustlib/$arch/lib \
hello.rs
But I'm sure a better way would be to run rustup as Chris Morgan suggest.
Coupla more points
You shouldn't compile code as root.
You may have to relogin or run bash -l to get the environment setup by rustup.
(Fellow rust newb here)
For me (Arch Linux) removing system's Rust fixed the issue.
pacman -Rc rust
I think there was a conflict among user installed Rust and system installed one.

Resources