我正在使用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
构建时,我从所有这些库中获得了对对象的 undefined reference :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()'
最佳答案
在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中,因为它们是静态的,不是吗?
关于c++ - 将Rust项目链接到与其他cmake项目链接的cmake项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66469387/