Thread overview
How to create a library, I mean an external file that holds functions just like std.stdio
Nov 20, 2013
Carlos
Nov 20, 2013
Carlos
Nov 20, 2013
evilrat
November 20, 2013
So I'm reading this tut from Ali :)

And we I got to this part :

A code example. http://pastebin.com/ESeL7dfH

But I want to declare this functions "print" outside of the file and call the file to be loaded by the compiler. So I can use the same solution in various program without having to copy paste it to each one.

Thanks you for your attention.

Checoimg

-------------------------------------
Main file :
-------------------------------------
import std.stdio;
import print;
void main()
{
    int[] numbers;

    int count;
    write("How many numbers are you going to enter? ");
    readf(" %s", &count);

    // Read the numbers
    foreach (i; 0 .. count) {
        int number;
        write("Number ", i, "? ");
        readf(" %s", &number);

        numbers ~= number;
    }

    // Print the numbers
    writeln("Before sorting:");
    print(numbers);

    numbers.sort;

    // Print the numbers
    writeln("After sorting:");
    print(numbers);
}
-----------------------------------------------
Library file :
-----------------------------------------------

void print(int[] slice)
{
    foreach (i, element; slice) {
        writefln("%3s:%5s", i, element);
    }
}
----------------------------------------------
November 20, 2013
I just readhow to do it down on the list of tuts.

I did : dmd primes.d ./prime.d, and done program ran perfectly
November 20, 2013
On Wednesday, 20 November 2013 at 02:14:29 UTC, Carlos wrote:
> But I want to declare this functions "print" outside of the file and call the file to be loaded by the compiler. So I can use the same solution in various program without having to copy paste it to each one.

first way - put this "print" function into its own module, compile your "client" program simply adding print module in source list.

example: "dmd yourmain.d print.d"

(this way you actually just adding all code from "print" module to your program)



second way(phobos actually using this) - put your "print" in its own module and compile it as static library. then when compiling your client program link with this lib and add import search path with -I to location where print can be found.

example: "dmd print.d -lib && dmd yourmain.d print.lib"

(with this way only used stuff gets imported(function/types/etc. definitions), and then linked at compile time with static lib which contains actual compiled code for that function).