Building lib with a c++ dependency usind cmake-rs

Hi, i have created a simple lib using cxx that used a external c++ lib as a dependency, i was able to compile and link this lib using cmake-rs, but for some reason when i tried to use the lib i get a error from the compiler saying it was unable to find the c++ lib a have used as a dependency.

The build.rs file of the lib:

use cmake::Config;

fn main() {
    cxx_build::bridge("src/lib.rs")
        .file("cpp/main.cpp")
        .flag_if_supported("-std=c++17")
        .compile("cxx-bridge");

    let dst = Config::new("cpp")
        .cxxflag("-std=c++17")
        .env("REALM_CPP_NO_TESTS", "ON")
        .env("CMAKE_CXX_FLAGS", "-fpermissive -Wchanges-meaning")
        .build();

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

    println!("cargo:rerun-if-changed=src/lib.rs");
    println!("cargo:rerun-if-changed=cpp/main.cpp");
    println!("cargo:rerun-if-changed=cpp/main.h");
}

When runing cargo build on the lib itself it compiles without any problem, but when i try to run it as a dependency of another project i receive the error:

  = note: /usr/bin/ld: cannot find -lcpprealm: No such file or directory
          collect2: error: ld returned 1 exit status

I don't have many experience using cpp code with rust so any help is much appreciated.

Your search path is probably wrong. Make sure path in dst exists and contains libcpprealm.a (unix) or cpprealm.lib (on windows)

exactly. when it is build as standalone crate, the output directory "happens to" be picked up by the linker.

the Config::build() returns the path to the library, but the linker search path expects a list of directories that contains the library. I think Path::parent() should do the trick.

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

I verified the target output and discovered that the lib was located on the a sub-directory of the path received by dst.display().

replacing

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

with

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

solved my problem. Thanks everyone for the help.

What is realm_rs?

The name of the lib I was compiling

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.