www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - -lib question(s)

reply "Joshua Niehus" <jm.niehus gmail.com> writes:
Hello,

I have a few methods that I want to put into a library but I'm 
having some trouble figuring out how to go about doing it...

Here is my "library":
// world.d
module world;
import std.traits;
T hello(T)(T name) /* probably want a string constraint here... 
*/ {
   return "hello " ~ name;
}
string hello2(string name) {
   return "hello " ~ name;
}

I put this file into one of my import paths (in dmd.conf) and use 
it as follows:
// testworld.d
import std.stdio, world;
void main() {
   writeln(hello("josh"));
   //writeln(hello2("josh")); // error: undefined symbol - name 
mangle of world_hello2
}

Q1) The template version of hello seems to work, but the 
simpleton version doesn't. What am i missing?

Q2) Shouldn't I be compiling world.d with -lib and then put 
world.a in some linker directory? I did that but got nowhere 
fast.  Basically I have my .a file and a bunch of lame scripts 
that need to use functions from it, its in a -L dir listed in my 
dmd.conf, but nothing compiles.

Thanks,
Josh
Apr 27 2012
parent reply "Jesse Phillips" <jessekphillips+D gmail.com> writes:
On Saturday, 28 April 2012 at 05:37:20 UTC, Joshua Niehus wrote:

 Q1) The template version of hello seems to work, but the 
 simpleton version doesn't. What am i missing?

 Q2) Shouldn't I be compiling world.d with -lib and then put 
 world.a in some linker directory? I did that but got nowhere 
 fast.  Basically I have my .a file and a bunch of lame scripts 
 that need to use functions from it, its in a -L dir listed in 
 my dmd.conf, but nothing compiles.
Sounds like linux, you are correct that you'll want libworld.a in a path specified by -L-L However you also will need to specify the library you want to load: -L-lworld More detail. The compiler needs to know the signatures of functions it is calling. It does this by reading "header" files. In D's case source files will do. These files are searched for from your include paths which are specified with -I Once compilation is complete the compiler make a call to the linker. Linker commands are passed through with -L. The linker needs to know two things, where to find library files (defaults with /usr/lib and /usr/local/lib), and also what libraries to load. Libraries have a special naming convention which starts with 'lib' the name of the library, and an extension of .a or .so. This way you can request the library by name, -lworld, and it can be found. When using dmd you give command to be passed to the linker -L-lworld. Alternatively you could just pass the library to the compiler, libworld.a.
Apr 29 2012
parent "Joshua Niehus" <jm.niehus gmail.com> writes:
On Monday, 30 April 2012 at 02:49:21 UTC, Jesse Phillips wrote:
 However you also will need to specify the library you want to 
 load: -L-lworld

 More detail.
 [snip]
Hi Jesse, Thanks for the help, that was informative! I didn't realize I needed the load command (-L-lworld) and so I was trying to use the libworld.a file directly, thinking that dmd's -L-L search paths would handle it. Josh
Apr 29 2012