Compiling D project as a library - what happens with dependencies?

165 views Asked by At

Ok, so here's my question:

I have a working DUB project which produces an application. I decided I also wanted a "library" configuration in my dub.json file:

"configurations": [
    {
        "name": "application",
        "targetType": "executable"
    },
    {
        "name": "library",
        "targetType": "library",
    }
],

So, now when I build the project using dub build --config=library, it produces a libXXXX.a file in the same directory.

So far, so good.

I tried using this library (actually a tiny test-function marked as extern "C" from a test C app).

So, I compile my C app using gcc -c ctest.c and then link them all together like dmd libMYLIBRARY.a ctest.o.

Now, here is the problem:

In this last step, the linker complains that many symbols are missing - all coming from external dependencies (2 object files and several .a libraries) that would normally be linked when building the project as an application.

So, the question is... how do I solve this?

I mean... Should I just link my test C app against ALL of the original dependencies (this would not make the library very portable admittedly), or is there any way around it, so that anybody could use my library, only by linking against my libXXXXX.a file?

1

There are 1 answers

2
Vladimir Panteleev On

Should I just link my test C app against ALL of the original dependencies (this would not make the library very portable admittedly),

This is the "technically correct" answer. The reason for that is because, otherwise, if the C app wanted to use another D library which had among its dependencies some package that's also a dependency in your library, and if it were linked in the same way (including all of its dependencies in its static library file), this dependency would then occur twice in the linker inputs. Even if you told the linker to discard one copy, there can be problems due to the dependency being of separate incompatible versions, etc. (Note that there is an ongoing D SAOC project to handle this.)

If you were to assume that the only D library the C program will use is your Dub package, then you could conceivably create a static library which includes all dependencies, though it would probably need to include the D standard library and runtime as well.