Making a D library for a C executable

Jan Allersma janallersma at gmail.com
Thu Apr 27 20:32:24 UTC 2023


Hello,

I see some examples on the internet on how to call C(++) in D. 
But I want to do it the other way around. Because of (probably) 
linkage issues, this way seems easier. I have this D code:

```
import std.stdio;

int maint() {
   writeln("Returning some random stuff...");
   return 10;
}

extern (C++) int foo() {
   return maint();
}

```

And this CPP source file:

```
#include <iostream>

int main() {
   std::cout << "deede" << foo() << std::endl;
}
```

This is some gibberish, but the point is that foo needs to be 
called from the D source file.

In Bash I compile it and get a response:
```
$ g++ ../build/libshared-api.so main.cpp
main.cpp: In function ‘int main()’:
main.cpp:4:27: error: ‘foo’ was not declared in this scope
     4 |   std::cout << "deede" << foo() << std::endl;
       |
```
Apparently foo isn't found from the CPP source file. Anyone some 
ideas on how to solve this? :)


More information about the Digitalmars-d-learn mailing list