twistedfall / opencv-rust

Rust bindings for OpenCV 3 & 4
MIT License
1.99k stars 160 forks source link

How to statically linking OpenCV with CUDA and use dynamic linking for CUDA #596

Open ImmortalD opened 3 months ago

ImmortalD commented 3 months ago

I used vcpkg to install OpenCV with CUDA support. I want to statically linking the OpenCV library while using dynamic linking for the CUDA libraries. However, I encountered a problem where the CUDA libraries are also statically linked to the program. I am not sure how to configure it correctly.

install opencv with vcpkg

vcpkg install opencv4[contrib,nonfree,cuda]:x64-windows-static-md

rust config

./.cargo/config.toml

[env]
VCPKGRS_TRIPLET = "x64-windows-static-md"

build.rs


fn main() {

    let vcpkgrs_triplet = std::env::var("VCPKGRS_TRIPLET");

    match vcpkgrs_triplet {
        Ok(triplet) => {
            if triplet == "x64-windows-static-md" {
                let cuda_path = std::env::var("CUDA_PATH").expect("CUDA_PATH not set");
                println!("cargo:rustc-link-search=native={}\\lib\\x64", cuda_path);
                println!("cargo:rustc-link-lib=dylib=nppc");
                println!("cargo:rustc-link-lib=dylib=nppial");
                println!("cargo:rustc-link-lib=dylib=nppicc");
                println!("cargo:rustc-link-lib=dylib=nppidei");
                println!("cargo:rustc-link-lib=dylib=nppif");
                println!("cargo:rustc-link-lib=dylib=nppig");
                println!("cargo:rustc-link-lib=dylib=nppim");
                println!("cargo:rustc-link-lib=dylib=nppist");
                println!("cargo:rustc-link-lib=dylib=nppisu");
                println!("cargo:rustc-link-lib=dylib=nppitc");
                println!("cargo:rustc-link-lib=dylib=npps");
                println!("cargo:rustc-link-lib=dylib=cufft");
                println!("cargo:rustc-link-lib=dylib=cublas");

                println!("cargo:rustc-link-lib=dylib=cudart");
            }
        }
        Err(_) => {}
    }
}
twistedfall commented 2 months ago

Can you please post the full output of cargo build -vv after doing a cargo clean? I'd like to see what are the actual link flags are passed