www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Is it possible to call D functions from C++

reply "Namal" <sotis22 mail.ru> writes:
Hello, as in the title. How can I call D functions from C++ (If 
my main() is in a c++ file).

Thanks alot!
Mar 18 2015
next sibling parent reply "Benjamin Thaut" <code benjamin-thaut.de> writes:
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
Mar 18 2015
next sibling parent reply "krzaq" <dlangmailinglist krzaq.cc> writes:
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.
Mar 18 2015
parent reply "Benjamin Thaut" <code benjamin-thaut.de> writes:
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.
Mar 18 2015
parent reply "krzaq" <dlangmailinglist krzaq.cc> writes:
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++.
Mar 18 2015
parent reply "Kagamin" <spam here.lot> writes:
On Wednesday, 18 March 2015 at 15:54:04 UTC, krzaq wrote:
 Oh, right. I'm sorry, I assumed void main() means D, since it's 
 not legit C++.
C++ allows implementation-defined return type of main.
Mar 19 2015
parent reply "krzaq" <dlangmailinglist krzaq.cc> writes:
On Thursday, 19 March 2015 at 07:42:48 UTC, Kagamin wrote:
 On Wednesday, 18 March 2015 at 15:54:04 UTC, krzaq wrote:
 Oh, right. I'm sorry, I assumed void main() means D, since 
 it's not legit C++.
C++ allows implementation-defined return type of main.
I'm afraid that you've mistaken C++ for C. The C++ standard quite clearly says:
 An implementation shall not predefine the main function. This 
 function shall not be overloaded. **It shall have a return type 
 of type int**, but otherwise its type is implementation-defined.
Mar 19 2015
parent reply "Kagamin" <spam here.lot> writes:
On Thursday, 19 March 2015 at 08:32:10 UTC, krzaq wrote:
 but otherwise its type is implementation-defined.
This.
Mar 19 2015
parent reply "krzaq" <dlangmailinglist krzaq.cc> writes:
On Thursday, 19 March 2015 at 08:33:07 UTC, Kagamin wrote:
 On Thursday, 19 March 2015 at 08:32:10 UTC, krzaq wrote:
 but otherwise its type is implementation-defined.
This.
I'm not sure what you're failing to understand here. It **shall* have the return type of int. The rest (as in: parameter list) is implementation defined.
Mar 19 2015
parent reply "Kagamin" <spam here.lot> writes:
Hmm... I read it as "it shall have the return type of int, but if 
not, then it's implementation-defined".
Mar 19 2015
next sibling parent reply "krzaq" <dlangmailinglist krzaq.cc> writes:
On Thursday, 19 March 2015 at 08:38:45 UTC, Kagamin wrote:
 Hmm... I read it as "it shall have the return type of int, but 
 if not, then it's implementation-defined".
In that case, I refer you to https://www.ietf.org/rfc/rfc2119.txt
Mar 19 2015
parent Andre Kostur <andre kostur.net> writes:
On 2015-03-19 1:57 AM, krzaq wrote:
 On Thursday, 19 March 2015 at 08:38:45 UTC, Kagamin wrote:
 Hmm... I read it as "it shall have the return type of int, but if not,
 then it's implementation-defined".
In that case, I refer you to https://www.ietf.org/rfc/rfc2119.txt
To be picky (because I live in RFCs)... RFC 2119 doesn't have anything to say about "shall". It does have something to say about "SHALL". The capitalization is important.
Mar 20 2015
prev sibling next sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 3/19/15 1:38 AM, Kagamin wrote:
 Hmm... I read it as "it shall have the return type of int, but if not,
 then it's implementation-defined".
"shall" is prescriptive in standardese, i.e. means "must". -- Andrei
Mar 19 2015
prev sibling parent "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Thursday, 19 March 2015 at 08:38:45 UTC, Kagamin wrote:
 Hmm... I read it as "it shall have the return type of int, but 
 if not, then it's implementation-defined".
The semantics of returning from main() is defined as: std::exit(main(…))
Mar 20 2015
prev sibling next sibling parent "Daniel Kozak" <kozzi11 gmail.com> writes:
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
Mar 18 2015
prev sibling parent reply "Namal" <sotis22 mail.ru> writes:
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.
Mar 18 2015
next sibling parent "Kagamin" <spam here.lot> writes:
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
Mar 18 2015
prev sibling parent reply "Benjamin Thaut" <code benjamin-thaut.de> writes:
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?
Mar 18 2015
parent reply "Namal" <sotis22 mail.ru> writes:
On Wednesday, 18 March 2015 at 15:52:33 UTC, Benjamin Thaut wrote:
 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?
Linux, gcc and dmd. In my case c++ refuses to compile if main is void.
Mar 19 2015
parent "Benjamin Thaut" <code benjamin-thaut.de> writes:
On Thursday, 19 March 2015 at 11:47:37 UTC, Namal wrote:
 On Wednesday, 18 March 2015 at 15:52:33 UTC, Benjamin Thaut 
 wrote:
 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?
Linux, gcc and dmd. In my case c++ refuses to compile if main is void.
well then change it to the full signature ;-)
Mar 19 2015
prev sibling parent "Chris" <wendlec tcd.ie> writes:
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 ;)
Mar 18 2015