2

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.

Community
  • 1
  • 1
Abhishek Jain
  • 9,614
  • 5
  • 26
  • 40
  • Decorating a symbol with __declspec(dllexport) in the executable forbids it from being optimized away. What exactly makes you say it isn't exported? How do you consume it from the DLLs? – Ofek Shilon Oct 07 '15 at 10:41
  • Hmm, no, that works fine on MSVC. The __declspec(dllexport) ensures that the function is exported from the EXE and that the linker generates an import library. The DLL then has to be built, declaring the exported function *without* __declspec(dllimport), just the prototype. And it has to link the EXE's import library. It is a very ugly and brittle way to do it, plugins should use interfaces. The host can expose its services by passing an interface pointer to the plugin initialization function. – Hans Passant Oct 07 '15 at 10:43
  • dumpbin /EXPORTS does not show the symbols. Even dynamic libs while loading complain of symbol not found. – Abhishek Jain Oct 07 '15 at 10:44
  • Rather while compiling dynamic libs, they complain of unresolved symbols as executable import library also does not have those symbols. – Abhishek Jain Oct 07 '15 at 10:51

2 Answers2

5

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.

Abhishek Jain
  • 9,614
  • 5
  • 26
  • 40
1

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?

Ofek Shilon
  • 14,734
  • 5
  • 67
  • 101
  • The EXE is getting built first and then the DLLs. If i use those exe exported symbols in the exe itself first, everything works fine, symbol get exported BUT if i don't, they do not get exported and hence do not even come in the exe import library and hence the DLL complain during linking with the exe import library. – Abhishek Jain Oct 07 '15 at 14:57
  • That sounds weird. Can you form a have a small code example demonstrating the issue? – Ofek Shilon Oct 08 '15 at 07:28
  • For code reference, you can find the code here: https://github.com/hunkabhicupid/exeexport I have edited my question, please checkout. – Abhishek Jain Oct 08 '15 at 07:29
  • This repository doesn't seem like an MSVC build. What is maya? can you post the sln/vcxproj files as well? – Ofek Shilon Oct 08 '15 at 07:32
  • ok, i have promoted a file out.txt in the repo. Please check that. It has the build commands issued to generate the executable and dynamic libraries. – Abhishek Jain Oct 08 '15 at 07:55
  • You have /OPT:REF at several places. Try removing the one on the executable link. – Ofek Shilon Oct 08 '15 at 08:07
  • Yes, i tried using /OPT:NOREF and also removing /OPT:REF. It did not work. – Abhishek Jain Oct 08 '15 at 08:08
  • Can you please pack the build in a vcxproj? If you do I'll try to build and inspect here – Ofek Shilon Oct 08 '15 at 08:16
  • Thanks for your help Ofek. – Abhishek Jain Oct 08 '15 at 12:45