twistedfall / opencv-rust

Rust bindings for OpenCV 3 & 4
MIT License
1.86k stars 144 forks source link

Create code working for multiple platforms and versions #540

Closed mdenty closed 2 months ago

mdenty commented 5 months ago
  1. Operating system I develop on macOS Sonoma and I build on Linux Ubuntu 22.04 (at the time of writing)
  2. The way you installed OpenCV:
    • macOS : brew
    • Linux : apt
  3. OpenCV version
    • macOS: 4.8.1_5
    • Linux: 4.5.4
  4. rustc version (rustc --version)
    • macOS: 1.74.0
    • Linux: 1.70

The issue is that I code:

        let sift = <SIFT>::create(
            params.nfeatures,
            params.noctave_layers,
            params.contrast_threshold,
            params.edge_threshold,
            params.sigma,
            params.enable_precise_upscale,
        )
        .expect("SIFT");

It compiles fine on the Mac but fails on the Linux box with the error message:

error[E0061]: this function takes 5 arguments but 6 arguments were supplied

The argument enable_precise_upscale appeared in version 4.8.0.

What is the best way to cope with this problem? I do not want to do a conditional compilation OS kind, since it will probably fail when 24.04 will be out.

For today, I did what I did not want to do:

        let sift = <SIFT>::create(
            params.nfeatures,
            params.noctave_layers,
            params.contrast_threshold,
            params.edge_threshold,
            params.sigma,
            #[cfg(target_os = "macos")] 
            params.enable_precise_upscale,
        )
        .expect("SIFT");
twistedfall commented 5 months ago

For this particular case I think you can use SIFT::create_def_1 (automatic naming sorry ^^”). The *_def functions are the variants of regular functions but without the arguments that have a default value in C++. In general case you can also check opencv_branch_34 and friends, but that’s for major OpenCV branches.

mdenty commented 5 months ago

Good to know about *_def functions. I'll keep that in mind.

mdenty commented 5 months ago

For some reason the SIFT::create_def_1 is not found under Linux:

error[E0599]: no function or associated item named `create_def_1` found for struct `SIFT` in the current scope
   --> src/some/dir/model.rs:390:28
    |
390 |         let sift = <SIFT>::create_def_1(
    |                            ^^^^^^^^^^^^
    |                            |
    |                            function or associated item not found in `SIFT`
    |                            help: there is an associated function with a similar name: `create_def`
For more information about this error, try `rustc --explain E0599`.
error: could not compile `project` (lib) due to previous error

It compiles fine under macOS, tho.

Back to square # 1.

twistedfall commented 3 months ago

This happens because of automatic renaming and the way how those functions are defined between those OpenCV versions. I'll try to fix this particular issue in the next create release.

twistedfall commented 2 months ago

After some tries it looks like it won't really work unfortunately. The most robust solution in your case is to have matching OpenCV versions between the environments.