Abhishek Jain
Abhishek Jain

Reputation: 10584

Executable exporting symbols BUT not getting exported as they are unreferenced by the executable itself

I have a particular use case where executable needs to export certain symbols, which are imported and used by the dynamically loaded DLLs the executable loads on runtime.

The executable links with some static libraries, which actually have symbols that are exported while the DLLs use these static libraries headers to import those symbols.

If these symbols are not used or un-referenced in the executable, then the linker removes them and hence they do not get exported and hence not available for DLLs at load time.

This i solved on GCC / clang using --whole-archive and -force_load option respectively.

What about MSVC on windows? I use __declspec(dllexport) and __declspec(dllimport) for exporting and importing symbols on windows.

EDIT: For code reference, you can find the code here: https://github.com/hunkabhicupid/exeexport

The issue is something similar to these posts 1, 2 BUT the answers to these posts did not help me find a solution or i did not find them useful.

Upvotes: 2

Views: 1923

Answers (2)

Abhishek Jain
Abhishek Jain

Reputation: 10584

Problem: On windows, STATIC LIB which contains an OBJ file that has a function marked __decl-spec(dll¬export) but if the same is not used in the EXE, function does not get exported from the EXE. On other platforms also we have the same problem BUT there we have compiler options like --whole-archive / -force_load, do make it work.

Links: Link1 Link2

Only solution that come to my mind is to not create STATIC libs, rather include all code (static LIBS) in the executable then: 1. It works on Windows 2. It works on Linux without --whole-archive 3. It works on Mac OS X without -force_load 4. We also need not worry about if 2 & 3 include the dead code, exe bloat etc.

This is the only solution till the linkers become smart and throw out every unused symbol, except those marked specifically for external consumption i.e. marked to be exported.

Upvotes: 6

Ofek Shilon
Ofek Shilon

Reputation: 16147

Does dumpbin /exports {dll} show you the exports properly? Perhaps you should try dumpbin /exports {import lib}?

Based on the info so far I'm guessing the problem is not that the symbols are not exported but rather one of build order. If you get 'unresolved externals' when linking the dll, it seems you expect the exe-exported symbols to be resolved by the linker when linking the dll, but the exe is not built yet. (you probably wired it to reference the dll, so it builds only after the dll is linked).

One way to go about it is to have the dll LoadLibrary the exe and GetProcAddress the functions you want - but that really is a contrived way to achieve what you're after. If these symbols are defined in a static library, why not have both the exe and the dll link against it?

Upvotes: 1

Related Questions