www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem with GC - linking C++ & D (with gdc)

reply Claude <claudemr live.fr> writes:
Hello,

I'm working on a C++ project requiring an XML parser. I decided 
to make it in D so I could easily parse at run-time or 
compile-time as I wish.

As our project uses a gcc tool-chain, I naturally use GDC (GCC 
9.4.0).

But I have a few problems with D, linking with it, trying to use 
better-C and CTFE, etc.

Here's a reduced sample of one of my problems:

parser.d
```
extern(C) int* ct_parse()
{
     int* a = new int(42);
     return a;
}
```

main.cpp
```
extern "C" const int* ct_parse();

int main(int argc, char ** argv)
{
     return *ct_parse();
}
```

Compiling/linking using the following command-lines:
```
gcc -c parser.d -o parser.o
gcc -std=c++17 -c main.cpp -o main.o
gcc main.o parser.o -lstdc++ -lgphobos -lgdruntime -o test
```

It seg-faults...

Here's the output of gdb:
```
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff777858a in gc_qalloc () from 
/usr/lib/x86_64-linux-gnu/libgdruntime.so.76
```

Does anyone have any idea what's going on?

(if I just compile a single D file with "int main() { int* a = 
new int(42); return *a; }", it works as intended.)
Apr 26 2022
next sibling parent reply Iain Buclaw <ibuclaw gdcproject.org> writes:
On Tuesday, 26 April 2022 at 10:23:15 UTC, Claude wrote:
 Hello,
Hello, <%--SNIP--%>
 Does anyone have any idea what's going on?

 (if I just compile a single D file with "int main() { int* a = 
 new int(42); return *a; }", it works as intended.)
The `new` keyword requests the druntime GC to allocate memory, however you haven't initialized the D run-time in your program. main.cpp ```D extern "C" int rt_init(); extern "C" const int* ct_parse(); int main(int argc, char ** argv) { rt_init(); return *ct_parse(); } ```
Apr 26 2022
parent reply Claude <claudemr live.fr> writes:
On Tuesday, 26 April 2022 at 10:29:39 UTC, Iain Buclaw wrote:
 On Tuesday, 26 April 2022 at 10:23:15 UTC, Claude wrote:
 Hello,
Hello, <%--SNIP--%>
 Does anyone have any idea what's going on?

 (if I just compile a single D file with "int main() { int* a = 
 new int(42); return *a; }", it works as intended.)
The `new` keyword requests the druntime GC to allocate memory, however you haven't initialized the D run-time in your program. main.cpp ```D extern "C" int rt_init(); extern "C" const int* ct_parse(); int main(int argc, char ** argv) { rt_init(); return *ct_parse(); } ```
Ok, thanks! I should have suspected something like this.
Apr 26 2022
next sibling parent reply Alain De Vos <devosalain ymail.com> writes:
PS :
I use
```
ldc2 --gcc=cc ,
cc -v : clang version 11.0.1
```
Apr 26 2022
parent reply Claude <claudemr live.fr> writes:
On Tuesday, 26 April 2022 at 12:49:21 UTC, Alain De Vos wrote:
 PS :
 I use
 ```
 ldc2 --gcc=cc ,
 cc -v : clang version 11.0.1
 ```
We only have gcc in our toolchain (we target an ARM-based embedded system). --- I also encountered problems while I was trying to use CTFE only functions (using betterC so I don't have to link phobos/D-runtime). However, if those functions use the GC for instance (like appending a dynamic-array), it will require me to link D-runtime, whereas I only use them at compile-time. So I'm a bit confused... I'll try and get more information and reduce a code sample.
Apr 26 2022
parent dangbinghoo <dangbinghoo gmail.com> writes:
On Tuesday, 26 April 2022 at 13:36:19 UTC, Claude wrote:
 On Tuesday, 26 April 2022 at 12:49:21 UTC, Alain De Vos wrote:
 ---

 I also encountered problems while I was trying to use CTFE only 
 functions (using betterC so I don't have to link 
 phobos/D-runtime).

 However, if those functions use the GC for instance (like 
 appending a dynamic-array), it will require me to link 
 D-runtime, whereas I only use them at compile-time. So I'm a 
 bit confused... I'll try and get more information and reduce a 
 code sample.
dont't use `new` if you mean using betterC, `new` is based on GC. with betterC, you should using `malloc/free` reference info: https://dlang.org/spec/cpp_interface.html#memory-allocation
Apr 27 2022
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 4/26/22 3:32 AM, Claude wrote:
 On Tuesday, 26 April 2022 at 10:29:39 UTC, Iain Buclaw wrote:
 you haven't initialized the D run-time in your program.
 I should have suspected something like this.
I've just discovered[1] that I had a DConf 2020 presentation which is somewhat related to this thread. Initializing the D runtime appears at this point: https://youtu.be/FNL-CPX4EuM?t=2585 Ali [1] I am not kidding! I've just stumbled upon a directory named 'dconf_online_2020/capi' on my computer, curious, looked inside, and couldn't believe that I did prepare such a presentation! Year 2020 must really have happened after all. :)
Apr 28 2022
prev sibling parent Claude <claudemr live.fr> writes:
On Tuesday, 26 April 2022 at 10:23:15 UTC, Claude wrote:
 It seg-faults...
Just to make it clear, it seg-faults at run-time (not at compilation or link time) when I launch the executable "test".
Apr 26 2022