D-Language and C-language mutual invocation

Source: Internet
Author: User

Many languages can be implemented with the C language of each other, but I think the D language is better, d and C compatibility is binary compatible, not on the source of compatibility, that is, the compiler binary files can be linked to each other. Nonsense not much to say directly on the example


First, the C function is called in the D language

The first is to prepare a C function

Foo.c#include <stdio.h>int Test () {printf ("Message from c\n"); return 123; Random return a value}

$ gcc-c FOO.C

Generate FOO.O


D Code

Bar.dimport Std.stdio;extern (C) int test (), void Main () {int RV = Test (); Writeln ("Test return is:", RV);

$ DMD BAR.D FOO.O

./bar

Message from C

Test return is:123


As you can see, the function to invoke C only needs to use extern (c) To declare the function, and then compile it together, if it is called C generated so, only need to compile D program when the link function can be like DMD bar.d-l-lxx

is not very simple, with C + + call C looks the same, just compile the time D need to be a binary file, C + + needs is the source code

There is a lot of information on the Internet that D to C support is not complete, so that although there is no problem, but it is obvious that the support is not good, in fact, the support is very nice, but different compilers on C will have some of their own extensions, this aspect of support is not good


Call C from D nothing, but can call the D function from c What to do, this aspect can be as easy as C + + is only D, in the D language in two cases, one is a simple function, the other is the D function used in D in the advanced features, such as classes, associative arrays, delegates, etc., Simple functions can only be declared as extern (C), if the advanced features are not difficult, but there is no such information to illustrate the need to call the D Runtime Library of the additional two functions, Rt_init () and Rt_term, I believe the name can understand his meaning, To support the advanced features in D only need to initialize the D runtime Library, at the end of the release of the D run-time library resources, see the following example


D Code

Import Std.stdio;class Test//Use class {int a = 100; void PrintA () {writeln (THIS.A);//use D standard IO function} Auto Getaa () {return ["K1": 111, "K2": 222, "K3": 334];    Returns an associative array}}extern (c) void Test ()//required to be declared as extern (c) {Auto t = new test;    T.printa (); Writeln (T.getaa ());}


C code

Bar.cint rt_init (); int rt_term (); int test (); int main () {rt_init ();    Test (); ... rt_term ();}


Compile

$ dmd-c FOO.D

$ gcc-o main BAR.C foo.o-lphobos2-l/usr/share/dmd/lib/

./main

100

["K1": 111, "K2": 222, "K3": 334]

We see that the use of the additional two functions mentioned above

Note that the compile time need to link libphobos2 This library, this is the standard library of D, I am the default installation directory under the Mac, according to the situation to find the installation directory can be


In addition to C + +, except C + +, only D is the simplest, only more than C + + two additional function calls, in addition to the extern (C) declaration, the other code is normal to write, without the need for additional C compatibility efforts, in C code need to call two more functions, If you want C to not know the existence of these two functions, you can put the calls of these two functions in the D code to call the line, that is, the initialization and release of the action itself, do not cause the caller trouble


If you have any doubts about the content, please contact


This article is from the "Li Yunxing blog" blog, make sure to keep this source http://quetzal.blog.51cto.com/3313934/1632959

D-Language and C-language mutual invocation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.