Skip to content

Building shared libraries on Mac requires '-undefined dynamic_lookup' #62874

Open
@jeff-davis

Description

@jeff-davis

Problem

A shared library used as a plugin will often reference symbols in the host program. These symbols will remain unresolved at build time, but will be resolved at the time the shared library is loaded into the host program.

Cargo on Mac fails to build any shared library that can't resolve the symbols at build time.

I expect cargo to build a crate containing unresolved symbols successfully; which works fine on ubuntu.

Steps

A crate with the following Cargo.toml:

[package]
name = "fail"
version = "0.1.0"
authors = ["jefdavi"]
edition = "2018"

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

and src/lib.rs:

extern "C" {
  static unresolved: i32;
}

#[no_mangle]
extern fn myfunc() {
  unsafe {
    println!("{}", unresolved);
  }
}

Possible Solution(s)

A workaround is to specify:

[build] rustflags = ["-Clink-args=-undefined dynamic_lookup"]
in .cargo/config, but that's not acceptable for a crate author (https://github.com/jeff-davis/postgres-extension.rs), who doesn't have control over the crate users' config files.

Notes

Output of cargo version:

cargo 1.36.0 (c4fcfb725 2019-05-15)

18.6.0 Darwin Kernel Version 18.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesO-macosOperating system: macOST-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions