Archived

This topic is now archived and is closed to further replies.

krez

DLL for C and/or C++

Recommended Posts

i am attempting to write a DLL for use with both C and C++. while the DLL internally uses C++, there are plenty of functions and structs to make it usable with straight C. however, i want to also expose some classes for use with C++ (using that "pure virtual struct" thing). i learned it from an article here at gamedev, and it works but, i wonder what will happen when a C user tries to use my DLL. i am planning on putting the C++-specific header stuff in #ifdefs, so the C compiler doesn't cry about the word "class", or any other C++ stuff (structs with member functions, etc). i am assuming that a C compiler wouldn't have a clue what to do with these, right? so i have to #ifdef them away? so, my main question is, can i rely on "__cplusplus" being #defined for C++ compilers? also, would i need to have a separate LIB for C, because of the C++ things in there? i am going to get this all running with C++ first (since that is what i use and therefore truly care about mainly)... but i'd like to know if i am going to face any terrible problems that i can avoid by planning ahead. thanks! [edited by - krez on October 21, 2003 1:42:52 PM]

Share this post


Link to post
Share on other sites
A DLL contains compiled code, and is thus usable with any language that supports DLL usage (given appropriate .def and .h files (or equivalent)). Part of the reason for DLLs existence is to let compiled code carrying out certain functions be accessible in a way that is allowable in many different languages.

Share this post


Link to post
Share on other sites
thanks for the heads-up. i knew that in theory, but i am very tired, and i got confused about the classes i have in the header files. upon further thought, though, i realized that i don''t export any of those from the DLL; i just export a few functions to fetch interfaces to them.

don''t mind me.

Share this post


Link to post
Share on other sites
as an almost-related follow-up stupid question:

under what circumstances is a DLL shared between programs? i cannot recall what i read about this, nor find the links i had...

basically i want to know if a "global" variable (or singleton for you OOP nazis) in the DLL will be shared between two programs using the DLL simultaneously, or if they will each get their own copy.

?

thanks!

Share this post


Link to post
Share on other sites
They each get their own copy. The dll is mapped into the process''s address space.

There''s a way to share data across processes in a dll, but then you have to worry about locks, critical sections, race conditions, and all that good stuff when you delve into that.

Share this post


Link to post
Share on other sites