-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
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
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); | ||
} |
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
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; | ||
} |
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
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 | ||
``` | ||
|
||
|