How can I execute C++ functions from Dlang?

thePengüin arcanet192 at gmail.com
Sat Aug 19 20:38:38 UTC 2023


On Saturday, 19 August 2023 at 20:16:47 UTC, Ferhat Kurtulmuş 
wrote:
> On Saturday, 19 August 2023 at 19:41:47 UTC, thePengüin wrote:
>> 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
>
> Estoy escribendo en mi celular. Entonces no tomo ayudar de 
> Google translate. Mi español no es bueno, disculpa. Nunca uso 
> un mesclar de mingw-g++ y algun d compilador en Windows. cl es 
> el comando del Visual C++. No sé si g++ y dmd son binarias 
> compatibles. Yo siempre uso LDC en Windows. Creo que tu 
> deberias installar e uso Visual studio c++ community edition. 
> Los compiladores msvc juega bien con los compiladores de dlang.
>
> Que es cl.exe: 
> https://learn.microsoft.com/en-us/cpp/build/reference/compiler-options?view=msvc-170

- ok, don't worry, Your spanish is so right, hehe I don't 
actually english cause I'm feel lazy xd.
- gracias no sabia que existia el Visual C++, gracias por el dato 
ahora mismo lo busco.


More information about the Digitalmars-d-learn mailing list