mio ccec149c9b Add initial git commit as hash to example 11 months ago
..
libexample ccec149c9b Add initial git commit as hash to example 11 months ago
source 9ad955346e Initial commit 11 months ago
.gitignore 9ad955346e Initial commit 11 months ago
README.adoc ccec149c9b Add initial git commit as hash to example 11 months ago
dub.json 9ad955346e Initial commit 11 months ago

README.adoc

= PCC Example

This directory contains an example of using PCC to compile a C library "on-the-fly" and use the library in D.

Generally speaking, you will want to start by creating a file which will make use of PCC. Let's call this `build.d`:

[,d]
----
#!/usr/bin/env dub
/+dub.sdl:
name "build"
dependency "pcc" repository="git+https://notabug.org/bluemoon/pcc.git" \
version="9ad955346e74d1951da0d2811ec0de85ed4cbc16"
+/

import pcc;

void main() {
auto compiler = new Compiler();
compiler.addFile("libexample/example.c");
compiler.compileTo("libexample.a");
}
----

NOTE: Paths are relative to the `dub.sdl` or `dub.json` file.

Our `libexample/example.c` file contains the following code:

[,c]
----
int compare_int(int lhs, int rhs) {
if (lhs > rhs) {
return -1;
} else if (lhs < rhs) {
return 1;
}
return 0;
}
----

To use this in D code, we need to create a binding to it:

[,d]
----
extern(C) int compare_int(int lhs, int rhs);
----

We can then use this as we'd expect:

[,d]
----
assert(compare_int(1, 2) == 1);
assert(compare_int(2, 1) == -1);
assert(compare_int(0, 0) == 0);
----