mockall icon indicating copy to clipboard operation
mockall copied to clipboard

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

Open zaq7434 opened this issue 1 year ago • 5 comments

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!

zaq7434 avatar Oct 16 '24 03:10 zaq7434

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.

asomers avatar Oct 16 '24 03:10 asomers

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.

zaq7434 avatar Oct 16 '24 06:10 zaq7434

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

asomers avatar Oct 16 '24 16:10 asomers

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'

zaq7434 avatar Oct 17 '24 00:10 zaq7434

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?

asomers avatar Oct 17 '24 13:10 asomers