Need help: multiple definition of mocked function

2,255 views
Skip to first unread message

Felipe de Andrade Neves Lavratti

unread,
Apr 6, 2015, 7:17:36 PM4/6/15
to cppu...@googlegroups.com
Hello!

I am mocking a function that is linked into the `lib_dot_stack_unit_tests.a`. Somehow I am getting the error of 'multiple definition of' the mocked function.

In file 'dot_requisition_dispatcher_test.cpp', inside a test, I call a production code function that calls 'network_init()' that is inside 'network.c', the 'network_init' function is the one I wrote a mock inside the file network_mocks.cpp.

I have two definitions of submodule_init, the original one inside production_code_lib.a, and the mocked version that is compiled into a .o object.

At the final linking I get the error:

make -f make_unit_tests.mk all
compiling dot_requisition_dispatcher_test.cpp
compiling network_frame_test.cpp
compiling main.cpp
compiling logging.c
compiling network_mocks.cpp
compiling network_frame.c
compiling dot_requisition_dispatcher.c
compiling network.c
Building archive obj_tests/lib_dot_stack_unit_tests.a
a - obj_tests/./src/network_frame.o
a - obj_tests/./src/dot_requisition_dispatcher.o
a - obj_tests/./src/network.o

Linking dot_stack_unit_tests
obj_tests/lib_dot_stack_unit_tests.a(network.o): In function `network_init':
/home/fanl/workspace-beyond/software/mesh/dot_stack/src/network.c:14: multiple definition of `network_init'

collect2: error: ld returned 1 exit status
make: *** [dot_stack_unit_tests] Error 1

Any suggestions? Thanks!


--
Skype: felipeanl

Bas Vodde

unread,
Apr 7, 2015, 1:51:23 AM4/7/15
to cppu...@googlegroups.com

Hi Felipe,

So, you have two definitions of the network_init method.

Now, the linker works so that it will pull in the whole compilation unit if you use one function in that unit. So, you are trying to stub out the network_init, but you probably still use another method from the network.o file that you do not stub. Because of that, it pulls in the whole object file and you get multiple definition failure.

You’d need to either:
- Stub the other method(s) too
- Seperate the network_init in the production code to a seperate file
- Use a different mocking technique

Hope that helps.

Thanks!

Bas

--
You received this message because you are subscribed to the Google Groups "cpputest" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cpputest+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Felipe de Andrade Neves Lavratti

unread,
Apr 7, 2015, 8:09:11 AM4/7/15
to cppu...@googlegroups.com
Well thanks Bas! Now it all makes sense for me, I didn't realise that I had to mock all public functions of the file.

A. Robert S.

unread,
Apr 13, 2015, 10:42:45 AM4/13/15
to cppu...@googlegroups.com
Hi Felipe,
 
Yeah, this one got me too.... But, as Bas pointed out, you still don't have to stub all functions in the compilation unit -- just those that are actually called from somewhere. A good "trick" is to not link the library and then pay close attention to any missing function from that compilation unit that the compiler complains about. Then write additional stubs just for those.
 
Of course, If you can foresee you are eventually going to use all of them, you might as well stub all of them. But, more often than not, you just need one or two out of a dozen.
 
Regards,
Robert
Reply all
Reply to author
Forward
0 new messages