www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Translating C headers to D: How do I compile it?

reply Kirill <kirill.saidov mail.com> writes:
Hello. I am learning how to translate C headers to D. But how do 
I compile it? I am stuck with this.

I have 4 files in the same directory: main.d, something.d, 
some.h, some.c

some.h:
//header guards
int add(int a, int b);

some.c:
#include "some.h"
int add(int a, int b) { return a+b; }

something.d:
module something;
int add(int a, int b);

main.d:
import std.stdio: writeln;
import something;

void main() { writeln("5+7=", add(5, 7); }

How do I compile this correctly?

Thank you for your time.
Jun 27 2020
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
On 28/06/2020 4:59 PM, Kirill wrote:
 module something;
extern(C)
 int add(int a, int b);
Compile as static library some.c, add to command line of dmd. Should be this simple more or less, depending on compilers and target involved.
Jun 27 2020
prev sibling next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Sunday, 28 June 2020 at 04:59:12 UTC, Kirill wrote:
 Hello. I am learning how to translate C headers to D. But how 
 do I compile it? I am stuck with this.

 I have 4 files in the same directory: main.d, something.d, 
 some.h, some.c

 some.h:
 //header guards
 int add(int a, int b);

 some.c:
 #include "some.h"
 int add(int a, int b) { return a+b; }

 something.d:
 module something;
 int add(int a, int b);
This should be extern(C) int add(int a, int b). The extern(C) tells the D compiler to use the standard C calling convention when calling that function.
 main.d:
 import std.stdio: writeln;
 import something;

 void main() { writeln("5+7=", add(5, 7); }

 How do I compile this correctly?

 Thank you for your time.
The C file and the header do not need to be (and arguably shouldn't be) in the same directory as the D files. Use a C compiler to compile the C file to an object file or into a static library. Assuming you're using DMD, then on Windows you'll want to use the Microsoft compiler (cl) which is part of the Microsoft Build Tools distribution and Visual Studio. Otherwise, you'll need the Digital Mars C and C++ compiler. On other platforms, GCC or clang will be fine. If you don't know how to compile C programs, there's plenty of information online for all the major compilers. Then, when you compile your D program, you'll need to link with the object file or the static library you compiled with the C compiler. On Windows, if you used Digital Mars C (dmc), then you won't need to pass any special compiler flags if you are calling DMD directly. If you used the MS compiler, you'll need to pass either -m32mscoff for 32-bit, or -m64 for 64-bit.
Jun 27 2020
parent Kirill <kirill.saidov mail.com> writes:
On Sunday, 28 June 2020 at 05:13:32 UTC, Mike Parker wrote:
 On Sunday, 28 June 2020 at 04:59:12 UTC, Kirill wrote:
 something.d:
 module something;
 int add(int a, int b);
This should be extern(C) int add(int a, int b). The extern(C) tells the D compiler to use the standard C calling convention when calling that function.
Thanks! It all works now! 'extern(C)' is what was missing.
Jun 28 2020
prev sibling parent mw <mingwu gmail.com> writes:
On Sunday, 28 June 2020 at 04:59:12 UTC, Kirill wrote:
 Hello. I am learning how to translate C headers to D. But how 
 do I compile it? I am stuck with this.
You can try just to use dpp: https://code.dlang.org/packages/dpp instead of doing the translation manually.
Jun 27 2020