www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - DLLs and friends

reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--f46d0407166d3b61a004cf2a1c65
Content-Type: text/plain; charset=UTF-8

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.

--f46d0407166d3b61a004cf2a1c65
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Can someone please explain to me what &quot;_declspec(dllimport)&quot;, &qu=
ot;__declspec(dllexport)&quot; (both of which are just &quot;export&quot; i=
n D) are for, what the &quot;.def&quot; files (which are supposed to be if =
the __declspec(whatever) are missing) are for, what is an import library an=
d how this all makes sense.<div>
<br></div><div>I googled for it, but no clear answer.</div><div>On an examp=
le of, let&#39;s say, OpenGL32.dll, the source and object files of which ar=
e unavailable, what options there are to use it besides manually GetProcAdd=
ress-ing every function?</div>
<div><br></div><div>This is a question from someone, who has never used DLL=
s other then with GetProcAddress.</div><div><br></div><div>This is importan=
t for the OpenGL bindings I&#39;m making and I want to preserve the headers=
 exactly as they are. But they contain __declspec(dllexport), which I need =
to understand. That&#39;s why this forum post exists.</div>
<div><br></div><div>Please help!<br clear=3D"all"><div><br></div>-- <br>Bye=
,<br>Gor Gyolchanyan.<br>
</div>

--f46d0407166d3b61a004cf2a1c65--
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
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 next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--90e6ba308eba62fc7404cf437690
Content-Type: text/plain; charset=UTF-8

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. --90e6ba308eba62fc7404cf437690 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable thanks!<div class=3D"gmail_extra"><br><br><div class=3D"gmail_quote">On Sun= , Nov 25, 2012 at 12:08 AM, Ellery Newcomer <span dir=3D"ltr">&lt;<a href= =3D"mailto:ellery-newcomer utulsa.edu" target=3D"_blank">ellery-newcomer ut= ulsa.edu</a>&gt;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex">__declspec(dllimport): This symbol exists so= mewhere else. I [the linker] am going to try to pull it in from some librar= y at [runtime? link time? I [ellery] don&#39;t remember]<br> <br> __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 us= e it.<br> <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0C =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0D<br> __declspec(dllimport) &lt;-&gt; export [apparently]<br> __declspec(dllexport) &lt;-&gt; extern export **<br> <br> <br> ** with dmd on linux, apparently __declspec(dllexport) &lt;-&gt; [nothing].= Noticed this when trying to link to the python c api. But it could just be= python isn&#39;t using dllexport.<br> <br> <br> <br> <br> </blockquote></div><br><br clear=3D"all"><div><br></div>-- <br>Bye,<br>Gor = Gyolchanyan.<br> </div> --90e6ba308eba62fc7404cf437690--
Nov 24 2012
prev sibling next sibling parent "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
prev sibling next sibling parent 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
prev sibling next 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