soundio-rs icon indicating copy to clipboard operation
soundio-rs copied to clipboard

Project doesn't build, linker error

Open xrudelis opened this issue 4 years ago • 5 comments

I have a minimal project which fails to build when the call to ctx.connect() is added to the code. This is my src/main.rs:

extern crate soundio;

fn main() -> Result<(), String> {
    let mut ctx = soundio::Context::new();
    ctx.set_app_name("mytest");
    ctx.connect()?;  // builds when this line is removed
    Ok(())
}

And Cargo.toml:

[package]
name = "mytest"
version = "0.1.0"
authors = ["xrudelis"]

[dependencies]
soundio = "0.2.1"

The output of cargo build ends like this, after many similar errors:

/usr/bin/ld: /home/xrudelis/mytest/target/debug/deps/liblibsoundio_sys-28f43b51c120f186.rlib(alsa.c.o): in function `instream_pause_alsa':
/home/xrudelis/.cargo/registry/src/github.com-1ecc6299db9ec823/libsoundio-sys-0.3.0/libsoundio/src/alsa.c:1843: undefined reference to `snd_pcm_pause'
/usr/bin/ld: /home/xrudelis/mytest/target/debug/deps/liblibsoundio_sys-28f43b51c120f186.rlib(alsa.c.o): in function `instream_get_latency_alsa':
/home/xrudelis/.cargo/registry/src/github.com-1ecc6299db9ec823/libsoundio-sys-0.3.0/libsoundio/src/alsa.c:1858: undefined reference to `snd_pcm_delay'
collect2: error: ld returned 1 exit status

I've attached the full output here of running cargo build --verbose on this project. Tested with Rust 1.40.0 and also a newer nightly. I am not very familiar with the rust compiler / linker but I have verified that I can build C projects with libsoundio just fine on this machine.

xrudelis avatar May 02 '20 18:05 xrudelis

I ran into a similar, if not the exact same, problem.

Here is what I did:

$ cd /tmp/
$ git clone https://github.com/RamiHg/soundio-rs
Cloning into 'soundio-rs'...
[...]
$ cargo build --example list_devices  --verbose
cargo build --example list_devices  --verbose
    Updating crates.io index
   Compiling cc v1.0.54
   Compiling pkg-config v0.3.17
   Compiling crossbeam v0.2.12
   Compiling hound v3.4.0
     Running `rustc --crate-name pkg_config /home/sebastian/.cargo/registry/src/github.com-1ecc6299db9ec823/pkg-config-0.3.17/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C debuginfo=2 -C metadata=7788bba80f4e792d -C extra-filename=-7788bba80f4e792d --out-dir /tmp/soundio-rs/target/debug/deps -L dependency=/tmp/soundio-rs/target/debug/deps --cap-lints allow`
     Running `rustc --crate-name crossbeam /home/sebastian/.cargo/registry/src/github.com-1ecc6299db9ec823/crossbeam-0.2.12/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C debuginfo=2 -C metadata=3e22032f1fcd9e73 -C extra-filename=-3e22032f1fcd9e73 --out-dir /tmp/soundio-rs/target/debug/deps -L dependency=/tmp/soundio-rs/target/debug/deps --cap-lints allow`
     Running `rustc --crate-name hound /home/sebastian/.cargo/registry/src/github.com-1ecc6299db9ec823/hound-3.4.0/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C debuginfo=2 -C metadata=1f86213401ff9e4f -C extra-filename=-1f86213401ff9e4f --out-dir /tmp/soundio-rs/target/debug/deps -L dependency=/tmp/soundio-rs/target/debug/deps --cap-lints allow`
     Running `rustc --crate-name cc --edition=2018 /home/sebastian/.cargo/registry/src/github.com-1ecc6299db9ec823/cc-1.0.54/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C debuginfo=2 -C metadata=0b29d51fbcae9d99 -C extra-filename=-0b29d51fbcae9d99 --out-dir /tmp/soundio-rs/target/debug/deps -L dependency=/tmp/soundio-rs/target/debug/deps --cap-lints allow`
   Compiling cmake v0.1.44
     Running `rustc --crate-name cmake /home/sebastian/.cargo/registry/src/github.com-1ecc6299db9ec823/cmake-0.1.44/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C debuginfo=2 -C metadata=d49ee41064c030b1 -C extra-filename=-d49ee41064c030b1 --out-dir /tmp/soundio-rs/target/debug/deps -L dependency=/tmp/soundio-rs/target/debug/deps --extern cc=/tmp/soundio-rs/target/debug/deps/libcc-0b29d51fbcae9d99.rmeta --cap-lints allow`
   Compiling libsoundio-sys v0.3.0 (/tmp/soundio-rs/libsoundio-sys)
     Running `rustc --crate-name build_script_build --edition=2018 libsoundio-sys/build.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=2b6578e006eb728e -C extra-filename=-2b6578e006eb728e --out-dir /tmp/soundio-rs/target/debug/build/libsoundio-sys-2b6578e006eb728e -C incremental=/tmp/soundio-rs/target/debug/incremental -L dependency=/tmp/soundio-rs/target/debug/deps --extern cc=/tmp/soundio-rs/target/debug/deps/libcc-0b29d51fbcae9d99.rlib --extern cmake=/tmp/soundio-rs/target/debug/deps/libcmake-d49ee41064c030b1.rlib --extern pkg_config=/tmp/soundio-rs/target/debug/deps/libpkg_config-7788bba80f4e792d.rlib`
     Running `/tmp/soundio-rs/target/debug/build/libsoundio-sys-2b6578e006eb728e/build-script-build`
     Running `rustc --crate-name libsoundio_sys --edition=2018 libsoundio-sys/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C debuginfo=2 -C metadata=49cd5c1f6decb2db -C extra-filename=-49cd5c1f6decb2db --out-dir /tmp/soundio-rs/target/debug/deps -C incremental=/tmp/soundio-rs/target/debug/incremental -L dependency=/tmp/soundio-rs/target/debug/deps -L native=/tmp/soundio-rs/target/debug/build/libsoundio-sys-56755798501abf76/out/lib -l static=soundio`
   Compiling soundio v0.2.1 (/tmp/soundio-rs)
     Running `rustc --crate-name soundio src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C debuginfo=2 -C metadata=b510c3b634dc2f67 -C extra-filename=-b510c3b634dc2f67 --out-dir /tmp/soundio-rs/target/debug/deps -C incremental=/tmp/soundio-rs/target/debug/incremental -L dependency=/tmp/soundio-rs/target/debug/deps --extern libsoundio_sys=/tmp/soundio-rs/target/debug/deps/liblibsoundio_sys-49cd5c1f6decb2db.rmeta -L native=/tmp/soundio-rs/target/debug/build/libsoundio-sys-56755798501abf76/out/lib`
