Linking C libraries with DMD
jmh530 via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jan 21 18:39:33 PST 2016
On Friday, 22 January 2016 at 01:34:00 UTC, bachmeier wrote:
>
> Have you used pragma(lib)?
> https://dlang.org/spec/pragma.html#lib
> There's also a section on it in Learning D.
>
Looks like the sections are split apart by a couple hundred
pages. I tried it with the .lib I created earlier without much
luck. Also note that the LearningD section recommends not using
the pragma. At this point, I'd rather just having something
working.
> I don't use Windows much, but when I link to a dll, that's what
> I do. I've actually never used -L options on Windows. If I want
> to call functions from R.dll, I use implib /system R.lib R.dll
> to create R.lib. Then I put pragma(lib, "R.lib"); in my .d file
> and compile.
Thanks for walking through what you do.
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
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.
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.
More information about the Digitalmars-d-learn
mailing list