www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - help understanding import libraries

reply maarten van damme <maartenvd1994 gmail.com> writes:
--000e0cd32ec05a0c7e04aadd51e0
Content-Type: text/plain; charset=ISO-8859-1

I'm really getting confused as to how import libraries actually work.
Someone once told me that in order to use an import library one has to write
d files declaring the functions so that the dmd compiler knows what's in the
lib files.
I've never thought any further untill I compiled the gtkd project to a lib
file.

the compiler flags I needed to add was -I for every src directory and -L for
the lib file. The problem with that was that those files in the src dir
don't declare the functions but also define them. They are the real source
code files so I didn't understand why the -L flag was necessary. I never
experimented further though :)

Today I've looked  in the lib files and saw that phobos.lib was there. just
for fun I decided to compile a simple hello world file and compile it with
the standard compiler options. Then I compiled without linking and did the
linking manually. The resulting executable was 146 kb while the executable
using the standard compiler options was almost 2 megabytes.

Is the compiler really including all the "garbage" I use from phobos from
the source files instead of linking to the phobos.lib file? Can I avoid
this? does the -L flag pointing to my gtkd.lib file actually do something?

Secondly I've also used implib to convert a dll file to a .lib file. The
resulting lib file was WAY smaller then the dll file so I concluded that the
lib file only contains information like this:
in dll XXX
function YYY at export adress Y
function ZZZ at export adress Z
...

But where does the lib file searches for the dll file? how can we control
that? Is my hunch right?

Maarten

--000e0cd32ec05a0c7e04aadd51e0
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

I&#39;m really getting confused as to how import libraries actually work.<d=
iv>Someone once told me that in order to use an import library one has to w=
rite d files declaring the functions so that the dmd compiler knows what&#3=
9;s in the lib files.</div>
<div>I&#39;ve never thought any further untill I compiled the gtkd project =
to a lib file.</div><div><br></div><div>the compiler flags I needed to add =
was -I for every src directory and -L for the lib file. The problem with th=
at was that those files in the src dir don&#39;t declare the functions but =
also define them. They are the=A0real=A0source code files so I didn&#39;t u=
nderstand why the -L flag was necessary. I never experimented further thoug=
h :)</div>
<div><br></div><div>Today I&#39;ve looked =A0in the lib files and saw that =
phobos.lib was there. just for fun I decided to compile a simple hello worl=
d file and compile it with the standard compiler options. Then I compiled w=
ithout linking and did the linking manually. The resulting executable was 1=
46 kb while the executable using the standard compiler options was almost 2=
 megabytes.</div>
<div><br></div><div>Is the compiler really including all the &quot;garbage&=
quot; I use from phobos from the source files instead of linking to the pho=
bos.lib file? Can I avoid this? does the -L flag pointing to my gtkd.lib fi=
le actually do something?</div>
<div><br></div><div>Secondly I&#39;ve also used implib to convert a dll fil=
e to a .lib file. The resulting lib file was WAY smaller then the dll file =
so I concluded that the lib file only contains information like this:</div>
<div>in dll XXX</div><div>function=A0YYY=A0at export adress Y</div><div>fun=
ction ZZZ at export adress Z</div><div>...</div><div><br></div><div>But whe=
re does the lib file searches for the dll file? how can we control that? Is=
 my hunch right?</div>
<div><br></div><div>Maarten</div><div><br></div>

--000e0cd32ec05a0c7e04aadd51e0--
Aug 19 2011
next sibling parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
maarten van damme Wrote:

 the compiler flags I needed to add was -I for every src directory and -L for
 the lib file. The problem with that was that those files in the src dir
 don't declare the functions but also define them. They are the real source
 code files so I didn't understand why the -L flag was necessary. I never
 experimented further though :)

