Skip to content

Add example of linking against a C++ shared library#725

Open
domfarolino wants to merge 1 commit intorust-lang-nursery:masterfrom
domfarolino:link-to-cpp-shared-library
Open

Add example of linking against a C++ shared library#725
domfarolino wants to merge 1 commit intorust-lang-nursery:masterfrom
domfarolino:link-to-cpp-shared-library

Conversation

@domfarolino
Copy link

@domfarolino domfarolino commented Feb 18, 2025

fixes #294 and contributes to #328.

Please take a look @budziq, I'd love for the cookbook to have a simple example of Rust dynamically linking against a C/C++ shared library through a C ABI.


Things to check before submitting a PR

  • the tests are passing locally with cargo xtask test all
    • Note: almost every test fails for me locally. I'm not sure why, but I'm pretty sure it is not related to my commits.
  • commits are squashed into one and rebased to latest master
  • PR contains correct "fixes #ISSUE_ID" clause to autoclose the issue on PR merge
  • non rendered items are in sorted order (links, reference, identifiers, Cargo.toml)
    • Question: Sorted how? Lexicographically, or by use (i.e., link reference/use)
  • links to docs.rs have wildcard version https://docs.rs/tar/*/tar/struct.Entry.html
  • example has standard error handling
    • My liberal use of .unwrap() in basic helper code I think is OK here? But I'd love for someone to confirm!
  • code identifiers in description are in hyperlinked backticks
[`Entry::unpack`]: https://docs.rs/tar/*/tar/struct.Entry.html#method.unpack

Copy link
Contributor

@AndyGauge AndyGauge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! This is a great addition to the cookbook, but there are several technical and naming inconsistencies that need to be addressed before it's ready for merge:

  1. Title vs. Content: The section header is titled "Compile and link statically...", but the content describes dynamic linking to a shared library (.so).
  2. Filename Inconsistencies:
    • The prose mentions src/mylibrary.h and src/mylibrary.cc.
    • Code blocks are labeled src/library.c and src/library.h.
    • The Makefile expects src/mylibrary.c and src/mylibrary.h.
    • src/library.c includes "api.h", which isn't defined (should likely be library.h or mylibrary.h).
  3. Language/Compiler Mismatch: The implementation file is labeled .c but contains C++ code (<iostream>, std::cout). It should probably be named .cpp or .cc, and the Makefile should reflect this.
  4. Build Search Path: cargo:rustc-link-search=native=. assumes the library is in the root during build. While simple, it's often better to reference CARGO_MANIFEST_DIR or provide instructions on where the user should run the Makefile to ensure the .so is found.

Requesting changes to align the filenames, headers, and terminology!

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

Successfully merging this pull request may close these issues.

add "Compile and link dynamically to an external C library" example

2 participants