How can I execute C++ functions from Dlang?

thePengüin arcanet192 at gmail.com
Sat Aug 19 19:41:47 UTC 2023


On Monday, 14 August 2023 at 07:36:31 UTC, Ferhat Kurtulmuş wrote:
> On Monday, 14 August 2023 at 06:40:04 UTC, thePengüin wrote:
>> hola a todos quisiera ejecutar este codigo de c++
>> `
>> #include \<iostream>
>>
>> using namespace std;
>>
>> int main() {
>>
>>     	return 0;
>>     }
>>
>>     int foo(int i, int j) {
>>     	cout \<\< i\<\<endl;
>>     	cout \<\< j \<\< endl;
>>     	return 7;
>>     }
>>     `
>>
>> con este comando: g++ -shared app2.cpp -o app2.obj
>> y este es mi codigo en Dlang
>>
>> `
>> #!/usr/bin/env dmd
>> import core.stdc.stdio;
>>
>> import std;
>>
>> alias print = writeln;
>>
>> extern(C++) int foo(int i, int j);
>> void main () {
>>
>>     	foo(3,6);
>>
>>
>> }
>> ` con este comando: dmd app1.d app2.obj
>> pero tengo este error:
>> lld-link: error: app2.obj: unknown file type
>> Error: linker exited with status 1
>
> ?Tu usas ambos de 64 bit o 32 bit para compiladores?
>
> No puedo reproducir tu codigo. Pero yo puedo ejecutar esto en 
> Windows:
>
> cppcode.cpp
>
> ```d
> #include <iostream>
>
> using namespace std;
>
> // no nececitamos un main aqui
>
> int foo(int i, int j) {
> 	cout << i << endl;
> 	cout << j << endl;
> 	return 7;
> }
>
> ```
>
> main.d
>
> ```d
> extern(C++) int foo(int i, int j);
>
> void main () {
>     foo(3,6);
> }
> ```
>
>
> La compulacion:
>
> cl cppcode.cpp -c
> dmd main.d cppcode.obj

estoy usando el de 64 o almenos eso es lo que me dice cuando hago 
un dmd --version:
DMD64 D Compiler v2.104.2-dirty
Copyright (C) 1999-2023 by The D Language Foundation, All Rights 
Reserved written by Walter Bright

por cierto "cl" en ves de g++, creo que es la abreviatura de 
"compilador" no? porque cl no me sale como comando en el 
mingw64x84


More information about the Digitalmars-d-learn mailing list