hanra
hanra

Reputation: 21

Can we export a function made available through a static library

I have a static library say "A.lib" which contains a function int foo(). I have another dll say "B.dll" which consumes A.lib and uses the function foo() and also exports some other functions. Is it possible to export the function int foo() (imported from A.lib) from B.dll so that it can be consumed in a third dll say "C.dll".

I want to know whether it is possible or not, I dont want workarounds like making A.lib available to the C.dll. Also, I am not concerned if this is a bad design or not.

Thanks very much for your patience to read this through.

Upvotes: 2

Views: 2653

Answers (2)

Marc Kyriaco
Marc Kyriaco

Reputation: 11

I had the same requirement - just found a different solution:

Assuming that A.lib has an A.h (that is consumed by source files used to build B.dll e.g. assuming that A.h contains prototypes for functions contained in A.lib), just add the following in A.h:

#pragma comment(linker, "/export:_foo")

This will instruct the linker to export foo() when building B.dll. Note the leading underscore - it is there because that's the true name of the symbol for foo() contained in A.lib (use dumpbin /symbols A.lib | findstr foo to see it). In my example foo() was using the __cdecl calling convention, but if you use __stdcall() or compile as C++, you'll get different name decoration, so you'll have to adjust the #pragma statement above as a result.

It doesn't matter if A.h gets included by many source files in B.dll - the linker doesn't complain if the exact same definition is made multiple times.

One "advantage" to this approach is that you don't even have to use the __declspec(dllexport) specifier on foo() in A.lib ...

Upvotes: 1

Adriano Repetti
Adriano Repetti

Reputation: 67090

Yes, it's possible but any code example is language dependent. (for example in C you may simply export a function with the same name and C.dll will see it)

Upvotes: 0

Related Questions