warning: use of deprecated item 'std::error::Error::description': use the Display impl or to_string()
   --> src/error.rs:110:26
    |
110 |         f.write_str(self.description())
    |                          ^^^^^^^^^^^
    |
    = note: `#[warn(deprecated)]` on by default

warning: use of deprecated item 'std::error::Error::description': use the Display impl or to_string()
   --> src/error.rs:133:13
    |
133 |         err.description().to_string()
    |             ^^^^^^^^^^^

warning: 2 warnings emitted

     Running `rustc --crate-name list_devices examples/list_devices/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=a3d0ec763ee8473b -C extra-filename=-a3d0ec763ee8473b --out-dir /tmp/soundio-rs/target/debug/examples -C incremental=/tmp/soundio-rs/target/debug/incremental -L dependency=/tmp/soundio-rs/target/debug/deps --extern crossbeam=/tmp/soundio-rs/target/debug/deps/libcrossbeam-3e22032f1fcd9e73.rlib --extern hound=/tmp/soundio-rs/target/debug/deps/libhound-1f86213401ff9e4f.rlib --extern libsoundio_sys=/tmp/soundio-rs/target/debug/deps/liblibsoundio_sys-49cd5c1f6decb2db.rlib --extern soundio=/tmp/soundio-rs/target/debug/deps/libsoundio-b510c3b634dc2f67.rlib -L native=/tmp/soundio-rs/target/debug/build/libsoundio-sys-56755798501abf76/out/lib`
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/home/sebastian/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.1179wevemhp47kzt.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.1hnqs8cryn1xye4b.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.1l66ub2d93ew7ee8.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.1rq3xkwltcrtweoc.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.1th1fr048mbpaewc.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.1zgekcuwtjrec30d.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.2q5qg9xmfd6ytdtv.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.2w14di2e8gc0jii8.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.3647jmqydv90b6xq.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.37p3z8pde924k9dt.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.38yyk7r46768x9qa.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.3rc86ttted9lj1qb.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.3sraozhaniyxgc62.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.3zbkkntd7c4fvoq0.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.427xc5jp4pv7yfk4.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.44h1qoin3cf3s1s3.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.47q8liuyyrvzpx8.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.4gh5wxj785028fsh.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.4mne1bynmiwnjqxi.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.4podi8smfirqldq.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.56448zhieo25o110.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.56wbaujfc7x550nk.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.jx3h2mrig5ripjh.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.oh3kyd03qejel8t.rcgu.o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.qnxt8xo22919sur.rcgu.o" "-o" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b" "/tmp/soundio-rs/target/debug/examples/list_devices-a3d0ec763ee8473b.429p277egq30esn5.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/tmp/soundio-rs/target/debug/deps" "-L" "/tmp/soundio-rs/target/debug/build/libsoundio-sys-56755798501abf76/out/lib" "-L" "/home/sebastian/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/tmp/soundio-rs/target/debug/deps/libsoundio-b510c3b634dc2f67.rlib" "/tmp/soundio-rs/target/debug/deps/liblibsoundio_sys-49cd5c1f6decb2db.rlib" "-Wl,--start-group" "/home/sebastian/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-205127404fcba336.rlib" "/home/sebastian/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-a657948fb7763780.rlib" "/home/sebastian/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-4af901391177f653.rlib" "/home/sebastian/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-9bdd51edf637d1e0.rlib" "/home/sebastian/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libbacktrace-df9a9630b2ea2e1e.rlib" "/home/sebastian/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libbacktrace_sys-fddbfe63d5cc9339.rlib" "/home/sebastian/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-dc1fcc6e43806791.rlib" "/home/sebastian/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-e2f4bff19e00107e.rlib" "/home/sebastian/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-a152d13c1d7ed405.rlib" "/home/sebastian/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-d92b841792fc008d.rlib" "/home/sebastian/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-a64c249515c023f5.rlib" "/home/sebastian/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-57654524b3e40c00.rlib" "/home/sebastian/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-72180f1527a726a2.rlib" "-Wl,--end-group" "/home/sebastian/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-990cc8f4a91a3ab4.rlib" "-Wl,-Bdynamic" "-ldl" "-lrt" "-lpthread" "-lgcc_s" "-lc" "-lm" "-lrt" "-lpthread" "-lutil" "-ldl" "-lutil"
  = note: /usr/bin/ld: /tmp/soundio-rs/target/debug/deps/liblibsoundio_sys-49cd5c1f6decb2db.rlib(jack.c.o): in function `refresh_devices_bare':
          /tmp/soundio-rs/libsoundio-sys/libsoundio/src/jack.c:99: undefined reference to `jack_get_ports'
          /usr/bin/ld: /tmp/soundio-rs/libsoundio-sys/libsoundio/src/jack.c:110: undefined reference to `jack_port_by_name'
          /usr/bin/ld: /tmp/soundio-rs/libsoundio-sys/libsoundio/src/jack.c:114: undefined reference to `jack_free'
          /usr/bin/ld: /tmp/soundio-rs/libsoundio-sys/libsoundio/src/jack.c:119: undefined reference to `jack_port_flags'
          /usr/bin/ld: /tmp/soundio-rs/libsoundio-sys/libsoundio/src/jack.c:120: undefined reference to `jack_port_type'
