Bug 5468 – Linker fails to link libraries using phobos2 with C code

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
tools
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2011-01-20T11:52:00Z
Last change time
2015-06-09T04:37:39Z
Assigned to
nobody
Creator
pastas4

Comments

Comment #0 by pastas4 — 2011-01-20T11:52:33Z
When you try to create a static D library that imports and/or uses anything from the phobos2 library, the generated library can no longer be linked with C programs. For testing purposes, I have two files, the D library: ~~~~~~~~~~~~~~~~ module dpart; import std.stdio; extern(C): shared int ResultD; int Process(int Value) { printf("You have sent the value %d to the D library.\n", Value); ResultD = (Value % 5); return ResultD; } ~~~~~~~~~~~~~~~~ And the C file that is using the library: ~~~~~~~~~~~~~~~~ #include <stdio.h> extern int ResultD; int Process(int Value); int main() { int Result; printf("This text is written in C. Input a number.\n"); scanf("%d", &Result); Process(Result); printf("%d modulus 5 is %d.\n", Result, ResultD); getchar(); getchar(); return 0; } ~~~~~~~~~~~~~~~~ I compile this code using two steps: dmd -c -lib dpart.d dmc dpart.lib cpart.c And I get this linker error: ~~~~~~~~~~~~~~~~ link cpart,,,dpart+user32+kernel32/noi; OPTLINK (R) for Win32 Release 8.00.5 Copyright (C) Digital Mars 1989-2009 All rights reserved. http://www.digitalmars.com/ctg/optlink.html dpart.lib Warning 140: Library probably needs FIXLIB dpart.lib(dpart) Error 42: Symbol Undefined _D3std5stdio12__ModuleInfoZ --- errorlevel 1 ~~~~~~~~~~~~~~~~ I get additional errors if I try to use writeln() instead of printf(), but it compiles and runs fine if I import std.c.stdio instead of std.stdio. Linking also fails if I try to import any of other parts of phobos2 included in D, such as std.path. I'm using DMD 2.051 and DMC 8.42n on Windows 7 x64.
Comment #1 by braddr — 2011-02-06T15:27:37Z
You need to link against the phobos library as well, something like: dmd -c -lib dpart.d dmc cpart.c dpart.lib phobos.lib Alternatively, use dmd for the last step, something like: dmd -c -lib dpart.d dmc -c cpart.c dmd cpart.o dpart.lib