sgrif / mysqlclient-sys

Rust bindings for libmysqlclient
Apache License 2.0
37 stars 31 forks source link

Static linking of libmysqlclient #9

Closed Emulator000 closed 6 years ago

Emulator000 commented 6 years ago

How I can static link the library in order to not require the installation of "libmysqlclient.so.20" in other machines?

I tried compiling with x86_64-unknown-linux-musl without luck.

sgrif commented 6 years ago

That's going to depend heavily on your setup. Are you using pkg-config or mysql_config? Do you have the correct version of mysqlclient installed on your machine compiled for static linking? You mentioned you're trying to use musl -- Did you compile mysql from source against musl?

Emulator000 commented 6 years ago

Actually I managed to resolve adding a "build.rs" file with this content:

fn main() {
    println!(r"cargo:rustc-link-lib=static=mysqlclient");
    println!(r"cargo:rustc-link-lib=static=stdc++");
    println!(r"cargo:rustc-link-lib=static=z");
}
Emulator000 commented 4 years ago

For everyone that is approaching this issue again, there is an update in the rustc compiler and the previous procedure is not supported now, in order to compile statically the libmysqlclient you have to use the nighlty compiler with the #![feature(static_nobundle)] added in the lib.rs or main.rs and this is the updated build.rs:

fn main() {
    println!(r"cargo:rustc-link-lib=static=mysqlclient");
    println!(r"cargo:rustc-link-lib=static-nobundle=stdc++");
    println!(r"cargo:rustc-link-lib=static=z");
}

Personally I use a more complete build.rs file as follows:

fn main() {
    println!(r"cargo:rustc-link-lib=static=mysqlclient");
    println!(r"cargo:rustc-link-lib=static-nobundle=stdc++");
    println!(r"cargo:rustc-link-lib=static=z");
    println!(r"cargo:rustc-link-lib=static=ssl");
    println!(r"cargo:rustc-link-lib=static=crypto");
}

Hope that this help!

BlackDex commented 4 years ago

@Emulator000 And on which OS are you building this? With which dev libraries installed?

Emulator000 commented 4 years ago

@BlackDex I'm bulding it on Ubuntu 20.04 LTS with libmysqlclient-dev installed, I think that libmariadb-dev it's okay too.

You can see a compiled binary in this way here: https://github.com/facile-it/rabbitmq-consumer/releases/tag/v1.0.3