www.digitalmars.com         C & C++   DMDScript  

D.gnu - importC is not working in GDC?

reply felixfxu <felixfxu gmail.com> writes:
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:

```d
//app.d
import std.stdio;
import importctest;
void main()
{
	importctest.test();
}
```
and
```c
//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
Oct 10
next sibling parent Serg Gini <kornburn yandex.ru> writes:
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
Oct 10
prev sibling parent reply Iain Buclaw <ibuclaw gdcproject.org> writes:
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: ```bash 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
Oct 10
parent felixfxu <felixfxu gmail.com> writes:
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: ```sh 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: ```c //file2.c #include <stdio.h> void test() { } ``` ```d //main.d import file2; void main() { file2.test(); } ``` ```sh 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?
Oct 10