Created
November 8, 2015 23:36
-
-
Save PetarKirov/19f966273b4a82a5c2f1 to your computer and use it in GitHub Desktop.
C++ <-> D non-virtual member function calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Build C++ static library: | |
g++ -c cpp_class.cpp -o cpp_class.o | |
ar rcs libcpp_class.a cpp_class.o | |
# Build D static library: | |
dmd -lib d_class.d | |
# Build C++ program: | |
g++ cpp_main.cpp cpp_class.cpp d_class.a -L/usr/lib/x86_64-linux-gnu/ -lphobos2 -o cpp_main | |
# Build D program: | |
dmd d_class.d libcpp_class.a /usr/lib/gcc/x86_64-linux-gnu/4.9/libstdc++.a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "cpp_class.h" | |
namespace testDInterop | |
{ | |
CppClass getCppClass() { return CppClass(); } | |
int CppClass::add(int a, int b) { return a + b; } | |
int CppClass::mul(double x, float y, char z) { return x * y * z; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
namespace testDInterop | |
{ | |
// Implemented in D | |
class DClass | |
{ | |
int32_t placeholder; | |
public: | |
int add(int, int); | |
int mul(double, float, char); | |
}; | |
// Implemented in C++ | |
class CppClass | |
{ | |
int32_t placeholder; | |
public: | |
int add(int a, int b); | |
int mul(double x, float y, char z); | |
}; | |
// Implemented in D | |
DClass getDClass(); | |
// Implemented in C++ | |
CppClass getCppClass(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "cpp_class.h" | |
using namespace std; | |
int main() | |
{ | |
// Add a reference to CppClass to prevent dead code elimination: | |
testDInterop::CppClass cpp = testDInterop::getCppClass(); | |
cpp.add(1, 2); | |
cpp.mul(3.14, 8.0, 'a'); | |
testDInterop::DClass d = testDInterop::getDClass(); | |
cout << "d.add(7, 8): " << d.add(7, 8) << endl; | |
cout << "d.mul(3.0, 2.0f, 'A'): " << d.mul(3.0, 2.0f, 'A') << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() | |
{ | |
import std.stdio; | |
CppClass cpp = getCppClass(); | |
writeln("cpp.add(40, 2): ", cpp.add(40, 2)); | |
writeln("cpp.mul(1.5, 2f, 'B') ", cpp.mul(1.5, 2f, 'B')); | |
} | |
extern (C++, testDInterop) | |
{ | |
// Implemented in C++: | |
CppClass getCppClass(); | |
// Implemented in D: | |
DClass getDClass() { return DClass(); } | |
// Implemented in D: | |
struct DClass | |
{ | |
private int placeholder; | |
int add(int a, int b) { return a + b; } | |
int mul(double x, float y, char z) { return cast(int)(x * y * z); } | |
} | |
// Implemented in C++: | |
struct CppClass | |
{ | |
private int placeholder; | |
int add(int, int); | |
int mul(double, float, char); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment