Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zaq7434 opened this issue Oct 16, 2024 · 5 comments
Open

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

zaq7434 opened this issue Oct 16, 2024 · 5 comments
Labels
question Usage question

Comments

@zaq7434
Copy link

zaq7434 commented Oct 16, 2024

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
Copy link
Owner

asomers commented Oct 16, 2024

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 asomers added the question Usage question label Oct 16, 2024
@zaq7434
Copy link
Author

zaq7434 commented Oct 16, 2024

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
Copy link
Owner

asomers commented Oct 16, 2024

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

@zaq7434
Copy link
Author

zaq7434 commented Oct 17, 2024

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
Copy link
Owner

asomers commented Oct 17, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Usage question
Projects
None yet
Development

No branches or pull requests

2 participants