Thread overview
importC is not working in GDC?
5 days ago
felixfxu
5 days ago
Serg Gini
5 days ago
Iain Buclaw
5 days ago
felixfxu
5 days ago

Hi,

I'm testing importC and I found that importC works in both dmd and ldc but not gdc.

In Ubuntu 24.04, I am testing with these files:

//app.d
import std.stdio;
import importctest;
void main()
{
	importctest.test();
}

and

//importtest.c
#include <stdio.h>
void test()
{
    printf("importC is available\n");
}

and compile them by:
dmd source/app.d source/importctest.c OK

ldc2 source/app.d source/importctest.c OK,

but with gdc:

>

gdc source/app.d source/importctest.c
source/app.d:2:8: error: unable to read module ‘importctest’
2 | import importctest;
| ^
source/app.d:2:8: note: Expected 'importctest.d' or 'importctest/package.d' in one of the following import paths:
2 | import importctest;
| ^
import path[0] = /usr/lib/gcc/x86_64-linux-gnu/13/include/d

5 days ago

On Friday, 10 October 2025 at 09:14:15 UTC, felixfxu wrote:

>

Hi,

I'm testing importC and I found that importC works in both dmd and ldc but not gdc.

import path[0] = /usr/lib/gcc/x86_64-linux-gnu/13/include/d

Default version of GCC in Ubuntu 24.04 could be too old.
Probably it is GCC 13. Try to install newer version.
GCC 14 should be available easily. Or you can even try to build version 15

5 days ago

On Friday, 10 October 2025 at 09:14:15 UTC, felixfxu wrote:

>

and compile them by:
dmd source/app.d source/importctest.c OK

ldc2 source/app.d source/importctest.c OK,

but with gdc:

>

gdc source/app.d source/importctest.c
source/app.d:2:8: error: unable to read module ‘importctest’

gdc doesn't compile C files, rather it delegates the compilation to gcc-proper in a separate compilation unit.

What you're running is equivalent to:

gdc -c source/app.d
gcc -c source/importctest.c
gdc source/app.o source/importctest.o

Therefore, the D module needs to be told where to find the source files, with -I source

Mind that C sources need to be preprocessed before handing them over to the D compilation unit.
https://gcc.gnu.org/onlinedocs/gdc/ImportC.html

5 days ago

On Friday, 10 October 2025 at 10:52:44 UTC, Iain Buclaw wrote:

>

Therefore, the D module needs to be told where to find the source files, with -I source

Mind that C sources need to be preprocessed before handing them over to the D compilation unit.
https://gcc.gnu.org/onlinedocs/gdc/ImportC.html

thanks, yes, -I source worked in my case. I have a sub folder of source which caused the problem.

Then I removed sub folder of source and continue the test.

I tried to manually call the pre-processor:

gcc -E file2.c > file2.i
gdc main.d file2.i -o main

It worked (no #include yet) so I assume the command is correct.

Then I add #include <stdio.h> in file2.c and run the commands again:

//file2.c
#include <stdio.h>
void test()
{
}
//main.d
import file2;
void main()
{
	file2.test();
}
gcc -E file2.c > file2.i
gdc main.d file2.i -o main

I got this error:

gdc main.d file2.i -o main
/usr/include/stdio.h:264:44: error: found ‘__filename’ when expecting ‘,’
  264 | extern FILE *fopen (const char *__restrict __filename,
      |                                            ^
/usr/include/stdio.h:264:54: error: no type-specifier for parameter
  264 | extern FILE *fopen (const char *__restrict __filename,

maybe it does not recognize __restrict ?

I tried both gdc (version 13) and gdc-14, same error.

Anything wrong with the commands that I have used?