www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Can you create a library with LDC and link to it with clang++?

reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
Can you create a library(.lib) with ldc and port it to clang++?
e.g.

//test.d
module test;
import std.stdio;
void hi()
{
writeln("hello");
}

//test.cpp(links to test.lib which contains test.hi
#include <iostream>
extern "D" void hi();
void main() {
hi();
}
//Hopefully prints "hi"
Sep 23 2020
parent reply Manu <turkeyman gmail.com> writes:
On Thu, Sep 24, 2020 at 9:35 AM Ruby The Roobster via Digitalmars-d <
digitalmars-d puremagic.com> wrote:

 Can you create a library(.lib) with ldc and port it to clang++?
 e.g.

 //test.d
 module test;
 import std.stdio;
 void hi()
 {
 writeln("hello");
 }

 //test.cpp(links to test.lib which contains test.hi
 #include <iostream>
 extern "D" void hi();
 void main() {
 hi();
 }
 //Hopefully prints "hi"
Of course. This is critically important functionality for many users. Except, you should use `extern(C++)` in your D code, and no need to extern in your C++ code.
Sep 23 2020
next sibling parent 12345swordy <alexanderheistermann gmail.com> writes:
On Wednesday, 23 September 2020 at 23:45:38 UTC, Manu wrote:
 On Thu, Sep 24, 2020 at 9:35 AM Ruby The Roobster via 
 Digitalmars-d < digitalmars-d puremagic.com> wrote:

 Can you create a library(.lib) with ldc and port it to clang++?
 e.g.

 //test.d
 module test;
 import std.stdio;
 void hi()
 {
 writeln("hello");
 }

 //test.cpp(links to test.lib which contains test.hi
 #include <iostream>
 extern "D" void hi();
 void main() {
 hi();
 }
 //Hopefully prints "hi"
Of course. This is critically important functionality for many users. Except, you should use `extern(C++)` in your D code, and no need to extern in your C++ code.
If we want a good mass adoption rate then we should aim for importing c header files c99 edition. -Alex
Sep 23 2020
prev sibling parent reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
On Wednesday, 23 September 2020 at 23:45:38 UTC, Manu wrote:
 Except, you should use `extern(C++)` in your D code, and no 
 need to extern in your C++ code.
Thanks.
Sep 23 2020
parent Manu <turkeyman gmail.com> writes:
Remember that if you're linking with Clang, it won't automatically link the
druntime library because it assumes it's linking a C++ program. Make sure
you add the D libraries to your link command if you intend to use the D
runtime library.
Also, if your main function is in C++ like you show, then you need to init
and term the D runtime library:
https://dlang.org/phobos/rt_dmain2.html#.rt_init ... Call those those at
the start/end of your main function since C++'s startup/main won't do it
for you.

On Thu, Sep 24, 2020 at 10:30 AM Ruby The Roobster via Digitalmars-d <
digitalmars-d puremagic.com> wrote:

 On Wednesday, 23 September 2020 at 23:45:38 UTC, Manu wrote:
 Except, you should use `extern(C++)` in your D code, and no
 need to extern in your C++ code.
Thanks.
Sep 24 2020