0

I have two static libraries (say A and B) and both of them reference an identical third party library (say C) by including the source files directly and independently. C is pretty simple and only have one .h and .c files. Executable D links with A and B.

Let's assume there is a function fn in C. When executable D resolves the function name: fn, it picks arbitrary fn implementation within either A or B.

My question is how to make sure the codes within A references the A's C implementation and B references B's C implementation?

As C is a third party library, so I'd not modify C's codes. So renaming either one of functions doesn't work. I ever consider uses namespace of C++ that wrap C's code like following

namespace WRAP
{
#include "moudule_c_source_file.c"
}

However, as module_c_souce_file.c includes module_c_source_file.h which contains extern "C" declare. This causes the generated symbol object file doesn't contains namespace information. So that executable D still links arbitrary function implementation.

Any idea of resolving my question mentioned above without touching the third party library codes?

Tinggo
  • 1,127
  • 1
  • 9
  • 18
  • You can use a bunch of `#define`s to effectively rename external names in your C library. – n. m. could be an AI Nov 08 '21 at 06:07
  • 1
    It should not matter which lib is used due to "one definition rule". Otherwise your program invokes undefined behavior – tstanisl Nov 08 '21 at 06:26
  • 1
    Are these static or dynamic libraries? On which os? – Alan Birtles Nov 08 '21 at 06:38
  • @AlanBirtles A and B are static library, C actually is one .h and one .c files and added into A and B directly. Operating System: Windows – Tinggo Nov 08 '21 at 07:09
  • If they're static libraries there isn't much you can do without modifying C – Alan Birtles Nov 08 '21 at 07:14
  • 1
    https://stackoverflow.com/questions/6940384/how-to-deal-with-symbol-collisions-between-statically-linked-libraries/6940389#6940389 https://stackoverflow.com/questions/20052228/an-objcopy-equivalent-for-windows-hack-for-clashing-lib-symbols – KamilCuk Nov 08 '21 at 15:32

2 Answers2

0

Possibility of the following idea depends on how big third-party libs are. The only idea I have is to create your own mid-layer-libraries. For example, mid-layer-library-A.dll uses A, and mid-layer-library-B.dll uses B. So, mid-layer-library-A.dll imports fn from A and exports mid_A_fn, mid-layer-library-B.dll imports fn from B and exports mid_B_fn. And executable D links to mid-layer-libraries and C. So, D has three defferent methods: mid_A_fn, mid_B_fn and fn from C. Something like design pattern 'adapter'.

nvf
  • 465
  • 1
  • 7
0

When executable D resolves the function name: fn, it picks arbitrary fn implementation within either A or B.

No, it does not. It picks the first definition available, and which one that is depends on the order of A and B on Ds link line.

how to make sure the codes within A references the A's C implementation and B references B's C implementation?

In the scenario you described, D will contain a single definition (from either A or B), so your question makes no sense.

In addition, since fn is identical, it can't matter which definition is used.

If fns are not in fact identical, you have an ODR violation and your binary has undefined behavior.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362