I'm digging through some files from Mesa 3-D graphics library
and came across this code:
gl.h
//..
#if defined(__WIN32__) && !defined(__CYGWIN__)
# if (defined(_MSC_VER) || defined(__MINGW32__)) && defined(BUILD_GL32) /* tag specify we're building mesa as a DLL */
# define GLAPI __declspec(dllexport)
# elif (defined(_MSC_VER) || defined(__MINGW32__)) && defined(_DLL) /* tag specifying we're building for DLL runtime support */
# define GLAPI __declspec(dllimport)
//..
My question is what function does it have __declspec(dllexport)
and __declspec(dllimport)
when should they be used?
These attributes are Windows specific and are used to build/link DLLs.
__declspec(dllexport)
tells the linker of the library in question that this element (function, class, etc.) must be visible outside the DLL. Without this attribute the method would only be visible inside the library.__declspec(dllimport)
It comes to complement the previous clause and allows the linker (this time the one of the application or library that links with the previous DLL), to carry out the opportune operations to correctly locate the entry point of said function.In the case of Linux, it would only be necessary to indicate the export of symbols through the attribute
__attribute__(visibility("default"))
. For symbol import there is no special attribute.The most common is to control the inclusion of one tag or another through precompiler directives, so that the same header can be used both to compile the library and to link to its functions:
With the example above, it is enough that when compiling the DLL the symbol ISDLL is defined so that the function is compiled with the export option
__declspec(dllexport)
. Otherwise it will be compiled with the import option__declspec(dllimport)
.