Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ld cannot find -lmy_custom_c_library within custom docker image #347

Closed
lynaghk opened this issue Nov 12, 2019 · 5 comments
Closed

ld cannot find -lmy_custom_c_library within custom docker image #347

lynaghk opened this issue Nov 12, 2019 · 5 comments

Comments

@lynaghk
Copy link

lynaghk commented Nov 12, 2019

Hi, thanks for Cross --- it's a great tool!

I'm trying to use cross from a Mac to build a binary for Raspberry Pi 0 (arm-unknown-linux-musleabihf).
Plain rust code works great, but I'm having trouble linking to a C library, pigpio.
The error is:

  = note: /usr/local/bin/../lib/gcc/arm-linux-musleabihf/6.4.0/../../../../arm-linux-musleabihf/bin/ld: cannot find -lpigpio

Here's what I've done so far:

  1. Built a Docker image that contains my library. I've verified that it exists:

    root@e3c15482d660:/usr/local/lib# file libpigpio.so.1
    libpigpio.so.1: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, stripped
    
  2. Specified library search path by adding to build.rs: println!(r"cargo:rustc-link-search=/usr/local/lib");
    I've verified that = note: "arm-linux-musleabihf-gcc" "-Wl,--as-needed" ... [lots of output] contains both "-L" "/usr/local/lib" and "-lpigpio".

I'm running cross via:

cross build --target arm-unknown-linux-musleabihf

and in my Cross.toml have

[target.arm-unknown-linux-musleabihf]
image = "lynaghk/arm-unknown-linux-musleabihf-0.1.16-pigpio"

Within my rust program itself, I'm declaring the C externs:

#[link(name = "pigpio", kind = "dylib")]
extern "C" {
    fn gpioInitialise() -> i32;
}

As far as I understand, this should all work.
I've done it plenty of times when compiling rust natively.
Hopefully I just have a typo somewhere...

Anyway, thanks again for the great tool.
Aside from this linking issue, it's working wonderfully!

@gdesmott
Copy link
Contributor

gdesmott commented Apr 2, 2020

I'm hitting a similar issue. Did you manage to make it work by any chance? :)

@gdesmott
Copy link
Contributor

gdesmott commented Apr 2, 2020

Ah, I got it working! Here is how I did:

  • cross-built my C library (GStreamer) with prefix=/usr/local
  • Installed it to a temp directory (DESTDIR) that I then extracted in the docker image so it's actually installed in /usr/local
  • Relied on pkg-config to detect the lib so all the linking paths are automatically configured.

@Alexhuszagh
Copy link
Contributor

As of #727, we've added the search directories for pkg-config to all *-linux-gnu images, however, this won't automatically apply to musl libc images. The solution by @gdesmott is correct, I'll likely add a wiki contribution explaining this in more detail. There's different ways of adding it to the expected search path, but it's pretty important it doesn't interfere with system packages.

@Alexhuszagh
Copy link
Contributor

Alexhuszagh commented Jul 2, 2022

This has been documented on the wiki and we've added a sample project. If you're building from source, you'll have to make sure it's found by the linker, likely using pkg-config (although other solutions work, like if using CMake). I'll be adding documentation on adding vcpkg binaries as well shortly.

@Alexhuszagh
Copy link
Contributor

FYI: I've fully documented using vcpkg and Conan (along with Meson) on the wiki, including a sample project and automatic integration into Rust.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants