Is D backwards compatible with C if you use C libraries?

If I import std.c libraries instead of including libraries in C, will there be a C code compiler with a D compiler or will there be compatibility issues with other versions?

+7
source share
3 answers

There are several subtleties to D that can cause C code to behave exactly the way you want it to. For example, the whole promotion rules are not exactly the same (but almost), and the initialization rules are different (for example, floating point values, including arrays of such, are initialized by NaN). In addition, the C function pointer syntax has been deprecated recently, so you might have to translate some C type syntax into the equivalent D syntax.

In general, however, much attention is paid to backward compatibility, and most C code should compile in D (or with very few changes) the exact (or with very few changes) with the same semantics as in C.

Also note that std.c deprecated; use core.stdc instead.

+6
source

Your question is different from the one you ask in the body of the OP.

Q1: D is backward compatible with C if you use C libraries?

A: Yes. You can use the C libraries. More on this here .

Q2: Will C compile using D compiler?

A: There was never an intention to implement the compiler D of compiling C code. However, a lot of C code will compile because D matches the types, layout, and layout of the functions of the C compiler . Since Zor pointed to C-style function pointer syntax and C-style array, pointer syntax is deprecated.

+3
source

You can never take a C or C ++ file and compile it as D code, and you cannot just #include C headers in D. D is not backward compatible with either C or C ++. Rather, you can declare extern(C) functions in your D code and call these C functions as if they were D functions (naturally, you need to link to the C library in which they are defined). Cm

for details on calling C code from D.

druntime (which contains the kernel modules. *) has declarations for quite a few standard C and OS functions (in the core.stdc. * and core.sys. * modules), but you will have to search the files in droutime yourself to find out what they are, because they have not been properly documented at this time. For any other C functions that you want to call, you can easily create declarations for them yourself, as described in the links above.

Now C and D are very similar to syntax, so some sections of C code will be compiled simply as D-code, but there will be no programs in general. The general rule is that C / C ++ code will either be compiled as a valid D code with the same semantics, or it will not compile as a D code. There are several cases where this is not the case (for example, static arrays are value types in D, unlike C / C ++), but this is almost the case. This makes it easy to port C / C ++ code to D, but it has never been assumed that D would be backward compatible with C code like C ++.

+3
source

All Articles