I have some quick questions on windows dll.
Basically I am using the ifdefs to handle the dllexport and dllimport, my question is actually regarding the placement of the dllexports and dllimports as well as extern keyword.
I am putting the dllimports/dllexports on the header files but do I have to put the dllexport and dllimports on the actualy definition?
What about for typedefs?
Do I put the dllimport/dllexport in front? as in
dllexport typedef map<string, int> st_map
Also regarding the extern keyword I have seen it being used like this:
extern "C" {
dllexport void func1();
}
I have also seen it being used like this:
extern dllexport func1();
One includes the "C" and the other does not, my question is what is the difference and do I need to use it? If I do then do I use it for both dllexport and dllimport also do I have to use it on both the header file declarations and the definitions?
My project is going to be shared library, it contains several class files which I want to export, some typdefs i want to export and some global functions which I also want to export all into a dll.
Anyone enlighten me please?
EDIT:
Okay I thought I'll post a small extract of what i've done, also notice that I am building the library for both linux and windows so I do a check for that:
mydll.h
#ifdef WINDOWS
# ifdef PSTRUCT_EXPORT
# define WINLIB __declspec(dllexport)
# else
# define WINLIB __declspec(dllimport)
# endif
#else
# define WINLIB
#endif
WINLIB void funct1();
Now in the source code:
mydll.cpp
#define PSTRUCT_EXPORT
void funct1() <---- do I need to add WINLIB in front of it?
Or is doing it in the header enough?
When you link to a dll, every function exported by that dll is made available to your dll, but in order to link to a dll you generally need a . h file that gives you the declarations, and a . lib file that provides the stub pointers for the exported functions. These two things together constitute the dll import.
Use __declspec(dllexport) to export the function or method. You can export an entire C++ class by placing the __declspec(dllexport) before the class name, or you can export a single method by placing __declspec(dllexport) before the method name. Make class methods static and public.
First, you don't need to import or export typedefs. As long as they're in the header files that both sides use, you're good. You do need to import/export functions and class definitions.
Presumably you use the same header files for both the importing and exporting code, so you could do some makefile magic to define a preprocessor macro on each side, then do something like this:
#if defined( LIBRARY_CODE )
#define MYAPI __declspec(dllexport)
#else
#define MYAPI __declspec(dllimport)
#endif
extern MYAPI void func1();
class MYAPI MyClass {
...
};
Regarding C vs. C++ functions, you can do this:
#if defined( __cplusplus__ ) // always defined by C++ compilers, never by C
#define _croutine "C"
#else
#define _croutine
#endif
extern _croutine void function_with_c_linkage();
Make sure you import this header file from your C++ source file (containing the implementation of this function) or the compiler won't know to give it C linkage.
Have a look at different OpenSource libs. There you'll find plenty of examples on how making a good library header. There could be issues with name decoration in the case of C++ without the extern "C".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With