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

Compiling cdylib #1331

Open
smvoss opened this issue Mar 28, 2024 · 1 comment
Open

Compiling cdylib #1331

smvoss opened this issue Mar 28, 2024 · 1 comment

Comments

@smvoss
Copy link

smvoss commented Mar 28, 2024

Hi,

I am attempting to compile my rust / c++ project into a single .so file that can be dynamically loaded by another application (implementation of a c++ headerfile that uses some rust calls, linked from a c++ app). This seems to generally work when using a static library with only needing to link the top-level library:

-Ltarget/debug -lmylib

(assuming target/debug/libmylib.a)

however when attempting to do the same for a cdylib I need to also link the library created during the build script as such:

-Ltarget/debug -lmylib ${OUT_DIR}/lib-built-by-cxxbuild.a

(assuming targ/debug/libmylib.so)

If I do not explicitly include the static library built as part of build.rs (cxx_build::[..].compile("built-by-cxxbuiild")) I have missing symbols.

Any advice on how to bridge this gap, so that all symbols are exported in the .so. I have attempted to add some linking flags to my build script, such as println!("cargo::rustc-link-lib=static={..}) to no avail.

If it makes a difference, the code exclusively calls from c++ to rust, and not the other way. This is being used as a C++ "shim" to provide a rust implementation of a c++ interface for a shared library.

Thanks!

@jwhear
Copy link

jwhear commented Apr 24, 2024

I was tinkering with this myself today. My workaround solution at the moment is to build the rust code as a static library. Here's my setup (crate name is sqlite_adaptor).

// Cargo.toml
[lib]
crate-type = ["staticlib"]

This produces target/debug/sqlite_adaptor.a

The next step is to link this static library with the cxxbridge static library built by your build.rs into a shared library:

// Makefile
debug: target/debug/libsqlite_adaptor.so
bridge = $(shell find target/debug/ -name libcppbridge.a)

target/debug/libsqlite_adaptor.so:
	gcc -shared -Wl,--whole-archive target/debug/libsqlite_adaptor.a \
		-Wl,--no-whole-archive $(bridge) \
		-o target/debug/libsqlite_adaptor.so

This is pretty janky right now as I'm still hacking on it, but it produces a shared library with the symbols from Rust library and the needed bridge code in target/debug/libsqlite_adaptor.so

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

2 participants