Building cdylib as plugin, failure on mac with missing host symbols, works on linux

#1

I’m working on a rust wrapper to implement plugins for Pure Data.
Pure Data calls these “externals”.

I’ve created a puredata-sys bindgen generated library wrapping the header that defines the interface I need to use to register and implement a plugin… which I have successfully used to create a simple plugin which builds (with cargo/rustc) and runs (loaded inside the Pure Data application) on linux… but I am failing to build it on osx.

The project is here:

Inside that I do:
cd examples/helloworld
cargo build
which eventually gives me:

error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-m64" "-L" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.13m5x14nnn3aoqmm.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.18r33ikj8cp76tl3.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.197m85357eygexev.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.1fe96eh33nt0owih.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.2e20bsgggkihrfo2.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.2ewwend2fjcds4qu.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.2zoxjt1m82dga9pa.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.32482a7ffjtbwfum.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.3b4zfwo4edm2memg.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.3lbseq12eq955hov.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.3w6g6gsq8gvixb5k.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.4h1he5h59zzwwbkf.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.4pamvswdqc4tb911.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.4v28u5ceeqhc608p.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.4z7krzudkbhrxw2z.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.5bmvr7slrwy76iwo.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.5dt3c3nwcyt8k93b.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.8k8d9jivuc0g27h.rcgu.o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.aqshk88qlzos9c8.rcgu.o" "-o" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/libhelloworld.dylib" "-Wl,-exported_symbols_list,/var/folders/xw/3w07fx_x0bj4fhm063rpmrp80000gn/T/rustcxe5YDq/list" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/helloworld.2oqf9yuyw2w5n8tv.rcgu.o" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps" "-L" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/alex/personal/src/puredata-rust/examples/helloworld/target/debug/deps/libpuredata_sys-999a2f91ec152bad.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-084d3c32b37d9af9.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-8cecbcf5f274d94d.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libbacktrace_sys-b03662eae585a920.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-61913e6feeeff678.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-75148971013e38a9.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-a5c22490cb7dbe67.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-d6235fdae9bccd59.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-04d7f10312787f91.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-0d676ca0824aedaa.rlib" "/Users/alex/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-ea47f39b75b3f57e.rlib" "-lSystem" "-lresolv" "-lc" "-lm" "-dynamiclib" "-Wl,-dylib"
  = note: Undefined symbols for architecture x86_64:
            "_class_addbang", referenced from:
                _helloworld_setup in helloworld.13m5x14nnn3aoqmm.rcgu.o
            "_gensym", referenced from:
                _helloworld_setup in helloworld.13m5x14nnn3aoqmm.rcgu.o
            "_class_new", referenced from:
                _helloworld_setup in helloworld.13m5x14nnn3aoqmm.rcgu.o
            "_pd_new", referenced from:
                helloworld::helloworld_new::h8642749205a325fe in helloworld.13m5x14nnn3aoqmm.rcgu.o
            "_post", referenced from:
                helloworld::helloworld_bang::h2e12cc0108f55cee in helloworld.13m5x14nnn3aoqmm.rcgu.o
          ld: symbol(s) not found for architecture x86_64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)


error: aborting due to previous error

error: Could not compile `helloworld`.

These are symbols that are available in the Pure Data host that loads the library, so it is understandable that they’re not defined yet.

Clearly I’m not setting up something correctly in my build settings. Any ideas?

BTW, I am able to build an example C based project on the same mac host, no problem:

git clone https://github.com/pure-data/externals-howto
cd externals-howto/example1/
make

This example C project is what I based my rust project on… so it should essentially be the same thing.

#2

This is not Rust specific. The Darwin linker doesn’t allow undefined symbol by default. But you can turn it off with -undefined dynamic_lookup .

See this related topic on StackOverflow: https://stackoverflow.com/questions/3695234/what-is-the-deal-with-undefined-symbols-in-a-shared-library-or-dylib

And for the how-to repo, it has code in makefile like this: https://github.com/pure-data/externals-howto/blob/ff4adb9b5e4dfce9834a13c0884b766c9e708a4c/pd-lib-builder/Makefile.pdlibbuilder#L570

1 Like
#3

fantastic!

i created the file examples/helloworld/.cargo/config
with:

[build]
rustflags = ["-C", "link-args=-Wl,-undefined,dynamic_lookup"]

and now cargo build works on osx.
Will have to verify on linux again, I wonder, can I add something to my bindgen generated library that does this? I saw that there is a link_args attribute you can use but that it is behind a feature gate…

Anyways, thanks a ton @381 !!

#4

I think what you can do is add this in your build.rs detect the os type and determine the linker arguments.