Alex Rivera | Logout

Forcing symbol export with MSVC

Asked 2009-01-14T19:34:10.713
9

I have a application and several plugins in DLL files. The plugins use symbols from the application via a export library. The application links in several static libraries and this is where most of the symbols come from. This works fine as long as the application uses a symbol. If the symbol is not used there, I get linker errors when compiling the DLL.

How can I force the export of the symbols only used in the plugins?

In order to trigger the export I've tried something like this:

    class MyClassExporter
    {
        MyClass mInstance;
    public:
        MyClassExporter() {}
    };
    static MyClassExporter TheMyClassExporter;

in one of the static libs the application is made of to force the export, which didn't work.

In response to Greg (thanks for the answer) and to clarify: The class I want to force the export for is MyClass (which has __declspec(...) defined, depending on wether I want to export or import). MyClassExport was my attempt to force the inclusion of unused (in terms of the application) symbols into the app. I want to 'touch' the symbols so that the linker recognizes them as used and includes them into the application so that it can in turn export these to my plugins. Linking the static libs into the plugins is not an option, since they contain singletons which would be duplicated (app and DLLs each have their own copy of static variables).

Edit
Report

2 Answers

3

You probably want to look at __declspec(export/import)

#ifdef DLL_EXPORTING
#define WHDLL __declspec(dllexport)
#else
#define WHDLL __declspec(dllimport)
#endif

When linking static module into a dll it will only bring in the code that is used. I've never imported stuff from a static lib to simply re export it.
Perhaps you just need to mark it as exportable in the dll when compiling the static lib.

But that reminds me of putting std containers into exported classes and using some trickery in msvc to export the 'instance' of the specialised container. the template code is similar to your static code (in my thinking)

for instance without the template you get warnings the template code is not exported to support the class - this is MSVC specific from my understanding

template class DLL_EXPORTING std::auto_ptr<wxCursor>;
class DLL_EXPORTING imageButton : public wxWindow
{
    std::auto_ptr<wxCursor> m_Cursor;
};
answered 2009-01-15T05:17:07.510
1

There is some discussion of this problem on MSDN which was pretty useful. As it turns out, /OPT:NOREF is not particularly helpful in this case. /INCLUDE can work but it can be hard to automatically figure out what needs to be /INCLUDEd. There's unfortunately no silver bullet.

http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/2aa2e1b7-6677-4986-99cc-62f463c94ef3

answered 2010-12-15T23:24:20.790

Your Answer