The process of creating an executable goes from generating machine code from source to linking the machine code for execution, as you know. The -I flag is the compiler import/include directory while -L is flags passed to the linker. That so while you may have complete source code in the import directory the compiler is only looking at the declaration and does not compile the files, it then informs the linker where the symbols can be found from the information you provide after the -L flag. If you wanted (and didn't run out of command line characters) you could list all .d files in every library you are using and have dmd compile everything for you. Then the -L would not be required.
Aug 19 2011
parent Jesse Phillips <jessekphillips+D gmail.com> writes:
Graham Fawcett Wrote:

 On Fri, 19 Aug 2011 12:38:01 -0400, Jesse Phillips wrote:
 
 If you wanted (and didn't run out of command line characters) you could
 list all .d files in every library you are using and have dmd compile
 everything for you. Then the -L would not be required.

Just a reminder, you have "dmd cmdfile" for cases where you want to put a "long" command line's options into a file for easier management. Graham

Yes but that just adds an extra lesson on top of learning what -I and -L do, why they are different and why both are used together.
Aug 19 2011
prev sibling next sibling parent Graham Fawcett <fawcett uwindsor.ca> writes:
On Fri, 19 Aug 2011 12:38:01 -0400, Jesse Phillips wrote:

 If you wanted (and didn't run out of command line characters) you could
 list all .d files in every library you are using and have dmd compile
 everything for you. Then the -L would not be required.

Just a reminder, you have "dmd cmdfile" for cases where you want to put a "long" command line's options into a file for easier management. Graham
Aug 19 2011
prev sibling next sibling parent Graham Fawcett <fawcett uwindsor.ca> writes:
On Fri, 19 Aug 2011 12:38:01 -0400, Jesse Phillips wrote:

 If you wanted (and didn't run out of command line characters) you could
 list all .d files in every library you are using and have dmd compile
 everything for you. Then the -L would not be required.

Just a reminder, you have "dmd cmdfile" for cases where you want to put a "long" command line's options into a file for easier management. Graham
Aug 19 2011
prev sibling next sibling parent Graham Fawcett <fawcett uwindsor.ca> writes:
On Fri, 19 Aug 2011 12:38:01 -0400, Jesse Phillips wrote:

 If you wanted (and didn't run out of command line characters) you could
 list all .d files in every library you are using and have dmd compile
 everything for you. Then the -L would not be required.

Just a reminder, you have "dmd cmdfile" for cases where you want to put a "long" command line's options into a file for easier management. Graham
Aug 19 2011
prev sibling next sibling parent Graham Fawcett <fawcett uwindsor.ca> writes:
On Fri, 19 Aug 2011 12:38:01 -0400, Jesse Phillips wrote:

 If you wanted (and didn't run out of command line characters) you could
 list all .d files in every library you are using and have dmd compile
 everything for you. Then the -L would not be required.

Just a reminder, you have "dmd cmdfile" for cases where you want to put a "long" command line's options into a file for easier management. Graham
Aug 19 2011
prev sibling next sibling parent Graham Fawcett <fawcett uwindsor.ca> writes:
On Fri, 19 Aug 2011 12:38:01 -0400, Jesse Phillips wrote:

 If you wanted (and didn't run out of command line characters) you could
 list all .d files in every library you are using and have dmd compile
 everything for you. Then the -L would not be required.

Just a reminder, you have "dmd cmdfile" for cases where you want to put a "long" command line's options into a file for easier management. Graham
Aug 19 2011
prev sibling next sibling parent Graham Fawcett <fawcett uwindsor.ca> writes:
On Fri, 19 Aug 2011 17:35:00 +0000, Graham Fawcett wrote:

 On Fri, 19 Aug 2011 12:38:01 -0400, Jesse Phillips wrote:
 
 If you wanted (and didn't run out of command line characters) you could
 list all .d files in every library you are using and have dmd compile
 everything for you. Then the -L would not be required.

Just a reminder, you have "dmd cmdfile" for cases where you want to put a "long" command line's options into a file for easier management. Graham

Sorry for the duplicate posts, I have no idea why that happened. Graham
Aug 19 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--000e0cd1471273c6fe04aae02562
Content-Type: text/plain; charset=ISO-8859-1

I saw the same with another user using the web interface, seems like it's
buggy

2011/8/19 Graham Fawcett <fawcett uwindsor.ca>

 On Fri, 19 Aug 2011 17:35:00 +0000, Graham Fawcett wrote:

 On Fri, 19 Aug 2011 12:38:01 -0400, Jesse Phillips wrote:

 If you wanted (and didn't run out of command line characters) you could
 list all .d files in every library you are using and have dmd compile
 everything for you. Then the -L would not be required.

Just a reminder, you have "dmd cmdfile" for cases where you want to put a "long" command line's options into a file for easier management. Graham

Sorry for the duplicate posts, I have no idea why that happened. Graham

--000e0cd1471273c6fe04aae02562 Content-Type: text/html; charset=ISO-8859-1 I saw the same with another user using the web interface, seems like it&#39;s buggy<br><br><div class="gmail_quote">2011/8/19 Graham Fawcett <span dir="ltr">&lt;<a href="mailto:fawcett uwindsor.ca">fawcett uwindsor.ca</a>&gt;</span><br> <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">On Fri, 19 Aug 2011 17:35:00 +0000, Graham Fawcett wrote:<br> <br> &gt; On Fri, 19 Aug 2011 12:38:01 -0400, Jesse Phillips wrote:<br> &gt;<br> &gt;&gt; If you wanted (and didn&#39;t run out of command line characters) you could<br> &gt;&gt; list all .d files in every library you are using and have dmd compile<br> &gt;&gt; everything for you. Then the -L would not be required.<br> &gt;<br> &gt; Just a reminder, you have &quot;dmd cmdfile&quot; for cases where you want to put<br> &gt; a &quot;long&quot; command line&#39;s options into a file for easier management.<br> &gt;<br> &gt; Graham<br> <br> Sorry for the duplicate posts, I have no idea why that happened.<br> <font color="#888888"><br> Graham<br> <br> </font></blockquote></div><br> --000e0cd1471273c6fe04aae02562--
Aug 19 2011
prev sibling next sibling parent reply Kagamin <spam here.lot> writes:
maarten van damme Wrote:

 But where does the lib file searches for the dll file? how can we control
 that? Is my hunch right?

The dll is searched by the system. It's a documented procedure. You can control it to some degree by manifest files.
Aug 20 2011
next sibling parent simendsjo <simendsjo gmail.com> writes:
On 20.08.2011 14:07, maarten van damme wrote:
 That explains it, thank you.
 Now only how come that
 "dmd hello.d" results in a 1.70 mb application whereas
 "dmd -c hello.d" && "dmd hello.obj phobos.lib" results in a 144 kb app
 (sorry for confusion, Turns out I didn't link my app with phobos.lib but
 recompiled the object file with phobos.lib

 2011/8/20 Kagamin <spam here.lot>

     maarten van damme Wrote:

      > But where does the lib file searches for the dll file? how can we
     control
      > that? Is my hunch right?

     The dll is searched by the system. It's a documented procedure. You
     can control it to some degree by manifest files.

I cannot reproduce this. I get the same size even if I link manually.
Aug 20 2011
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 08/20/2011 04:35 PM, Andrej Mitrovic wrote:
 On 8/20/11, maarten van damme<maartenvd1994 gmail.com>  wrote:
 Now only how come that
 "dmd hello.d" results in a 1.70 mb application whereas
 "dmd -c hello.d"&&  "dmd hello.obj phobos.lib" results in a 144 kb app

In both cases on my system it produces a 464Kb app. You must be doing something wrong. :)

Unless it is a bug of some sort. Maarten, what system are you on? Also, can you provide your hello.d? Have you tried to strip the executable?
Aug 20 2011
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 08/20/2011 06:27 PM, Timon Gehr wrote:
 On 08/20/2011 04:35 PM, Andrej Mitrovic wrote:
 On 8/20/11, maarten van damme<maartenvd1994 gmail.com> wrote:
 Now only how come that
 "dmd hello.d" results in a 1.70 mb application whereas
 "dmd -c hello.d"&& "dmd hello.obj phobos.lib" results in a 144 kb app

In both cases on my system it produces a 464Kb app. You must be doing something wrong. :)

Unless it is a bug of some sort. Maarten, what system are you on? Also, can you provide your hello.d? Have you tried to strip the executable?

should have read "have you tried stripping the executable".
Aug 20 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--000e0cd29e26ab562c04aaeeb103
Content-Type: text/plain; charset=ISO-8859-1

That explains it, thank you.
Now only how come that
"dmd hello.d" results in a 1.70 mb application whereas
"dmd -c hello.d" && "dmd hello.obj phobos.lib" results in a 144 kb app
(sorry for confusion, Turns out I didn't link my app with phobos.lib but
recompiled the object file with phobos.lib

2011/8/20 Kagamin <spam here.lot>

 maarten van damme Wrote:

 But where does the lib file searches for the dll file? how can we control
 that? Is my hunch right?

The dll is searched by the system. It's a documented procedure. You can control it to some degree by manifest files.

--000e0cd29e26ab562c04aaeeb103 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">That explains it, thank you.=A0</div><div class= =3D"gmail_quote">Now only how come that</div><div class=3D"gmail_quote">&qu= ot;dmd hello.d&quot; results in a 1.70 mb application whereas</div><div cla= ss=3D"gmail_quote"> &quot;dmd -c hello.d&quot; &amp;&amp; &quot;dmd hello.obj phobos.lib&quot; = results in a 144 kb app</div><div class=3D"gmail_quote">(sorry for confusio= n, Turns out I didn&#39;t link my app with phobos.lib but recompiled the ob= ject file with phobos.lib</div> <div class=3D"gmail_quote"><br></div><div class=3D"gmail_quote">2011/8/20 K= agamin <span dir=3D"ltr">&lt;spam here.lot&gt;</span><br><blockquote class= =3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd= ing-left:1ex;"> <div class=3D"im">maarten van damme Wrote:<br> <br> &gt; But where does the lib file searches for the dll file? how can we cont= rol<br> &gt; that? Is my hunch right?<br> <br> </div>The dll is searched by the system. It&#39;s a documented procedure. Y= ou can control it to some degree by manifest files.<br> </blockquote></div><div><br></div> --000e0cd29e26ab562c04aaeeb103--
Aug 20 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 8/20/11, maarten van damme <maartenvd1994 gmail.com> wrote:
 Now only how come that
 "dmd hello.d" results in a 1.70 mb application whereas
 "dmd -c hello.d" && "dmd hello.obj phobos.lib" results in a 144 kb app

In both cases on my system it produces a 464Kb app. You must be doing something wrong. :)
Aug 20 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--00151748e7681c78ab04aaf57ddd
Content-Type: text/plain; charset=ISO-8859-1

hello.d first only used part from druntime, thats when it compiled to 144
kb. If I follow the same procedure with parts from phobos.lib it is 500 kb
like yours.

I think it is because of my sc.ini.
it contatins this:
-I% P%\..\..\src\phobos" "-I% P%\..\..\src\druntime\import"
"-I% P%\..\..\src\gtkd\src"
"-I% P%\..\..\src\gtkd\srcsv" "-I% P%\..\..\src\gtkd\srcgda"
"-I% P%\..\..\src\gtkd\srcgl"
"-I% P%\..\..\src\gtkd\srcgstreamer" "-L % P%\..\lib\GtkD.lib"
"-I% P%\..\..\src\bindings"
"-L % P%\..\lib\win32.lib"

Is it including stuff from that while that's not used?
I'm using dmd on windows 7.

--00151748e7681c78ab04aaf57ddd
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

hello.d first only used part from druntime, thats when it compiled to 144 k=
b. If I follow the same procedure with parts from phobos.lib it is 500 kb l=
ike yours.<div><br><div>I think it is because of my sc.ini.</div><div>it co=
ntatins this:</div>
<div>-I% P%\..\..\src\phobos&quot; &quot;-I% P%\..\..\src\druntime\import&q=
uot; =A0&quot;-I% P%\..\..\src\gtkd\src&quot; &quot;-I% P%\..\..\src\gtkd\s=
rcsv&quot; &quot;-I% P%\..\..\src\gtkd\srcgda&quot; &quot;-I% P%\..\..\src\=
gtkd\srcgl&quot; &quot;-I% P%\..\..\src\gtkd\srcgstreamer&quot; &quot;-L % =
P%\..\lib\GtkD.lib&quot; &quot;-I% P%\..\..\src\bindings&quot; &quot;-L % P=
%\..\lib\win32.lib&quot;</div>
<div><br></div><div>Is it including stuff from that while that&#39;s not us=
ed?</div><div>I&#39;m using dmd on windows 7.</div><div><br></div><div><br>=
</div></div>

--00151748e7681c78ab04aaf57ddd--
Aug 20 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--000e0cd29e4c088d4d04ab1a8bb9
Content-Type: text/plain; charset=ISO-8859-1

I'm thinking this is a compiler bug as I have reviewed my programs and
nearly all of them are 1.7 mb so it includes parts of something in my flags
while not needed

2011/8/20 maarten van damme <maartenvd1994 gmail.com>

 hello.d first only used part from druntime, thats when it compiled to 144
 kb. If I follow the same procedure with parts from phobos.lib it is 500 kb
 like yours.

 I think it is because of my sc.ini.
 it contatins this:
 -I% P%\..\..\src\phobos" "-I% P%\..\..\src\druntime\import" 
"-I% P%\..\..\src\gtkd\src"
 "-I% P%\..\..\src\gtkd\srcsv" "-I% P%\..\..\src\gtkd\srcgda"
"-I% P%\..\..\src\gtkd\srcgl"
 "-I% P%\..\..\src\gtkd\srcgstreamer" "-L % P%\..\lib\GtkD.lib"
"-I% P%\..\..\src\bindings"
 "-L % P%\..\lib\win32.lib"

 Is it including stuff from that while that's not used?
 I'm using dmd on windows 7.

--000e0cd29e4c088d4d04ab1a8bb9 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable I&#39;m thinking this is a compiler bug as I have reviewed my programs and = nearly all of them are 1.7 mb so it includes parts of something in my flags= while not needed<br><br><div class=3D"gmail_quote">2011/8/20 maarten van d= amme <span dir=3D"ltr">&lt;<a href=3D"mailto:maartenvd1994 gmail.com">maart= envd1994 gmail.com</a>&gt;</span><br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex;">hello.d first only used part from druntime,= thats when it compiled to 144 kb. If I follow the same procedure with part= s from phobos.lib it is 500 kb like yours.<div> <br><div>I think it is because of my sc.ini.</div><div>it contatins this:</= div> <div>-I% P%\..\..\src\phobos&quot; &quot;-I% P%\..\..\src\druntime\import&q= uot; =A0&quot;-I% P%\..\..\src\gtkd\src&quot; &quot;-I% P%\..\..\src\gtkd\s= rcsv&quot; &quot;-I% P%\..\..\src\gtkd\srcgda&quot; &quot;-I% P%\..\..\src\= gtkd\srcgl&quot; &quot;-I% P%\..\..\src\gtkd\srcgstreamer&quot; &quot;-L % = P%\..\lib\GtkD.lib&quot; &quot;-I% P%\..\..\src\bindings&quot; &quot;-L % P= %\..\lib\win32.lib&quot;</div> <div><br></div><div>Is it including stuff from that while that&#39;s not us= ed?</div><div>I&#39;m using dmd on windows 7.</div><div><br></div><div><br>= </div></div> </blockquote></div><br> --000e0cd29e4c088d4d04ab1a8bb9--
Aug 22 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
You are explicitly linking to GtkD.lib and win32.lib, that's what's
causing the increase in size.
Aug 22 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--000e0cd2df605fd5dd04ab1d558b
Content-Type: text/plain; charset=ISO-8859-1

Is there a way to only link against it when it's needed automatically? I was
hoping dmd would do that automatically.
But you're right, it now spits out a 144 kb executable
2011/8/22 Andrej Mitrovic <andrej.mitrovich gmail.com>

 You are explicitly linking to GtkD.lib and win32.lib, that's what's
 causing the increase in size.

--000e0cd2df605fd5dd04ab1d558b Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Is there a way to only link against it when it&#39;s needed automatically? = I was hoping dmd would do that automatically.<div>But you&#39;re right, it = now spits out a 144 kb executable<br><div class=3D"gmail_quote">2011/8/22 A= ndrej Mitrovic <span dir=3D"ltr">&lt;<a href=3D"mailto:andrej.mitrovich gma= il.com">andrej.mitrovich gmail.com</a>&gt;</span><br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex;">You are explicitly linking to GtkD.lib and = win32.lib, that&#39;s what&#39;s<br> causing the increase in size.<br> </blockquote></div><br></div> --000e0cd2df605fd5dd04ab1d558b--
Aug 22 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Usually this calls for a a build system, e.g. a build script or
something, that you use for specific projects which require GtkD or
something else.

For example (I'm assuming you're on win32), you could have this batch
file (build.bat):
http://codepad.org/vt0TskPy

And you could invoke it via "build main.d".

The "%%~na" nonsense kills the extension of a file, and then '.exe' is
appended to it. "%*" are any arguments passed to the batch file (in
this case main.d).

Or you could use a makefile, or some semi-automated build system (I
don't use any because none seem stable or they're too complicated).
Typically I use RDMD for quick testing and a batch file or a D script
for building projects.
Aug 22 2011