| Thread overview | |||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 18, 2015 Is it possible to call D functions from C++ | ||||
|---|---|---|---|---|
| ||||
Hello, as in the title. How can I call D functions from C++ (If my main() is in a c++ file). Thanks alot! | ||||
March 18, 2015 Re: Is it possible to call D functions from C++ | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namal | On Wednesday, 18 March 2015 at 14:17:13 UTC, Namal wrote: > Hello, as in the title. How can I call D functions from C++ (If my main() is in a c++ file). > > Thanks alot! D: extern(C++) void SomeDFunction() { doSomething(); } C++: void SomeDFunction(); void main() { SomeDFunction(); } Please also see: http://dlang.org/cpp_interface.html Kind Regards Benjamin | |||
March 18, 2015 Re: Is it possible to call D functions from C++ | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namal | On Wednesday, 18 March 2015 at 14:17:13 UTC, Namal wrote: > Hello, as in the title. How can I call D functions from C++ (If my main() is in a c++ file). > > Thanks alot! http://dlang.org/cpp_interface.html http://dlang.org/interfaceToC.html There is also D.learn ;) | |||
March 18, 2015 Re: Is it possible to call D functions from C++ | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Benjamin Thaut | On Wednesday, 18 March 2015 at 14:20:19 UTC, Benjamin Thaut wrote:
> On Wednesday, 18 March 2015 at 14:17:13 UTC, Namal wrote:
>> Hello, as in the title. How can I call D functions from C++ (If my main() is in a c++ file).
>>
>> Thanks alot!
>
> D:
>
> extern(C++) void SomeDFunction()
> {
> doSomething();
> }
>
> C++:
>
> void SomeDFunction();
>
> void main()
> {
> SomeDFunction();
> }
>
> Please also see:
> http://dlang.org/cpp_interface.html
>
> Kind Regards
> Benjamin
I'm pretty sure the OP wanted to call D functions from C++, not C++ functions from D.
| |||
March 18, 2015 Re: Is it possible to call D functions from C++ | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Benjamin Thaut | On Wednesday, 18 March 2015 at 14:20:19 UTC, Benjamin Thaut wrote: > On Wednesday, 18 March 2015 at 14:17:13 UTC, Namal wrote: >> Hello, as in the title. How can I call D functions from C++ (If my main() is in a c++ file). >> >> Thanks alot! > > D: > > extern(C++) void SomeDFunction() > { > doSomething(); > } > > C++: > > void SomeDFunction(); > > void main() > { > SomeDFunction(); > } > > Please also see: > http://dlang.org/cpp_interface.html > > Kind Regards > Benjamin he probably will need cal rt_init and rt_term too. similar post with example for C: http://forum.dlang.org/post/ljgtqa$1qb1$1@digitalmars.com | |||
March 18, 2015 Re: Is it possible to call D functions from C++ | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Benjamin Thaut | alright, thanks alot. Now I have.
test.d
import std.stdio;
extern (C++) int foo(int i, int j, int k)
{
writefln("i = %s", i);
writefln("j = %s", j);
writefln("k = %s", k);
return 1;
}
and main.cpp
int foo(int i, int j, int k);
int main()
{
foo(1,2,3);
}
Can you help me show how to compile and link it together please, thank you.
| |||
March 18, 2015 Re: Is it possible to call D functions from C++ | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namal | On Wednesday, 18 March 2015 at 14:50:21 UTC, Namal wrote: > Can you help me show how to compile and link it together please, thank you. Try C example first: http://forum.dlang.org/post/qppmeeakboxjzuuwreyy@forum.dlang.org | |||
March 18, 2015 Re: Is it possible to call D functions from C++ | ||||
|---|---|---|---|---|
| ||||
Posted in reply to krzaq | On Wednesday, 18 March 2015 at 14:40:48 UTC, krzaq wrote:
>
> I'm pretty sure the OP wanted to call D functions from C++, not C++ functions from D.
If you look very closely you will notice that my example does exactly what the OP wanted.
| |||
March 18, 2015 Re: Is it possible to call D functions from C++ | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namal | On Wednesday, 18 March 2015 at 14:50:21 UTC, Namal wrote:
>
> Can you help me show how to compile and link it together please, thank you.
What platform are you on, windows, linux, osx? What c++ compiler do you use? msvc, clang, gcc?
| |||
March 18, 2015 Re: Is it possible to call D functions from C++ | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Benjamin Thaut | On Wednesday, 18 March 2015 at 15:51:28 UTC, Benjamin Thaut wrote:
> On Wednesday, 18 March 2015 at 14:40:48 UTC, krzaq wrote:
>>
>> I'm pretty sure the OP wanted to call D functions from C++, not C++ functions from D.
>
> If you look very closely you will notice that my example does exactly what the OP wanted.
Oh, right. I'm sorry, I assumed void main() means D, since it's not legit C++.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply