Search code examples
dvisual-d

How to link to packages in static libraries using Visual D


I am using VisualD and DMD to write D in Visual Studio 2012.

My solution looks like this in the Solution Explorer:

ConsoleApp1 (solution name)
- codecramlib (a static D library)
- - http (folder)
- - - package.d 
- - - request.d
- malakai (console project)
- - main.d

codecramlib

My static D library codecramlib is building fine on it's own. Here's the source:

package.d

module codecramlib.http;

public import request;

request.d

module request;

// temporary filler code
class Request
{
    public int imARequest()
    {
        return 13;
    }
}

When I right click on codecramlib and build I get this output in the vs console:

------ Build started: Project: codecramlib, Configuration: Debug Win32 ------
Building Debug\codecramlib.lib...
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

malakai (main project)

main.d:

import std.stdio;
import codecramlib.http; // also tried import codecramlib;

int main(string[] argv)
{
    writeln("Hello D-World!");
    return 0;
}

I added codecramlib as a malakai dependency by right clicking on the malakai project and selecting Properties > Configuration Properties > Compiler > General and modifying the "Additional Imports" field to read "../codecramlib". Then I click 'Apply' and 'OK'.

when I right click on malakai in the Solution Explorer and click Build, I get this error:

Error   1   Error: module http is in file 'codecramlib\http.d' which cannot be read C:\Users\< path removed >\Visual Studio 2012\Projects\ConsoleApp1\malakai\main.d    2   

Troubleshooting

The first thing I tried was to change the "Additional Imports" line to:

../

This seemed to help because the build error changed to: Error 1 Error: module request is in file 'request.d' which cannot be read C:\Users\< path removed >\Visual Studio 2012\Projects\ConsoleApp1\codecramlib\http\package.d 3

In an attempt to get the compiler to recognize request.d in the codecramlib static library, I changed package.d to read:

module codecramlib.http;

public import http.request;

This seemed to be a step backwards because then the codecramlib library no longer built, and when I tried to build malakai I got these errors:

Error   1   Error: module request from file http\request.d must be imported as module 'request' C:\Users\< path removed >\Visual Studio 2012\Projects\ConsoleApp1\codecramlib\http\package.d    3   
Error   2   Error: module request is in file 'http\request.d' which cannot be read  C:\Users\< path removed >\Visual Studio 2012\Projects\ConsoleApp1\codecramlib\http\package.d    3   

How do I use the codecramlib.http package in the codecramlib static library in my malakai console project with Visual D?

Edit

Corrected my folder structure as shown in the solution explorer. My indentations of package.d and request.d were not correct.

Edit 2

Added content of main.d


Solution

  • This part

    The first thing I tried was to change the "Additional Imports" line to:

    ../

    This seemed to help because the build error changed to: Error 1 Error: module request is in file 'request.d' which cannot be read C:\Users\< path removed >\Visual Studio 2012\Projects\ConsoleApp1\codecramlib\http\package.d 3

    leads me to believe that it is because your request module is not in the codecramlib.http package, and because of that dmd doesn't know to look in /codecramlib/http/ for request.d.

    My suggestion is to leave the "Additional Imports" field as "../", and rename the module in codecramlib/http/request.d to codecramlib.http.request, and then change your import in codecramlib/http/package.d to public import codecramlib.http.request.

    Generally speaking, you'll want to match your module names with the directory layout.