I have a Main executable that loads .dll
/.so
Plugins, which works just fine in Linux but on windows(Visual Studio 2012), it fails with undefined reference errors.
The plugin uses functions like session->SendLine("bla")
which are defined in the Main executable. (class of session ans methods defined in a .h included in the plugin, but the actual function in a .cpp compiled in main exec).
tl;dr: "I need the windows linker to ignore undefined references in plugins, defined in the main executable"
What is the best way to "make it work" in windows but keep it compatible with Linux without a million #ifdef
's?
Linking of libraries on Windows is handled completely differently from how it is handled on Linux. Linking from plugin to host executable is simple on Linux, but not so much on Windows.
On Windows the traditional way to link to an external module is to use an import library, provided by a .lib file. In order to do it that way, you would need to create an import library for your executable file which includes all the exported functions that your plugins need to call. I've never created an import library for an executable. Normally you do it for a DLL. I'm not even sure it will work for an executable.
Some other options:
GetProcAddress
in your plugin to bind to them at runtime.When using mingw, this can be done by generating an import library for the executable as follows:
$ dlltool --export-all-symbols <program>.exe -l lib<program>.a -D <program>.exe
The -l
argument specifies the filename of the library to be created, and the -D
argument specifies the dllname of the library (and it is important that this is equal to the program name). To compile the dll, you will then need to link against the import library by adding -l<program>
to the linker flags.
If you want to limit the exported symbols, you can first generate a defs file, edit it, and then generate the import library from the defs file:
$ dlltool --export-all-symbols <program>.exe -z <program>.defs
$ vi <program>.defs # Edit the list of exported symbols
$ dlltool -d <program>.defs -l lib<program>.a -D <program>.exe
Note: The name of dlltool may vary depending on the mingw environment (i.e. i686-w64-mingw32-dlltool
on Fedora for cross-compiling to i686 windows).
To call functions defined in an executable from DLL, you have to export those functions from an executable using __declspec(dllexport), just as you export functions from DLL.
The compiler will generate an import library for the executable that includes stubs for the exported functions.
Link with this import library when you build your DLL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With