Can't compile simple CMake project using Cargo

I'm trying to build a CMake C++ project with Cargo and link to my Rust program. The C++ program have some C++ dependencies (all of them comes from ZLMediaKit) as you can see below.

Here's my CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
set (CMAKE_CXX_STANDARD 17)
project(lib_ZLMediaKit_LIB CXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")

#Disable ZLMediaKit tests
set(ENABLE_TESTS OFF FORCE)
option(ENABLE_TESTS OFF)
#No openssl for ZLMediaKit for now, because it uses 1.1-dev and ubuntu bionic only has 1.0-dev
#TODO: enable OpenSSL since we're building in openvpn3 
set(ENABLE_OPENSSL FALSE FORCE)
option(ENABLE_OPENSSL OFF)

add_subdirectory(ZLMediaKit)

add_library(zlmediakit_cpp_interface STATIC interface.cpp ZLMedia.cpp)
target_include_directories(zlmediakit_cpp_interface PUBLIC 
. 
${CMAKE_CURRENT_SOURCE_DIR}/ZLMediaKit/src 
${CMAKE_CURRENT_SOURCE_DIR}/ZLMediaKit/3rdpart/ZLToolKit/src)
target_link_libraries(zlmediakit_cpp_interface mpeg mov flv libstdc++ zlmediakit zltoolkit)
#install(TARGETS zlmediakit zltoolkit mpeg mov flv DESTINATION .)
install(TARGETS zlmediakit_cpp_interface DESTINATION .)

Here's the lib.rs:

extern crate cmake;
use cmake::Config;

fn main()
{
    let dst = Config::new("zlmediakit_lib").build();       

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

I'm getting undefined reference for a symbol in the zlmediakit library, which is linked to zlmediakit_cpp_interface, so I don't know why this happens:

 = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-Wl,--eh-frame-hdr" "-L" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/home/dev/orwell/liborwell_rust/src/zlmediakit_rust/target/debug/examples/simple_rtsp_client-43202b32a359c197.2ioah68t1ylhfv46.rcgu.o" "/home/dev/orwell/liborwell_rust/src/zlmediakit_rust/target/debug/examples/simple_rtsp_client-43202b32a359c197.388oj6cmh3y0dcp.rcgu.o" "/home/dev/orwell/liborwell_rust/src/zlmediakit_rust/target/debug/examples/simple_rtsp_client-43202b32a359c197.3apczk0uw2pqyp4b.rcgu.o" "/home/dev/orwell/liborwell_rust/src/zlmediakit_rust/target/debug/examples/simple_rtsp_client-43202b32a359c197.3b6ztkga9lg9dcjg.rcgu.o" "/home/dev/orwell/liborwell_rust/src/zlmediakit_rust/target/debug/examples/simple_rtsp_client-43202b32a359c197.3d8w20uufael1kwk.rcgu.o" "/home/dev/orwell/liborwell_rust/src/zlmediakit_rust/target/debug/examples/simple_rtsp_client-43202b32a359c197.4uwrg7i2m4bu5ohv.rcgu.o" "/home/dev/orwell/liborwell_rust/src/zlmediakit_rust/target/debug/examples/simple_rtsp_client-43202b32a359c197.gskmpeqspqmbxim.rcgu.o" "-o" "/home/dev/orwell/liborwell_rust/src/zlmediakit_rust/target/debug/examples/simple_rtsp_client-43202b32a359c197" "/home/dev/orwell/liborwell_rust/src/zlmediakit_rust/target/debug/examples/simple_rtsp_client-43202b32a359c197.hh4bm99l3vowkea.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/home/dev/orwell/liborwell_rust/src/zlmediakit_rust/target/debug/deps" "-L" "/home/dev/orwell/liborwell_rust/src/zlmediakit_rust/target/debug/build/zlmediakit-b2dbd6a86088b69d/out" "-L" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/dev/orwell/liborwell_rust/src/zlmediakit_rust/target/debug/deps/libzlmediakit-af1c512661973fdb.rlib" "/home/dev/orwell/liborwell_rust/src/zlmediakit_rust/target/debug/deps/liblibc-0a43f320b0d2a047.rlib" "-Wl,--start-group" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-f14aca24435a5414.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-48d342a8b48d1d01.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-14bc0820888c8eb3.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-9cbd9e217bff06bc.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-31826136df98934e.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-075976a117c8fd5d.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-2d5cbedfbf17a011.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-0474372ff08c5319.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-d437c34460d2315a.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-fb61ed1b8cc4de79.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-bf76d1b643bfc9f0.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-a1b53aa7fddcf418.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-28585e57fac45c73.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-64801769bc15ab28.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-541997b56bb98660.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-cdea3c81adab3d12.rlib" "-Wl,--end-group" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-cd9f15a39fb65cbc.rlib" "-Wl,-Bdynamic" "-lstdc++" "-ldl" "-lrt" "-lpthread" "-lgcc_s" "-lc" "-lm" "-lrt" "-lpthread" "-lutil" "-ldl" "-lutil"
  = note: /usr/bin/ld: /home/dev/orwell/liborwell_rust/src/zlmediakit_rust/target/debug/deps/libzlmediakit-af1c512661973fdb.rlib(interface.cpp.o): in function `void __gnu_cxx::new_allocator<mediakit::MediaPlayer>::construct<mediakit::MediaPlayer>(mediakit::MediaPlayer*)':
          /usr/include/c++/9/ext/new_allocator.h:147: undefined reference to `mediakit::MediaPlayer::MediaPlayer(std::shared_ptr<toolkit::EventPoller> const&)'
          collect2: error: ld returned 1 exit status

How can I have this error? zlmediakit is a static libray that I link to my static library zlmediakit_cpp_interface.

my repo: https://github.com/lucaszanella/zlmediakit_rust

just run cargo build --examples

Hi

The first issue is that your build script should be in a build.rs file.
Second issue, this line:
target_link_libraries(zlmediakit_cpp_interface mpeg mov flv libstdc++ zlmediakit zltoolkit)
Doesn’t really work the way you migh be thinking. If you’re building a static library, no linking happens, so the libraries there need to be in the build.rs file:
println!("cargo:rustc-link-lib=static=zlmediakit");
etc.
For static libraries, that cmake line helps other consuming cmake projects (if you specify PUBLIC linkage) to transitively link those libraries when you export a cmake config file from a cmake build.

thanks, now I undrstand what's happening. I thought it archived all the .a libraries into one giant one.

Anyways, the build script was in build.rs, I typed the wrong name.

I also had to install all the dependencies:

install(TARGETS zlmediakit_cpp_interface zltoolkit zlmediakit mpeg mov flv DESTINATION .)

Now, I get this:

error: could not find native static library zlmediakit, perhaps an -L flag is missing?

Then I did

    println!("cargo:rustc-link-search=native={}", dst.display());
    println!("cargo:rustc-link-lib=static=zlmediakit_cpp_interface"); 
    println!("cargo:rustc-link-lib=static=zlmediakit");
    println!("cargo:rustc-link-lib=static=zltoolkit");
    println!("cargo:rustc-link-lib=static=mov");
    println!("cargo:rustc-link-lib=static=mpeg");
    println!("cargo:rustc-link-lib=static=flv");
    println!("cargo:rustc-link-lib=dylib=stdc++");

These seem to be installed directly in the OUT_DIR. So you can also do something like:

let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

println!("cargo:rustc-link-search=native={}", out_dir.display());