Cannot compile C file using ImportC

Steven Schveighoffer schveiguy at gmail.com
Tue Nov 9 13:44:04 UTC 2021


On 11/9/21 6:45 AM, rempas wrote:
> So I'm trying to compile the [toml99](https://github.com/cktan/tomlc99) 
> C library with DMD using ImportC. I first preprocessed the file using 
> `cproc`. The reason I didn't used GCC is because it defines some symbols 
> specific to GCC so it will furthermore mess the compilation process. I 
> created a gist of the file and uploaded 
> [here](https://gist.github.com/rempas/a1633ba4d3fd4f166277d7ff35cedc63) 
> in case someone wants to get the file and try it. The error message I'm 
> getting is the following:
> 
> ```
> toml.c(39): Error: cannot implicitly convert expression `malloc` of type 
> `extern (C) void*(ulong __size)` to `extern (C) void* function(ulong)`
> toml.c(40): Error: cannot implicitly convert expression `free` of type 
> `extern (C) void(void* __ptr)` to `extern (C) void function(void*)`
> ```
> 
> These two symbols are function pointers but I wasn't able to modify and 
> make it work. Any ideas?

It seems like it should work. Figuring out the "lines" for things is 
really difficult in this expanded format, even though I know why it does 
that. I think importC should possibly allow printing of the actual 
source line along with the explicit source file/line, because knowing 
the vagrancies of the preprocessor is not going to be something that's 
easy to deal with, and you want to see what raw format is being passed 
to the compiler.

[here](https://gist.github.com/rempas/a1633ba4d3fd4f166277d7ff35cedc63#file-tomld-c-L1439) 
is where "line 39" of toml.c is.
[here](https://gist.github.com/rempas/a1633ba4d3fd4f166277d7ff35cedc63#file-tomld-c-L763) 
is where the definition of `malloc` is.

To me, this looks reasonable. I narrowed it down to the following file, 
and it builds with gcc and ldc2 on my system. I don't have support for 
DMD, as this is an Arm system, but on another system, I can confirm that 
DMD 2.098.0 does not compile this:

```c
typedef long unsigned int size_t;
extern void *malloc (size_t __size)
      ;
static void* (*ppmalloc)(size_t) = malloc;
```

I used `dmd -c testc.c`, `gcc -c testc.c` and `ldc2 -c testc.c`.

Please file a bug report.

https://issues.dlang.org

-Steve


More information about the Digitalmars-d-learn mailing list