3

I have a quite large C application consisting of several shared libraries. One of the core libraries has a function

void common_function(const char * arg) { ... }

Which is called by all the other libraries. During testing I would like to use a different test implementation of the common_function.

My plan has been to create a test library containing the alternate implementation of common_function; is it at all possible to replace the default common_function runtime using dlopen() / dlsym() trickery, or alternatively would this link line:

gcc -o test.c -ltest -lcommon

ensure that the common_function implementation in libtest.so was used also in the libcommon.so - altough the latter has it's own implementation of common_function.

user422005
  • 1,989
  • 16
  • 34

2 Answers2

1

Read Drepper's paper: How To Write Shared Libraries and wikipage on dynamic linker.

You probably want to play the LD_PRELOAD trick (assuming all your libraries are shared, not static).

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

If you write an implementation of common_function in test.c, the linker will use that and therefore the loader won't even try to load it from a library (static or dynamic).

What you want is a mock (that page example uses Python to illustrate the concept but it holds for any language).

My personal favourite mocking framework for C is FFF (here's an example of how to use it). There's also C-Mock.

Leonardo
  • 1,533
  • 17
  • 28