Skip to content

Commit

Permalink
d call cpp example
Browse files Browse the repository at this point in the history
  • Loading branch information
tlming16 committed Aug 28, 2020
1 parent 9527415 commit 4450507
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions d/util/cpp-d/bar.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// bar.d
extern (C++) int foo(int i, int j, int k);

void main()
{
foo(1, 2, 3);
}
12 changes: 12 additions & 0 deletions d/util/cpp-d/foo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// foo.cpp
#include <iostream>
using namespace std;

int foo(int i, int j, int k)
{
cout << "i = " << i << endl;
cout << "j = " << j << endl;
cout << "k = " << k << endl;

return 7;
}
43 changes: 43 additions & 0 deletions d/util/cpp-d/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# dlang 与 cpp

```cpp
// foo.cpp
#include <iostream>
using namespace std;
int foo(int i, int j, int k)
{
cout << "i = " << i << endl;
cout << "j = " << j << endl;
cout << "k = " << k << endl;

return 7;
}
```
```D
// bar.d
extern (C++) int foo(int i, int j, int k);
void main()
{
foo(1, 2, 3);
}
```

```
g++ -c foo.cpp
ldc2 bar.d foo.o -L-lstdc++
```

- 在 windows 环境下,这种操作无法连接成功

- 切换成 ubuntu,不管是 dmd编译器,还是ldc2 编译器,都可以链接成功

- 参考资料 https://dlang.org/spec/cpp_interface.html

- ```
sudo apt-get remove --purge ldc2
sudo apt-get install ldc2
```


0 comments on commit 4450507

Please sign in to comment.