[...]
          /usr/bin/ld: /tmp/soundio-rs/libsoundio-sys/libsoundio/src/alsa.c:1796: undefined reference to `snd_pcm_mmap_begin'
          /usr/bin/ld: /tmp/soundio-rs/target/debug/deps/liblibsoundio_sys-49cd5c1f6decb2db.rlib(alsa.c.o): in function `instream_end_read_alsa':
          /tmp/soundio-rs/libsoundio-sys/libsoundio/src/alsa.c:1825: undefined reference to `snd_pcm_mmap_commit'
          /usr/bin/ld: /tmp/soundio-rs/target/debug/deps/liblibsoundio_sys-49cd5c1f6decb2db.rlib(alsa.c.o): in function `instream_pause_alsa':
          /tmp/soundio-rs/libsoundio-sys/libsoundio/src/alsa.c:1843: undefined reference to `snd_pcm_pause'
          /usr/bin/ld: /tmp/soundio-rs/target/debug/deps/liblibsoundio_sys-49cd5c1f6decb2db.rlib(alsa.c.o): in function `instream_get_latency_alsa':
          /tmp/soundio-rs/libsoundio-sys/libsoundio/src/alsa.c:1858: undefined reference to `snd_pcm_delay'
          collect2: error: ld returned 1 exit status
          

error: aborting due to previous error

error: could not compile `soundio`.

Caused by:
  process didn't exit successfully: `rustc --crate-name list_devices examples/list_devices/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=a3d0ec763ee8473b -C extra-filename=-a3d0ec763ee8473b --out-dir /tmp/soundio-rs/target/debug/examples -C incremental=/tmp/soundio-rs/target/debug/incremental -L dependency=/tmp/soundio-rs/target/debug/deps --extern crossbeam=/tmp/soundio-rs/target/debug/deps/libcrossbeam-3e22032f1fcd9e73.rlib --extern hound=/tmp/soundio-rs/target/debug/deps/libhound-1f86213401ff9e4f.rlib --extern libsoundio_sys=/tmp/soundio-rs/target/debug/deps/liblibsoundio_sys-49cd5c1f6decb2db.rlib --extern soundio=/tmp/soundio-rs/target/debug/deps/libsoundio-b510c3b634dc2f67.rlib -L native=/tmp/soundio-rs/target/debug/build/libsoundio-sys-56755798501abf76/out/lib` (exit code: 1)

Full output

I'm running a freshly updated Archlinux. The soundio library itself in libsoundio-sys/libsoundio compiles fine for me so it's likely not a dependencie issue. It rather looks like the linker pass for den final list_devices binary is missing references to the pulseaudio, alsa and jack libraries.

LongHairedHacker avatar Jun 21 '20 15:06 LongHairedHacker

I did some digging: libsoundio-sys wraps a statically linked version of libsoundio, that lives somewhere in the target folder: target/debug/build/libsoundio-sys-56755798501abf76/out/lib/libsoundio.a. If we poke it with nm we get a bunch of undefined like:

                 U snd_card_next
                 U snd_config_update
                 U snd_config_update_free_global
                 U snd_ctl_card_info
                 U snd_ctl_card_info_get_name
                 U snd_ctl_card_info_sizeof
                 U snd_ctl_close
                 U snd_ctl_open
                 U snd_ctl_pcm_info
                 U snd_ctl_pcm_next_device
                 U snd_device_name_free_hint
                 U snd_device_name_get_hint
                 U snd_device_name_hint

or

                 U jack_activate
                 U jack_client_close
                 U jack_client_open
                 U jack_connect
                 U jack_free
                 U jack_get_buffer_size
                 U jack_get_ports
                 U jack_get_sample_rate
                 U jack_on_shutdown
                 U jack_port_by_name
                 U jack_port_flags
                 U jack_port_get_buffer
                 w jack_port_get_latency_range
                 U jack_port_name
                 U jack_port_register
                 U jack_port_type
                 U jack_set_buffer_size_callb

Which are likely all of the symbols the linker complained about earlier. So while libsoundio-sys itself compiles fine, the resulting library is missing all the symbols for the underlying library. And that causes the linker to fail as soon as we want to link an actual binary against it.

LongHairedHacker avatar Jun 21 '20 15:06 LongHairedHacker

It works for me after making the following changes.

diff --git a/libsoundio-sys/build.rs b/libsoundio-sys/build.rs
index 5e3d33c..21afdb1 100644
--- a/libsoundio-sys/build.rs
+++ b/libsoundio-sys/build.rs
@@ -105,6 +105,10 @@ fn main() {
 
     // Link soundio.
     println!("cargo:rustc-link-lib=static=soundio");
+    // Link the dependencies
+    println!("cargo:rustc-link-lib=asound");
+    println!("cargo:rustc-link-lib=pulse");
+    println!("cargo:rustc-link-lib=jack");
 
     // OSX
     if target.contains("apple") {

However I suspect that adding those linker arguments will probably break the build on OSX and Windows.

LongHairedHacker avatar Jun 21 '20 20:06 LongHairedHacker

@LongHairedHacker how about

if target.contains("linux") {
    println!("cargo:rustc-link-lib=asound");
    println!("cargo:rustc-link-lib=pulse");
    println!("cargo:rustc-link-lib=jack");
}

Works for me. I ran into the same issue on my Raspberry Pi

phip1611 avatar Jul 13 '20 14:07 phip1611

That should work.

The other thing I ran into is that that linking against all three libraries means your binary will always depend all of those libraries even if your are just using pulse audio. In addition to that you have to have all of them installed for the linking to work. So a proper fix should also address that somehow.

I stopped using soundio-rs a few weeks ago, because I ran into some other unrelated problems further downstream in my project and decided to ditch platform independent sound for now. I switched to libpulse_simple_binding, which suits my usecase a little better.

LongHairedHacker avatar Jul 15 '20 14:07 LongHairedHacker