Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link sqlite3 statically in Cargo.toml

I have created a rust application, which uses the sqlite crate from crates.io

I am just using this crate as is and when I run my application with cargo run, I do get what I want. However, it seems my application has now a dependency to sqlite3.dll , which needs to be in my path.

From the Cargo docs that I read, my understanding is that the sqlite crate itself is linked statically, but not the C Library that it depends on.

On my own computer, I solved this by simply copying the sqlite3.dll into a folder in my path or to the same directory, where cargo has created my executable.

But, I realised that this didn't work with any sqlite3.dll file, but it worked with the one I found somewhere deep under the .multirust folder.

Additionally, when I give this application to someone else, I would also have to make sure that the DLL is also there.

So, I would like to avoid these complications by statically linking this DLL to my executable.

Is there a way to tell Cargo link this C Library statically without diving into custom build scripts?

like image 571
Kağan Kayal Avatar asked Sep 21 '25 01:09

Kağan Kayal


1 Answers

The sqlite crate depends on the sqlite3-sys crate to provide the FFI towards SQLite. This crate in turn depends on the sqlite3-src crate, which includes an optional feature called bundle - if you specify this feature then it bundles a copy of SQLite into your rust binary.

To do this, specify the dependency on this package manually like this:

[dependencies.sqlite3-src]
version="0.2"
features=["bundled"]

After doing this the generated binary should not link towards sqlite3.dll. I couldn't test that for Windows, but it worked for Linux:

$ ldd target/debug/so_sqlite
        linux-vdso.so.1 =>  (0x00007ffcf7972000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f1781fb9000)
        librt.so.1 => /lib64/librt.so.1 (0x00007f1781db1000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f1781b95000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f178197f000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f17815b2000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f17824d3000)
like image 50
harmic Avatar answered Sep 23 '25 10:09

harmic