Skip to content

Cargo does not use output of build.rs for the bin target in projects containing both bin and lib #7506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
operutka opened this issue Oct 11, 2019 · 5 comments
Labels
C-bug Category: bug

Comments

@operutka
Copy link

Problem
I have a project that was originally an application and recently I've decided to make it I also a library. The project uses a build script to link with libpcap. After I added src/lib.rs, the project stopped building.

When I run cargo with the -v option, I can see that the lib target gets build just fine but the bin target fails because the linker cannot find some symbols (contents of libpcap). I can also see that the rustc command for the lib target contains arguments -l static=net_devices -l dylib=pcap but these arguments are not present for the bin target.

I'd expect all targets to use the output of build.rs.

Steps

  1. Clone https://github.com/angelcam/arrow-client
  2. Make sure that libpcap is installed on your system
  3. Add src/lib.rs and
  4. Run cargo build --features all

Notes

Output of cargo version:
cargo 1.38.0 (23ef9a4 2019-08-20)

@operutka operutka added the C-bug Category: bug label Oct 11, 2019
@ehuss
Copy link
Contributor

ehuss commented Oct 11, 2019

This is expected and intended, otherwise you would get duplicate symbols. To work with this, the code using the libraries needs to be in lib.rs, and main.rs should just use arrow_client.

@operutka
Copy link
Author

@ehuss Good to know! Thank you for the explanation.

@zoumi
Copy link

zoumi commented Jan 23, 2020

I want to use winres to embed icons into my bin output.But cargo only specify the resource for the lib.
How can I make this work?
my Cargo.toml


[package]
name = "usb_pc"
version = "0.1.0"
authors = ["zoumi"]
edition = "2018"


[[bin]]
name = "usb"
build = "build.rs"
path = "src/main.rs"

[lib]
crate-type = ["cdylib"]
name = "Control"
path = "src/lib/mod.rs"
...
...

[target.'cfg(windows)'.build-dependencies]
winres = "0.1"

compile output

Running E:\rustdir\target\debug\build\usb_pc-b1e9a06638b500ef\build-script-build
Fresh failure v0.1.5
Fresh log4rs v0.9.0
Fresh iui v0.3.0
Running rustc --edition=2018 --crate-name usb src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=4bbdbf4966dc28f4 --out-dir 'E:\rustdir\target\debug\deps' -C 'incremental=E:\rustdir \target\debug\incremental' -L 'dependency=E:\rustdir\target\debug\deps' --extern 'chrono=E:\ru........bwinapi-7e317701135e936d.rlib' --extern 'winreg=E:\rustdir\target\debug \deps\libwinreg-64335c9ef5f53258.rlib' -L 'native=E:\rustdir\target\debug\build\usb_pc- c3f3edc6e4329b2c\out' -L 'native=E:\rustdir\target\debug\build\ui-sys-18d99634946e2216 \out\build\out\Release'

Running rustc --edition=2018 --crate-name Control src/lib/mod.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type cdylib --emit=dep-info,link -C debuginfo=2 -C metadata=4bbdbf4966dc28f4 --out-dir 'E:\rustdir\target\debug\deps' -C 'incremental=E:\rustdir \ta..........b2c\out' -l dylib=resource -L 'native=E:\rustdir\target\debug\build\ui- sys-18d99634946e2216\out\build\out\Release'

@mbrubeck
Copy link
Contributor

This is expected and intended, otherwise you would get duplicate symbols. To work with this, the code using the libraries needs to be in lib.rs, and main.rs should just use arrow_client.

Is this true only for static libs? Could it be relaxed when dynamic linking is explicitly specified?

@mbrubeck
Copy link
Contributor

See also #5881.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

4 participants