Hi,
I've made a first version of a software in rust, and now want to distribute it. It builds and runs fine when I call "cargo run -r". However, the executable I want to distribute is in target/release/ and when I try to execute it I get the following error:
dyld[39812]: Library not loaded: @rpath/libc++.1.dylib
I am on macOS and I have used the CXX crate to call some C++ code from my software. Does anyone know how cargo executes my program differently from when I simply run it in the terminal (or as part of a packaged .app bundle?).
I can manage to run the program without cargo using "install_name_tool -add_rpath ..." to change the rpath, but then the binary only works on my computer. Is there some way to compile a rust program on macOS that links to C++ code that can run without a cargo install?
It seems like your executable is linked against libc++.1.dylib from the rpath rather than /usr/lib/libc++.1.dylib[1] for some reason. Did you enable rpath or something? Or maybe have it installed through Homebrew in a way that takes precedence over the system version when running cargo? Do you have rustc itself installed through rustup or through Homebrew?
cargo runs executables with DYLD_LIBRARY_PATH containing the rustc toolchain and target/{debug,release}/deps. Neither of which should contain libc++.1.dylib however as that is a system library.
Note that this file doesn't actually exist on the disk on modern macOS versions anymore. Instead it is part of the linker cache, but still referenced by this path, so eg dlopen("/usr/lib/libc++.1.dylib") will work just fine. âŠī¸
Hi bjorn3, thank you so much!
At some point I got an error when compiling tests that wanted to link to usr/lib/libc++.1.dylib. I noticed that this file doesn't exist there but found one elsewhere so I told the linker to use this which worked fine when running with cargo.
Your comment about the nonexistence of usr/lib/libc++.1.dylib solves it, i can just let it look for this nonexisting library and it just works anyways :).
Thanks for your explanation of DYLD_LIBRARY_PATH, now I know what the difference is when running it with cargo and from the terminal :). Just to answer your question, I installed through rustup.