views:

56

answers:

1

Here's the scenario. We use Visual C++ 9. There's a C++ library intended to be used by many other libraries. Its interface is in a header:

#pragma once

//CommonLibraryHeader.h
CSomeClass GetSomeClassFunction(); //is defined in some .cpp file
const CSomeClass MagicValue( 100, 200 ); //some predefined value that the previous function retuns to signify something important

now the library is built into a .dll file. The header file is published in a common location and included into several other libraries. Those libraries are built into their .dll files.

The net result is the following. Since every .dll is a separate executable module it has to preserve all those constants. Each "constant" is in fact an instance of a class with a non-trivial constructor and destructor. Now every .dll has a copy of MagicValue and code for construction and destruction of the variable is injected into every .dll file which adds to the load time and inflates the .dll files considerably if the same happens for many constants.

The possible solution is to mark the constant extern and move the definition into a .cpp file. But then the constant values passed into the constructor are not immediately visible to the human reader of the header file. One could add a comment there about what the values are but as usual now we would heve to keep the comment in sync with the actual code.

Is there any better solution - without moving the constants from the header and without injecting the construction/destruction code into every .dll file?

+2  A: 

You could use the trick that is often used with DLL's that export classes and functions: use a #define that is defined only when the DLL you want to include the code in is built, but not defined in the others, and do an #if that either calls the constructor or defines it as extern, as appropriate.

sje397
Will work, but the header will become a mess.
sharptooth