-
Notifications
You must be signed in to change notification settings - Fork 290
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
add "Compile and link dynamically to an external C library" example #294
Comments
I have a minimal working example here. I am currently verifying that the library produced is indeed dynamic. |
I'm starting to get into the weeds on this and before I go too deep I thought I should get some clarification here... What is the overall goal of this issue? Are we trying to create library that is dynamically linked to or are we simply creating a dynamic object which is then statically compiled into the executable? Specifically, there is a subtle difference between a shared object and a shared library -- the former can be statically linked into an executable, while the latter is linked at runtime. To expand upon this, the example from the cc crate docs would lead one to come up with the following code example: cc::Build::new()
.file("src/foo.c")
.shared_flag(true)
.pic(true)
.compiler("gcc")
.compile("foo.so"); However, as far as I can tell, this does not actually produce a shared library. Instead, it produces To return to the original question, what do we want to do with this recipe? It seems we have two options. The more complex option:
The alternative:
As an aside, is there an easy way to create an executable binary in rust? Cargo does not seem to offer this functionality so I imagine this is done via rustc -- I'll read the docs. Ideally I'd like to get option 1 working with a compiled binary, then delete the shared lib and watch the binary give me an error when it is subsequently run. |
We would be interested in linking to a full blown external shared library (probably the title could be more descriptive). So we just assume existence of some *.so file with given symbols. As you have noticed the cc crate does not seam to provide a way to create a *.so file (although we might drop a note about it in their bugtracker as the feature would be desirable and their docs are not perfectly clear on it imho)
Please see information under "Configuring a target" you are probably looking for |
Thanks for the links @budziq ! Very useful info. I'll follow-up with the cc team. |
Not necessarily. We can still:
Then it still might an interesting example. |
Great points. I think the latter might be more interesting. To that end, it might be worth having a separate section for cargo with various recipes that show what you can do with cargo (e.g., linking to a shared lib). The main takeaways I got from the book with regards to cargo were creating binary and library projects, and the commands |
@j-haj I feel that this example is stepping (possibly neck deep) into a FFI territory that is quite scattered in the rust landscape. For now I've marked this as "needs clarification" and created an issue to gather some opinions here before we proceed here. |
Use cc crate to compile and link dynamically to an external/system C library from a build.rs file.
Please note that it will be important to mention that this is a build time crate.
Most likely we will dedicate a separate (last) chapter like "build_time.md"
cc #236
The text was updated successfully, but these errors were encountered: