Linking C libraries with DMD

bachmeier via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 21 19:52:59 PST 2016


On Friday, 22 January 2016 at 02:39:33 UTC, jmh530 wrote:
> I tried to create an example that more closely resembles what 
> is in LearningD (see 
> https://github.com/aldacron/LearningD/tree/master/Chapter09_Connecting%20D%20with%20C/clib). I created two files
>
> clib.c
> ----------------
> #include <stdio.h>
>
> int some_c_function(int);
>
> int some_c_function(int a) {
> 	printf("Hello, D! from C! %d\n", a);
> 	return a + 20;
> }
>
> and
>
> dclib.d
> -----------------
> pragma(lib, `libclib.lib`);
>
> extern(C) @nogc nothrow {
> 	int some_c_function(int);
> }
>
> void main()
> {
> 	import std.stdio : writeln;
> 	writeln(some_c_function(10));
> }
>
> ------------------
> I then ran
> gcc -Wall -fPIC -c clib.c
> gcc -shared -o libclib.dll clib.o
> implib libclib.lib libclib.dll

That should be implib /system libclib.lib libclib.dll. I'm pretty 
sure the /system option is required. (Also be sure you are not 
mixing 32-bit or 64-bit libraries. I don't think implib works 
with 64-bit.)


> I'm getting an error on the implib command on my home computer. 
> Maybe running it on a different computer would work.
>
> The LearningD book says that you should compile the libraries 
> with DMC on Windows, but I can't figure out how to generate a 
> shared library on DMC. I didn't get the implib error for what I 
> was working on before.

You won't need to use DMC if you're using implib.

> I feel like getting stuff to work with Windows is always such a 
> hassle, but that's the only way I'll be able to use this stuff 
> at work.

It definitely is more difficult.


More information about the Digitalmars-d-learn mailing list