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

undefined reference if only mock the inner most functions #494

Open
aiChaoSONG opened this issue Jan 17, 2025 · 3 comments
Open

undefined reference if only mock the inner most functions #494

aiChaoSONG opened this issue Jan 17, 2025 · 3 comments

Comments

@aiChaoSONG
Copy link

I am using create_makefile.rb to generate Makefile for me. the file tree loos like this

.
├── a.c    -> runController() in this file, which calls rreg() and wreg()
├── a.h
├── b.c    -> init() in this file, which calls runController() 
├── b.h
├── io.c   -> rreg() and wreg() in this file
└── io.h

I have test_b.c to test init() function. Is it possible to only mock the inner most rreg() and wreg() function? So that I can do

rreg()
wreg()

TEST_ASSERT_EQUAL(expectedReturn, init())

In my test, I got undefined reference to runController() function, because for the test_b binary, a.o is not added as dependency in the generated Makefile, although I have included a.h both in b.c and test_b.c, and mock_io.h in test_b.c

@aiChaoSONG aiChaoSONG changed the title undefined reference if only mock the inner most functions included undefined reference if only mock the inner most functions Jan 17, 2025
@mvandervoord
Copy link
Member

Yes, this is an easy thing to do. In your test, #include the files you want the REAL version of (in this case a.h and b.h). When you compile and link, make sure you're including a.c and b.c. You can also include mock_io.h then compile and link mock_io.c. At that point you should have all the real and mocked sections you desire. :)

@aiChaoSONG
Copy link
Author

aiChaoSONG commented Jan 17, 2025

@mvandervoord Thank you very much.

In your test, #include the files you want the REAL version of (in this case a.h and b.h).

They are included for sure

When you compile and link, make sure you're including a.c and b.c.

What can I do to include a.c? I am using create_makefile.rb to generate MakefileSupport, in the generated MakefileSupport, there are rules for a.o, b.o and test_b.o. the issue is that a.o is not added as a dependency for test_b binary, and thus I got undefined reference for the runController() function. Is there any way I can override the rules in MakefileSupport, or any option I can tune to add a.o to test_b's dependency list?

test_b: test_b.o runner_test_b.o b.o mock_io.o unity.o cmock.o

By the way, I am using CMock 2.5.3. Maybe it is too old?

@mvandervoord
Copy link
Member

Hm. I'm not 100% sure how it's done with create_makefile. I've never used that script myself. I believe it uses the same general method that Ceedling uses: The dependencies are listed in the test file itself... so if there's an include for the header, the corresponding C file is sought out.

You've regenerated your makefile since you edited the test to include other headers?

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

No branches or pull requests

2 participants