# Building FoundationDB C API static Library for Rust Programing language

**URL:** <https://forums.foundationdb.org/t/building-foundationdb-c-api-static-library-for-rust-programing-language/2006>\
**Category:** Using FoundationDB\
**Created:** [March 9, 2020, 9:04am UTC](https://forums.foundationdb.org/t/building-foundationdb-c-api-static-library-for-rust-programing-language/2006 "2020-03-09T09:04:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nguyenduybinh](https://avatars.discourse-cdn.com/v4/letter/n/5f9b8f/32.png) [@nguyenduybinh](https://forums.foundationdb.org/u/nguyenduybinh)\
**Post date:** [March 9, 2020, 9:04am UTC](https://forums.foundationdb.org/t/building-foundationdb-c-api-static-library-for-rust-programing-language/2006/1 "2020-03-09T09:04:47Z")

</div>

Hi everyone,  
I’m using FDB with Rust for my application. I have a problem with build Rust Binary file with FDB C API that i want to build Rust application Binary as static libarary but FDB is a dynamic(sharing Libary), so that the build error.  
Anyone have experience with Rust + FoundationDB? Or can i build the FDB C API as static library instead of libfdb\_c.so?

Thanks for reading!

---

<div class="post-metadata">

**Author:** ![alexmiller](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/alexmiller/32/326_2.png) [@alexmiller](https://forums.foundationdb.org/u/alexmiller)\
**Post date:** [March 9, 2020, 9:48am UTC](https://forums.foundationdb.org/t/building-foundationdb-c-api-static-library-for-rust-programing-language/2006/2 "2020-03-09T09:48:15Z")

</div>

Technically you could, but linking libfdb\_c as a static library would be strongly discouraged. Each libfdb\_c is tied to a specific version of FDB, and statically linking to it would tie your application to one specific version of FDB. This would mean that there would be no way to upgrade your FDB cluster without having application downtime.

There is a project on the roadmap to introduce a stable wire protocol, which would allow a client to be written that would be free of libfdb\_c, and thus safe to fully statically link.

---

<div class="post-metadata">

**Author:** ![nguyenduybinh](https://avatars.discourse-cdn.com/v4/letter/n/5f9b8f/32.png) [@nguyenduybinh](https://forums.foundationdb.org/u/nguyenduybinh)\
**Post date:** [March 9, 2020, 11:36am UTC](https://forums.foundationdb.org/t/building-foundationdb-c-api-static-library-for-rust-programing-language/2006/3 "2020-03-09T11:36:00Z")

</div>

Thanks for your reply, i know the problem you said. But now, is there any solution for my problem? is there any solution to get libfdb\_c static?

---

<div class="post-metadata">

**Author:** ![markus.pilman](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/markus.pilman/32/379_2.png) [@markus.pilman](https://forums.foundationdb.org/u/markus.pilman)\
**Post date:** [March 9, 2020, 5:18pm UTC](https://forums.foundationdb.org/t/building-foundationdb-c-api-static-library-for-rust-programing-language/2006/4 "2020-03-09T17:18:34Z")

</div>

I don’t think there’s an officially supported way to get a static library (for the reasons Alex mentioned above).

It probably is possible to build one yourself. You could change `SHARED` to `STATIC` in this line and it probably will work (I didn’t test this though):

> <https://github.com/apple/foundationdb/blob/0e6ac08f268e0e6e51bb471d59c58b98423542c2/bindings/c/CMakeLists.txt#L36>

Be aware that even if you do this, your application will still link at least against glibc. So you won’t get a completely static executable.

---

<div class="post-metadata">

**Author:** ![josephg](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/josephg/32/84_2.png) [@josephg](https://forums.foundationdb.org/u/josephg)\
**Post date:** [March 9, 2020, 7:14pm UTC](https://forums.foundationdb.org/t/building-foundationdb-c-api-static-library-for-rust-programing-language/2006/5 "2020-03-09T19:14:04Z")

</div>

Why do you want a static binary? Rust can link to dynamic library files, and the rust foundationdb bindings I’ve seen out there do just that.

---

<div class="post-metadata">

**Author:** ![nguyenduybinh](https://avatars.discourse-cdn.com/v4/letter/n/5f9b8f/32.png) [@nguyenduybinh](https://forums.foundationdb.org/u/nguyenduybinh)\
**Post date:** [March 10, 2020, 2:12am UTC](https://forums.foundationdb.org/t/building-foundationdb-c-api-static-library-for-rust-programing-language/2006/7 "2020-03-10T02:12:36Z")

</div>

Hi josephg,  
I want to build my Rust Application to binary using musl libc to optimize my CI/CD pipeline for Rust Application. I have used Rust FoundationDB dynamic ok, but when using like that, it take a long time to build Docker Image because of recompile the denpendent everytime.

I expect to build the Docker image with Dockerfile like that:

> Blockquote  
> FROM rust:latest AS builder  
> RUN apt-get update && apt-get install musl-tools llvm clang -y  
> RUN wget [https://www.foundationdb.org/downloads/6.2.15/ubuntu/installers/foundationdb-clients\_6.2.15-1\_amd64.deb](https://www.foundationdb.org/downloads/6.2.15/ubuntu/installers/foundationdb-clients_6.2.15-1_amd64.deb) && dpkg -i foundationdb-clients\_6.2.15-1\_amd64.deb  
> WORKDIR /  
> RUN rustup target add x86\_64-unknown-linux-musl  
> RUN USER=root cargo new app  
> WORKDIR /app  
> COPY Cargo.toml Cargo.lock ./  
> RUN RUSTFLAGS=-Clinker=musl-gcc cargo build --release --target=x86\_64-unknown-linux-musl  
> RUN rm -f target/x86\_64-unknown-linux-musl/release/deps/ranker\*  
> RUN rm src/\*.rs  
> COPY src ./src  
> RUN RUSTFLAGS=-Clinker=musl-gcc cargo build --release --target=x86\_64-unknown-linux-musl

But it failed because of FDB is a share library.

Have you had some experience with Rust and FoundationDB and have any solution for my problem?

Thanks!

---

<div class="post-metadata">

**Author:** ![alexmiller](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/alexmiller/32/326_2.png) [@alexmiller](https://forums.foundationdb.org/u/alexmiller)\
**Post date:** [March 10, 2020, 3:31am UTC](https://forums.foundationdb.org/t/building-foundationdb-c-api-static-library-for-rust-programing-language/2006/8 "2020-03-10T03:31:34Z")

</div>

I would also recommend against building with musl in this fashion. libfdb\_c uses dlopen() to load other versions of libfdb\_c for upgrade support, and a statically linked executable and dlopen() isn’t supported in musl: [https://www.openwall.com/lists/musl/2012/12/08/4](https://www.openwall.com/lists/musl/2012/12/08/4). Even if it does work, in the best case, you’ll be the first person running FDB on musl, and all stability bets are off. In the worst case, the dlopen ends up bringing in the base system’s glibc, and then you have two libc’s both trying to manipulate the program.

---

<div class="post-metadata">

**Author:** ![nguyenduybinh](https://avatars.discourse-cdn.com/v4/letter/n/5f9b8f/32.png) [@nguyenduybinh](https://forums.foundationdb.org/u/nguyenduybinh)\
**Post date:** [March 10, 2020, 6:44am UTC](https://forums.foundationdb.org/t/building-foundationdb-c-api-static-library-for-rust-programing-language/2006/9 "2020-03-10T06:44:18Z")

</div>

Thanks so much for all your supports!
