Skip to content

shared_flag and static_flag don't work #594

Open
@glandium

Description

@glandium
Contributor

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.

Activity

jerry73204

jerry73204 commented on Jun 4, 2022

@jerry73204

Is the issue intended to be fixed?

ssh352

ssh352 commented on Jun 1, 2023

@ssh352

how can I build dylib with cc crate?

zhangzhuang15

zhangzhuang15 commented on Jul 21, 2023

@zhangzhuang15

Same Problem.

XuKeli8080174

XuKeli8080174 commented on Jan 2, 2024

@XuKeli8080174

Same Problem too

zhangzhuang15

zhangzhuang15 commented on Jan 2, 2024

@zhangzhuang15
sladg

sladg commented on Jan 22, 2024

@sladg

Any solution to this one?

NobodyXu

NobodyXu commented on Jan 22, 2024

@NobodyXu
Collaborator

This issue is due to cc always run ar, changing it should be possible.

cc @ChrisDenton @thomcc What's your thoughts on this?

silvanshade

silvanshade commented on May 14, 2024

@silvanshade

Here's a workaround for this:

let out_dir = Path::new(std::env::var("OUT_DIR").unwrap());
let builder = cc::Build::new().file("src/foo.c");
let objects = builder.compile_intermediates();
builder
    .get_compiler()
    .to_command()
    .args(["-shared", "-o"])
    .arg(out_dir.join("libfoo.so"))
    .args(&objects)
    .status()
    .unwrap();
Walter-Reactor

Walter-Reactor commented on Dec 17, 2024

@Walter-Reactor

Just following up here, this is doubly true on MSVC where the -shared flag actually triggers an error

zhangzhuang15

zhangzhuang15 commented on Dec 17, 2024

@zhangzhuang15
linked a pull request that will close this issue on Mar 30, 2025
madsmtm

madsmtm commented on Apr 4, 2025

@madsmtm
Collaborator

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 want cc-rs to use the same linker as rustc, 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 like cc -fuse-ld=cc? Should we instead invoke rustc as the linker? But what if the user doesn't have rustc 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

NobodyXu commented on Apr 4, 2025

@NobodyXu
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @glandium@jerry73204@madsmtm@silvanshade@sladg

      Issue actions

        shared_flag and static_flag don't work · Issue #594 · rust-lang/cc-rs