Which of the two solutions shown in the example below is the proper way to export constants from my API (a windows DLL) and why is it the superior alternative?
Header file
namespace ExampleAPI
{
// Solution one
extern const DWORD __declspec(dllexport) AKTION_OK;
extern const DWORD __declspec(dllexport) AKTION_FEHLER;
// Solution two
const DWORD AKTION_FEHLER_DATENBANK = 2;
const DWORD AKTION_FEHLER_XXX = 3;
}
Cpp file
namespace ExampleAPI
{
// Solution one
const DWORD AKTION_OK = 0;
const DWORD AKTION_FEHLER = 1;
}
I think solution one is the better alternativ, because the constants are defined only once in the cpp file and not in every link unit that is including the header file. Correct me if I’m wrong. Although it lacks readability…
Using the second solution, the compiler will know the constants, when compiling the application USING the API. This could allow the compiler to perform more optimizations.
The first solution has the advantage that you can change the constants without recompiling the application using the API.