Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 480 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is D backwards compatible with C if you use the C libraries?

#1
If I import the std.c libaries instead of including the libraries in C, would C code compile with a D compiler, or are there other backwords compatibility issues?
Reply

#2
There are several subtleties in D that can make C code not behave exactly as you may want it to. For instance, integer promotion rules are not quite the same (but almost), and initialization rules are different (floating point values -- including arrays of such -- are initialized to NaN, for example). Further, C function pointer syntax was deprecated recently, so you might have to translate some C type syntax to the equivalent D syntax.

In general, though, there is great focus on backwards compatibility, and *most* C code should compile fine (or with a very minor amount of changes) in D with the same semantics as in C.

Also note that `std.c` is deprecated; please use `core.stdc` instead.
Reply

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

Q1: *Is D backwards compatible with C if you use the C libraries?*

A: Yes. You can use C libraries. More about this [here][1].

Q2: *Would C code compile with a D compiler?*

A: It was never intention for an implementation of D compiler to be able to compile C code. However, lots of C code would compile because D *[matches C compiler's data types, layouts, and function call/return sequences][2]*. As Zor pointed out [C-style function pointer syntax][3], and [C-style array pointer syntax][4] has been deprecated.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

Reply

#4
You're never going to be able to take a C or C++ file and compile at as D code, and you can't just `#include` C headers in D. D is _not_ backwards compatible with either C or C++. Rather, it's possible to declare `extern©` functions in your D code and call those C functions as if they were D functions (naturally, you then have to link with the C library that they're defined in). See

* [Interfacing to C](

[To see links please register here]

)
* [Interfacing to C++](

[To see links please register here]

)
* [Converting C.h Files to D Modules](

[To see links please register here]

)

for details on calling C code from D.

druntime (which contains the core.* modules) has declarations for quite a few of the standard C and OS functions (in the core.stdc.* and core.sys.* modules), but you'll have to look at the druntime files yourself to see what they are, because they're not properly documented at this point. 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 syntactically, so some sections of C code will compile just fine as D code, but programs as a whole will not. The general rule is that C/C++ code will either compile as valid D code with the same semantics, or it won't compile as D code. There are a few cases where that isn't true (e.g. static arrays are value types in D, unlike C/C++), but it is in almost all cases. This makes *porting* C/C++ code to D fairly easy, but it was never intended that D be backwards compatible with C code in the way that C++ is.
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through