Skip to content
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

Building a shared library #250

Closed
j-haj opened this issue Oct 11, 2017 · 6 comments
Closed

Building a shared library #250

j-haj opened this issue Oct 11, 2017 · 6 comments

Comments

@j-haj
Copy link

j-haj commented Oct 11, 2017

It seems that cc offers the ability to build a shared object via the .shared(true) method, but this object is then statically linked into the rust executable. Additionally, by default on macOS (which uses gcc as an alias for clang), the use of the .shared(true) method doesn't actually do anything because clang doesn't recognize -shared as a valid flag when creating an object file. As far as I can tell, there does not seem to be a way to build a shared library and then link it with the rust executable with this crate.

This seems to be confirmed here where cc is always using ar, rather than the necessary ld for shared libraries.

Is there something I'm missing? If not I think this would be useful functionality.

@alexcrichton
Copy link
Member

IIRC that flag was added to fix a bug but didn't actually end up fixing the bug or wasn't needed. In general though it's not the intention of this crate to produce shared libraries, just static libraries.

@tjpalmer
Copy link

I understand the current intent of this library. I think there could be value in expanding the scope to building shared libraries and executables. I suggest this because it could be useful for other needs, and you've already done some good legwork here (like the windows registry, for example) that others would need to duplicate.

For my current needs (building a different language at runtime to c/c++, but others could have other situations), it seems like another alternative is for me to expect cmake to do this work instead.

Could you consider expanding the scope even just a little for now, or would you recommend relying on cmake?

Or I guess another alternative is compile to rust with c included there ...

@alexcrichton
Copy link
Member

Unfortunately everything to do with shared libraries, loading, and copying them around I do not have time to review or maintain. I'd prefer to keep this library as simple as possible and targeted towards just "getting C into Rust" which 99% of the time doesn't require shared libraries. I think that this function should likely be built on top of cc-rs, if at all.

@tjpalmer
Copy link

Thanks for the feedback. I think I'll just start from this project as a base for getting what I need done. Thanks for your hard work!

@alexcrichton
Copy link
Member

I'm going to close this because I would prefer to not support building a shared library with this crate. It'd be neat to see if a crate could be made which depends on cc which does support this though!

@Jakobeha
Copy link

For people who are still interested in building shared libraries, at least on macOS you just have to run clang -dynamiclib -o libfoobar.dylib <object files produced during compilation>.

e.g.

let status = Command::new("/usr/bin/clang")
        .args(["-dynamiclib", "-o", "libfoobar.dylib"])
        .args(find_object_files_in("."))
        .status()
        .expect("link command failed, IO error");
if !status.success() {
    panic!("link command failed, exit status {}", status);
}

Pass -undefined dynamic_lookup if your shared library references other shared symbols

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

No branches or pull requests

4 participants