Can we use "ImportC" used yet?

jfondren julian.fondren at gmail.com
Sun Oct 17 02:56:50 UTC 2021


On Sunday, 17 October 2021 at 02:45:03 UTC, data pulverizer wrote:
> While we're on this subject, I've been having similar issues 
> now tried compiling @rempas's example file with:
>
> ```
> gcc test_og.c -c -o test_og.o
> dmd test.d test_og.o
> ```
>
> and get the response:
>
> ```
> test_og.c(1): Error: identifier or `(` expected
> test_og.c(6): Error: identifier or `(` expected
> ```

You're not doing this with the files in the thread, or you'd get

```
test.d(1): Error: module `test_c` is in file 'test_c.d' which 
cannot be read
```

as test.d is still importing test_c.c, a different file from 
test_og.o

I get your error if the original test_og.c is used but test.d is 
modified to import test_og.c:

```d
import test_og;

void main() {
       hello_world();
}
```

In this case, your compilation to test_og.o doesn't matter; D is 
still trying to import test_og.c, and since that file has CPP 
`#include` directives which importC doesn't support, it's 
erroring out on those.

To link in a C object rather than use importC, you'd need this 
test.d:

```d
extern (C) void hello_world();

void main() {
       hello_world();
}
```

With which:

```
$ gcc test_og.c -c -o test_og.o
$ dmd test.d test_og.o
$ ./test
Hello world!!!
```


More information about the Digitalmars-d-learn mailing list