www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - DLLs and friends

reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
Can someone please explain to me what "_declspec(dllimport)",
"__declspec(dllexport)" (both of which are just "export" in D) are for,
what the ".def" files (which are supposed to be if the __declspec(whatever)
are missing) are for, what is an import library and how this all makes
sense.

I googled for it, but no clear answer.
On an example of, let's say, OpenGL32.dll, the source and object files of
which are unavailable, what options there are to use it besides manually
GetProcAddress-ing every function?

This is a question from someone, who has never used DLLs other then with
GetProcAddress.

This is important for the OpenGL bindings I'm making and I want to preserve
the headers exactly as they are. But they contain __declspec(dllexport),
which I need to understand. That's why this forum post exists.

Please help!

-- 
Bye,
Gor Gyolchanyan.
Nov 23 2012
next sibling parent reply "Ellery Newcomer" <ellery-newcomer utulsa.edu> writes:
__declspec(dllimport): This symbol exists somewhere else. I [the 
linker] am going to try to pull it in from some library at 
[runtime? link time? I [ellery] don't remember]

__declspec(dllexport): This symbol exists in this image. I [the 
linker] am going to make it available to other dlls and 
executables if they want to use it.

            C                    D
__declspec(dllimport) <-> export [apparently]
__declspec(dllexport) <-> extern export **


** with dmd on linux, apparently __declspec(dllexport) <-> 
[nothing]. Noticed this when trying to link to the python c api. 
But it could just be python isn't using dllexport.
Nov 24 2012
next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
thanks!


On Sun, Nov 25, 2012 at 12:08 AM, Ellery Newcomer <
ellery-newcomer utulsa.edu> wrote:

 __declspec(dllimport): This symbol exists somewhere else. I [the linker]
 am going to try to pull it in from some library at [runtime? link time? I
 [ellery] don't remember]

 __declspec(dllexport): This symbol exists in this image. I [the linker] am
 going to make it available to other dlls and executables if they want to
 use it.

            C                    D
 __declspec(dllimport) <-> export [apparently]
 __declspec(dllexport) <-> extern export **


 ** with dmd on linux, apparently __declspec(dllexport) <-> [nothing].
 Noticed this when trying to link to the python c api. But it could just be
 python isn't using dllexport.
-- Bye, Gor Gyolchanyan.
Nov 24 2012
prev sibling next sibling parent reply "David Nadlinger" <see klickverbot.at> writes:
On Saturday, 24 November 2012 at 20:08:10 UTC, Ellery Newcomer 
wrote:
 with dmd on linux, apparently __declspec(dllexport) <-> 
 [nothing]. Noticed this when trying to link to the python c 
 api. But it could just be python isn't using dllexport.
There is no such thing as dllimport/dllexport on Linux, everything is "public" by default. See http://gcc.gnu.org/wiki/Visibility for a description of a mechanism for hiding symbols on Linux/GCC. David
Nov 24 2012
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, November 24, 2012 21:45:51 David Nadlinger wrote:
 On Saturday, 24 November 2012 at 20:08:10 UTC, Ellery Newcomer
 
 wrote:
 with dmd on linux, apparently __declspec(dllexport) <->
 [nothing]. Noticed this when trying to link to the python c
 api. But it could just be python isn't using dllexport.
There is no such thing as dllimport/dllexport on Linux, everything is "public" by default. See http://gcc.gnu.org/wiki/Visibility for a description of a mechanism for hiding symbols on Linux/GCC.
And thank goodness for that. The existence of export is my number one complaint about Windows programming. I am so sick and tired of running into compilation issues due to it that it's not even funny. Linux's shared library model is _so_ much better than Windows' model, and that's only one of the reasons why. - Jonathan M Davis
Nov 24 2012
parent Paulo Pinto <pjmlp progtools.org> writes:
Am 24.11.2012 22:20, schrieb Jonathan M Davis:
 On Saturday, November 24, 2012 21:45:51 David Nadlinger wrote:
 On Saturday, 24 November 2012 at 20:08:10 UTC, Ellery Newcomer

 wrote:
 with dmd on linux, apparently __declspec(dllexport) <->
 [nothing]. Noticed this when trying to link to the python c
 api. But it could just be python isn't using dllexport.
There is no such thing as dllimport/dllexport on Linux, everything is "public" by default. See http://gcc.gnu.org/wiki/Visibility for a description of a mechanism for hiding symbols on Linux/GCC.
And thank goodness for that. The existence of export is my number one complaint about Windows programming. I am so sick and tired of running into compilation issues due to it that it's not even funny. Linux's shared library model is _so_ much better than Windows' model, and that's only one of the reasons why. - Jonathan M Davis
Funny I prefer the private by default way of Windows, and Aix may I add. But I started developing for Windows 3.1 back in the day, while watching some friends do Amiga 500 coding, and only later on discovered the ways of UNIX. -- Paulo
Nov 24 2012
prev sibling parent "Ellery Newcomer" <ellery-newcomer utulsa.edu> writes:
On Saturday, 24 November 2012 at 20:08:10 UTC, Ellery Newcomer 
wrote:
            C                    D
 __declspec(dllimport) <-> export __declspec(dllexport) <-> 
 extern export
I might have these backwards if you're implementing your own library.
Nov 24 2012
prev sibling parent "Daniel Murphy" <yebblies nospamgmail.com> writes:
"Gor Gyolchanyan" <gor.f.gyolchanyan gmail.com> wrote in message 
news:mailman.2175.1353679685.5162.digitalmars-d puremagic.com...
 Can someone please explain to me what "_declspec(dllimport)",
 "__declspec(dllexport)" (both of which are just "export" in D) are for,
 what the ".def" files (which are supposed to be if the 
 __declspec(whatever)
 are missing) are for, what is an import library and how this all makes
 sense.
When linking an application, the linker creates an import table which contains the dllname+functionname of each imported function. Dlls contain an export table giving the address for each exported function. At load time, the loader walks the import table of each module (dlls generally have import tables too) and searches for the appropriate function address, which it then writes into the import table. The other thing you need is a import library file - these contain a set of mappings from the function's mangled name to the function's exported name. This is needed because the 'export name' of the function is not usually the same as the mangled name, at least not for windows api functions. Exported functions don't actually need to be named, each has a unique number you can import it with. .def files are essentially a human readable version of the import library file. eg. App uses CloseHandle Linker sees symbol _CloseHandle 4 Finds import definition in kernel32.lib: _CloseHandle 4 -> kernel32.dll:CloseHandle Adds that entry to the import table Loader maps in kernel32.dll and resolves the address. Using LoadLibrary/GetProcAddress is very similar, but happens later in the process: App is loaded: LoadLibrary maps the dll into the current process (or increases the reference count if it's already loaded) GetProcAddress walks the export table looking for the function.
Nov 24 2012