asomers / mockall

A powerful mock object library for Rust
Apache License 2.0
1.5k stars 62 forks source link

can mock c function? error: multiple definition of `get_num' #612

Open zaq7434 opened 5 days ago

zaq7434 commented 5 days ago

my project with rust and c. i want use rust to rest c function. when i to make mock c function in rust, get multiple definition error!

    #[automock]
    pub mod ffi {
        extern "C" {
            pub fn get_num() -> i32;
        }
    }
int get_num(){

    return 1 + do_someting();

}

int do_someting(){
    return 1;

}

use build.rs to build c file.

when i run 'cargo test' error: multiple definition of `get_num'

Another problem is that: how to mock 'do_someting()'

I hope you can give me some suggestions. thank you very mach!

asomers commented 5 days ago

This is really a duplicate of #602 . I can easily fix it; however that will break one other user's use case. Sadly, I think I'll have to do that. But until then, the easiest way for you to fix your problem is to not link the C file into your project when you build the unit tests. Only link the C file into your real executable. That's usually a good idea anyway.

zaq7434 commented 5 days ago

This is really a duplicate of #602 . I can easily fix it; however that will break one other user's use case. Sadly, I think I'll have to do that. But until then, the easiest way for you to fix your problem is to not link the C file into your project when you build the unit tests. Only link the C file into your real executable. That's usually a good idea anyway.

If you don't link C libraries, you can, but this is to design the test framework for c, and I want to use cargo test to test rust and c together. So I'd like to be able to mock C, like do_something() in the example, which he didn't import into Rust.

asomers commented 4 days ago

In your test program, do you expect that get_num will be called by another C function, or by a Rust function?

zaq7434 commented 4 days ago

In your test program, do you expect that get_num will be called by another C function, or by a Rust function?

'get_num' will called by rust function, 'get_num' call another C function -- 'do something', i want mock 'do something'

asomers commented 3 days ago

But in the example, you showed mocking get_num, not do_something. How are you getting multiple definition errors of functions that you haven't mocked?