将 Rust 项目链接到链接到其他 cmake 项目的 cmake 项目

时间:2021-03-04 05:56:33

标签: c++ cmake rust rust-cargo

我正在使用 cmake crate 编译一个 CMake 项目,该项目依赖并编译其他 CMake 项目

这是我的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++");
}

这就是我的 src/cpp/CMakeLists.txt 编译 libopenvpn3

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

但是,当我使用 cargo build 构建时,我从所有这些库中获得了对对象的未定义引用:crypto ssl lzo lz4 tins

我也尝试制作 libopenvpn3 STATIC 所以最终的 libopenvpn3 将包括所有需要的库:crypto, ssl, lzo, lz4, tins,像这样:add_library(libopenvpn3 STATIC OpenVpnInstance.cpp) 但我仍然得到错误。我认为其他库 (crypto, ssl, lzo, lz4, tins) 只有在它们也是静态的情况下才会包含在 libopenvpn3 中。还是不行?

无论如何,我认为我应该在 build.rs 上重新链接这些库,如下所示:

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");

但我不知道它们在哪里,因为它们是从 CMakeLists.txt 生成的,而且我认为硬编码生成它们的路径不是一个好主意。

我应该在这里做什么?

更新:

错误示例:

  /home/dev/orwell/liborwell_rust/src/libopenvpn3/src/cpp/openvpn3/openvpn3/openvpn/openssl/util/pem.hpp:66: undefined reference to `BIO_new_mem_buf'
  /usr/bin/ld: /home/dev/orwell/liborwell_rust/src/libopenvpn3/src/cpp/openvpn3/openvpn3/openvpn/openssl/util/pem.hpp:73: undefined reference to `PEM_read_bio'
  /usr/bin/ld: /home/dev/orwell/liborwell_rust/src/libopenvpn3/src/cpp/openvpn3/openvpn3/openvpn/openssl/util/pem.hpp:91: undefined reference to `CRYPTO_free'
  /usr/bin/ld: /home/dev/orwell/liborwell_rust/src/libopenvpn3/src/cpp/openvpn3/openvpn3/openvpn/openssl/util/pem.hpp:92: undefined reference to `CRYPTO_free'
  /usr/bin/ld: /home/dev/orwell/liborwell_rust/src/libopenvpn3/src/cpp/openvpn3/openvpn3/openvpn/openssl/util/pem.hpp:93: undefined reference to `CRYPTO_free'
  /usr/bin/ld: /home/dev/orwell/liborwell_rust/src/libopenvpn3/src/cpp/openvpn3/openvpn3/openvpn/openssl/util/pem.hpp:95: undefined reference to `BIO_free'
  /usr/bin/ld: /home/dev/orwell/lab/hyper_vpn/target/debug/deps/liblibopenvpn3-7498dcb6c355a9d6.rlib(OpenVpnInstance.cpp.o): in function `SimplePacketCrafter::replaceSourceAddressIpv4(unsigned char*, unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
  /home/dev/orwell/liborwell_rust/src/libopenvpn3/src/cpp/SimplePacketCrafter.h:117: undefined reference to `Tins::IP::IP(unsigned char const*, unsigned int)'
  /usr/bin/ld: /home/dev/orwell/liborwell_rust/src/libopenvpn3/src/cpp/SimplePacketCrafter.h:118: undefined reference to `Tins::IPv4Address::IPv4Address(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
  /usr/bin/ld: /home/dev/orwell/liborwell_rust/src/libopenvpn3/src/cpp/SimplePacketCrafter.h:118: undefined reference to `Tins::IP::src_addr(Tins::IPv4Address)'
  /usr/bin/ld: /home/dev/orwell/liborwell_rust/src/libopenvpn3/src/cpp/SimplePacketCrafter.h:119: undefined reference to `Tins::PDU::serialize()'
  /usr/bin/ld: /home/dev/orwell/lab/hyper_vpn/target/debug/deps/liblibopenvpn3-7498dcb6c355a9d6.rlib(OpenVpnInstance.cpp.o): in function `Tins::IP::~IP()':
  /home/dev/orwell/liborwell_rust/src/libopenvpn3/src/cpp/libtins/include/tins/ip.h:63: undefined reference to `vtable for Tins::IP'
  /usr/bin/ld: /home/dev/orwell/liborwell_rust/src/libopenvpn3/src/cpp/libtins/include/tins/ip.h:63: undefined reference to `Tins::PDU::~PDU()'

1 个答案:

答案 0 :(得分:0)

在 CMakeLists.txt 上,我在做:

install(TARGETS libopenvpn3 DESTINATION .)

但后来我做到了

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

所以它安装了所有需要的库

然后,这个 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=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");
}

然而,我不知道为什么我必须在 Rust 中再次链接它们。它们应该包含在 libopenvpn3 中,因为它们是静态的,不是吗?