zdf
zdf

Reputation: 4808

VisualC – DLL Class Implemented in Header not Exported

  1. Create a “Dynamic Linked Library with exports” project (Test.vcxproj).
  2. Replace the predefined class with the class below.
  3. Delete the implementation of predefined classes and #include "Test.h".
  4. Build.
  5. Drag the DLL in Dependency Walker -> Nothing is exported.
  6. The class is exported if you use the header, or implement the destructor, in a CPP file.

I have noticed this behavior a long time ago but I forgot about it. Today I had a hard time trying to understand, again, what it is happening.

I do not see why this is normal since the header is part of the project.

Thank you.

class TEST_API CTest 
{
public:
  virtual ~CTest() = 0 {}
};

Upvotes: 0

Views: 51

Answers (1)

Quentin
Quentin

Reputation: 63154

Referencing the header in the project file is a red herring. You can, indeed, exclude it from the base working example and everything still works. The actual files being processed are the .cpp files.

DLL imports and exports are generated by the compiler (and later used by the linker) when encountering __declspec(dllimport) and __declspec(dllexport) attributes, as used in your class. However, since no .cpp file includes your header, the compiler never encounters your class at all. Hence, no export.

Note that, even if your class ends up in a compiled file and the exports appear, your destructor is implicitly inline and thus a user of the library might (or will, I'm not 100% sure) generate and use its own definition rather than importing the one from the DLL.

Upvotes: 2

Related Questions