Get the App
SLTechnology News&Howtos  ›  Development  › 

How to migrate c to d06

Shulou Source: shulou.com Published: 2022-06-03 06:00:45 11月17日 Update

This article mainly introduces "how to transplant c to D06". In the daily operation, I believe that many people have doubts about how to transplant c to D06. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to transplant c to D06" Next, please follow the editor to study!

Method。 But the problem is. What is put in it depends on the c compiler, starting with the least important bits or the most important bits. And whether there is a blank space between the fields. It's the same problem as std.bitmanip.bitfields.

I have encountered it, and then use a single flag bit field that cannot access it. If you run on multiple platforms with libraries of other compilers, there is no simple solution. It has to be given to the user. Specific bindings for specific compilers / platforms.

Function pointer. Generally, .d has its own syntax as a callback, so it must be adapted.

Int function () MyFuncPtr

/ / d style pointer declaration

The format is medium type-> function keyword-> parameter list-> function pointer name.

You can use MyFuncPtr directly, but you can also define aliases.

Alias int function () da_MyFuncPtr

Da_MyFuncPtr MyFuncPtr

/ / it's not interesting

Int foo (int I)

{

Return i

}

Void main ()

{

Int function (int) fooPtr;//

FooPtr = & foo

Alias int function (int) da_fooPtr

Da_fooPtr fooPtr2 = & foo

Import std.stdio

Writeln (fooPtr (1))

Writeln (fooPtr2 (2))

}

/ / convert in this way

/ / In C, foo.h

Typedef int (* MyCallback) (void)

/ / In D

Extern (C) alias int function () MyCallback

Use an alias so that you can use it like c.

/ / In C, foo.h

Extern void foo (int (* BarPtr) (int))

/ / In D.

/ / 1 like this

Extern (C) void foo (int function (int) BarPtr)

/ / 2 like this

Extern (C) alias int function (int) BarPtr

Extern (C) void foo (BarPtr)

2 is better and can be reused. Next, declare the function pointer inline in the construct.

/ / In C, foo.h

Typedef struct

{

Int (* BarPtr) (int)

} baz_t

/ / In D

Struct baz_t

{

Extern (C) int function (int) BarPtr

}

Function declarations in static bindings do not have to be declared in .d. Implementation is a declaration. It also has nothing to do with your implementation / declaration of location. In order to link to the c library, you do not have to and do not have access to the implementation, so bind. In order to invoke them, d should know that they exist. So that you can find the correct address when you link. Therefore, it must be declared.

/ / In C, foo.h

Extern int foo (float f)

Extern void bar (void)

/ / In D

Extern (C)

{

Int foo (float)

Void bar ()

}

Dynamic binding. Declare with a function pointer instead of a function. It is not possible to make a simple statement. First consider initializing the function pointer.

/ / D in.

Int foo () {return 1;}

Void* getPtr () {return cast (void*) & foo;}

Void main ()

{

Int function () fooPtr

FooPtr = getPtr ()

}

/ / compiled to get

Fptr.d (10): Error: cannot implicitly change the (void*) type of (getPtr ()) to `int function () `

At this point, the study on "how to transplant c to D06" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Functions pointers porting learning compiling methods compilers problems important next aliases fields platforms more types links help no utility location Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft OPPO Reno Xiaomi vpn NVidia