Reputation: 2526
First I'll show my questions and then I'll provide more background details:
I'm wokring in a Visual C++ project on Windows platform(but at the same time we need to guarantee the cross-platform capability of our source code to support Linux).
One of my colleagues was asked to design and implement some stand-alone DLL modules which could be shared by the other projects. He planned to develop two DLLs with different purposes:
His considerations of such a design include:
In fact, with some real test, we found these global objects could not be constructed as intented, which, sadly, breaks his heart ;-). But I think his design still looks good so we want to figure out some way to make it work. We'd prefer to use these DLLs in this way: The DLLs are deployed in the developers' computer with libs and includes. Then in a concrete VC++ project they are incorporated at the link-time. Right now we don't have an compile or link errors but just these global objects in DLL #2 are not created as desired.
Upvotes: 1
Views: 191
Reputation: 38218
Can't you use the DLL's DllMain()
function to initialize everything? Just check for DLL_PROCESS_ATTACH
, which will let you know if the DLL is just being loaded right now. Similarly, you can use DLL_PROCESS_DETACH
to perform any cleanup.
If you're keeping things open for Linux, you can also use GCC's __attribute__((constructor))
.
As for your questions:
main()
function. If you can guarantee they're created in DllMain()
, which will be called before any other function in the DLL (and the user doesn't call it; the OS does), do you still really need it to be before main()
? I'd be surprised.Upvotes: 1