Linking to cmake project that links to other cmake projects

I'm using the cmake crate to compile a CMake project which depends and compiles other CMake projects

This is my build.rs:

extern crate cmake;
use cmake::Config;

fn main() {
    let dst = Config::new("src/cpp")
        .define("COMPILE_TARGET", "DESKTOP_x86_64")
        .define("FLAVOR", "DESKTOP")
        .define("LIBOPENVPN3_NOT_BUILD_EXAMPLES", "TRUE")
        .build();

    println!("cargo:rustc-link-search=native={}", dst.display());
    println!("cargo:rustc-link-lib=static=libopenvpn3");
    println!("cargo:rustc-link-lib=dylib=stdc++");
}

This is how my src/cpp/CMakeLists.txt compiles libopenvpn3

add_library(libopenvpn3 SHARED OpenVpnInstance.cpp)
target_link_libraries(libopenvpn3 crypto ssl lzo lz4 tins)

However, when I build with cargo build, I get undefined references to objects from all these libraries: crypto ssl lzo lz4 tins.

I also tried making libopenvpn3 STATIC so the final libopenvpn3 will include all of the needed libraries: crypto, ssl, lzo, lz4, tins, like this: add_library(libopenvpn3 STATIC OpenVpnInstance.cpp) but I still get the error. I think the other libraries (crypto, ssl, lzo, lz4, tins) will only be included together if they are static too. Or not?

Anyways, I think that I should relink with these libraries on build.rs, like this:

println!("cargo:rustc-link-lib=dylib=openvpn");
println!("cargo:rustc-link-lib=dylib=crypto");
println!("cargo:rustc-link-lib=dylib=lzo");
println!("cargo:rustc-link-lib=dylib=lz4");
println!("cargo:rustc-link-lib=dylib=tins");

but I don't know where they are, because they are generated from CMakeLists.txt, and I don't think hardcoding the path to where they are generated is a good idea.

What should I do here?

On the CMakeLists.txt, I was doing:

install(TARGETS libopenvpn3 DESTINATION .)

but then I did

install(TARGETS libopenvpn3 crypto ssl lzo lz4 tins DESTINATION .)

so it installs all the libs needed

Then, this build.rs works:

extern crate cmake;
use cmake::Config;

fn main() {
    let dst = Config::new("src/cpp")
        .define("COMPILE_TARGET", "DESKTOP_x86_64")
        .define("FLAVOR", "DESKTOP")
        .define("LIBOPENVPN3_NOT_BUILD_EXAMPLES", "TRUE")
        .build();

    println!("cargo:rustc-link-search=native={}", dst.display());
    println!("cargo:rustc-link-lib=dylib=stdc++");
    println!("cargo:rustc-link-lib=static=libopenvpn3");
    println!("cargo:rustc-link-lib=static=crypto");
    println!("cargo:rustc-link-lib=static=lzo");
    println!("cargo:rustc-link-lib=static=lz4");
    println!("cargo:rustc-link-lib=static=tins");
    println!("cargo:rustc-link-lib=static=ssl");
}

I don't know, however, why I have to link them again in Rust. They should be included in libopenvpn3 since they are static, shouldn't them?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.