betterC与C混合编译

参考地址

#include 

int count();  // From the D code

int main()
{
    int j;
    for (j = 0; j < 10; j++)
    {
        printf("%d\n", count());
    }
    return 0;
}

上面c代码,下面d代码:

module count;

@nogc:
nothrow:

import core.atomic : atomicOp, atomicLoad;

extern(C)
{
        int count()
        {
                scope(exit) counter.addOne();
                return counter.getValue();
        }
}

private:

shared struct AtomicCounter(T)
{
        void addOne() pure
        {
                atomicOp!"+="(_v, 1);
        }

        int getValue() const pure
        {
                return atomicLoad(_v);
        }

        private:
        T _v;
}

unittest
{
        shared test_counter = AtomicCounter!int(42);
        assert (test_counter.getValue() == 42);
        test_counter.addOne();
        assert (test_counter.getValue() == 43);
}

shared counter = AtomicCounter!int(1);
编译命令:

dmd -c -m32mscoff -betterC d.d
cl c.c d.obj
```

你可能感兴趣的:(笔记)