Open
Description
The -shared
and -static
flags are linkage flags, and don't do anything when passed alongside -c
.
Taking the example from shared_flag:
cc::Build::new()
.file("src/foo.c")
.shared_flag(true)
.compile("libfoo.so");
What that does is:
cc $OTHER_FLAGS -shared -o foo.o -c src/foo.c
ar cq libfoo.so.a foo.o
ar s libfoo.so.a
The first command is strictly equivalent to cc $OTHER_FLAGS -o foo.o -c src/foo.c
. So in practice, it makes no difference whether shared_flag was passed or not, and compile("libfoo.so")
would make you think you'd end up with a dynamic library, but you don't.
Metadata
Metadata
Assignees
Labels
No labels
Activity
jerry73204 commentedon Jun 4, 2022
Is the issue intended to be fixed?
ssh352 commentedon Jun 1, 2023
how can I build dylib with cc crate?
zhangzhuang15 commentedon Jul 21, 2023
Same Problem.
XuKeli8080174 commentedon Jan 2, 2024
Same Problem too
zhangzhuang15 commentedon Jan 2, 2024
sladg commentedon Jan 22, 2024
Any solution to this one?
NobodyXu commentedon Jan 22, 2024
This issue is due to cc always run ar, changing it should be possible.
cc @ChrisDenton @thomcc What's your thoughts on this?
silvanshade commentedon May 14, 2024
Here's a workaround for this:
src/<lib>.c
modules correctly nvim-neorocks/lux#187Walter-Reactor commentedon Dec 17, 2024
Just following up here, this is doubly true on MSVC where the -shared flag actually triggers an error
zhangzhuang15 commentedon Dec 17, 2024
madsmtm commentedon Apr 4, 2025
Heads up: This is being discussed in #1444.
Personally, I'm against having this feature in
cc-rs
, since it's complex to support correctly (for example, you'd wantcc-rs
to use the same linker asrustc
, but when using e.g.RUSTFLAGS=-Clinker=rust-lld
, we'd have to know how to pass the correct flags to the linker. And e.g. for correctness on at least macOS, we'd need to pass-platform_version
. Might be possible to work around by passing-fuse-ld=rust-lld
to the compiler driver? But even then, how do we then avoid nonsensical things likecc -fuse-ld=cc
? Should we instead invokerustc
as the linker? But what if the user doesn't haverustc
in their PATH?).I'd be more in favour of someone creating a separate crate like
linker-rs
, and implementing the desired capabilities there (since linking is mostly orthogonal to compiling).NobodyXu commentedon Apr 4, 2025
Well yeah, having a separate crate might be more reasonable, given that we already expose enough API for them to use cc to do compiler detection/etc
Like we can expose some APIs useful for link-rs/dynlib-rs