www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - help understanding import libraries

reply maarten van damme <maartenvd1994 gmail.com> writes:
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
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
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 parent reply 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
next sibling parent reply 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
parent maarten van damme <maartenvd1994 gmail.com> writes:
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
Aug 19 2011
prev sibling 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 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 reply maarten van damme <maartenvd1994 gmail.com> writes:
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.
Aug 20 2011
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 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
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 reply 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
next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
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.
Aug 20 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
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.
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:
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.
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