在Mac上构建共享库时出现链接器错误

时间:2019-06-14 17:22:33

标签: rust rust-cargo

我正在尝试在Mac上构建rust cdylib共享库,其中某些符号仅在最终会加载它的主机程序中定义。我遇到了许多有关未解析符号的构建错误,但是符号应该在构建时尚未解析。如果存在任何未解决的符号错误,则应在主机程序中的dlopen()时发生。

我的箱子可以在gunt-gcc-7.4或clang-6.0上的ubuntu上正常运行,但是在Mac上不起作用。

extern "C" {
    pub static mut unresolved_symbol: i32;
}

#[export_name="some_func"]
pub extern "C" fn some_func() {
    unsafe {
        println!("{}", unresolved_symbol);
    }
}
[package]
name = "sharedlib"
version = "0.1.0"
authors = ["Jeff Davis <jeff@j-davis.com>"]
edition = "2018"

[lib]
crate-type = ["cdylib"]

错误:

$ cc --version
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
$ cargo build
   Compiling sharedlib v0.1.0 (/Users/ABCWXYZ/code/rust/sharedlib)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-m64" "-L" "/Users/ABCWXYZ/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/ABCWXYZ/code/rust/sharedlib/target/debug/deps/sharedlib.1fuvldog2z497idd.rcgu.o" "/Users/ABCWXYZ/code/rust/sharedlib/target/debug/deps/sharedlib.1qjfsnlmqnj43b7e.rcgu.o" "/Users/ABCWXYZ/code/rust/sharedlib/target/debug/deps/sharedlib.4t85wgccdhlen7ed.rcgu.o" "-o" "/Users/ABCWXYZ/code/rust/sharedlib/target/debug/deps/libsharedlib.dylib" "-Wl,-exported_symbols_list,/var/folders/yn/ps5k_p2j7_g7gfzdc_4jvqf80000gn/T/rustcONVtkx/list" "/Users/ABCWXYZ/code/rust/sharedlib/target/debug/deps/sharedlib.512ryopkdhq7yt5h.rcgu.o" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/Users/ABCWXYZ/code/rust/sharedlib/target/debug/deps" "-L" "/Users/ABCWXYZ/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/ABCWXYZ/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-1dfdd87ff6e60945.rlib" "/Users/ABCWXYZ/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-2afa3d9ef23111d9.rlib" "/Users/ABCWXYZ/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libbacktrace_sys-36ad4a075c8befe6.rlib" "/Users/ABCWXYZ/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-5af683adadcc6add.rlib" "/Users/ABCWXYZ/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-11930efa977c702c.rlib" "/Users/ABCWXYZ/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-ebd54b7045c72e75.rlib" "/Users/ABCWXYZ/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-f9e1c7e0a4778297.rlib" "/Users/ABCWXYZ/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-06903edfb4cff5d5.rlib" "/Users/ABCWXYZ/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-9becee61e6692014.rlib" "/Users/ABCWXYZ/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-f3814eca990faded.rlib" "-lSystem" "-lresolv" "-lc" "-lm" "-dynamiclib" "-Wl,-dylib"
  = note: Undefined symbols for architecture x86_64:
            "_unresolved_symbol", referenced from:
                _some_func in sharedlib.1qjfsnlmqnj43b7e.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 `sharedlib`.

To learn more, run the command again with --verbose.

在ubuntu上,它仅生成并且库正确包含导出的符号和未解析的符号。我可以将其加载到定义了未解析符号的主机程序中。

0 个答案:

没有答案
相关问题