On Thursday, 11 November 2021 at 07:04:57 UTC, rempas wrote:
After not being able to use ImportC to automatically compile a
C library, now I'm trying to do it myself. The library I want
to use is [tomlc99](https://github.com/cktan/tomlc99) which is
a small C library to parse toml files. Now I compiled the
library and I copied the "config.h" file and modified to make
it a D file that will contain the declaration so D knows which
symbols to call. I resulted with a "toml.d" file and a "main.d"
file. The first one contains the decorations and the second one
contains the code from the example in the
[usage](https://github.com/cktan/tomlc99#usage) section.
However, when I try to compile, I'm getting the following error
message:
```
main.d(51): Error: need `this` for `s` of type `char*`
Deprecation: argument `__error` for format specification `"%s"`
must be `char*`, not `_error_`
main.d(57): Error: need `this` for `i` of type `long`
main.d(63): Error: need `this` for `s` of type `char*`
```
I uploaded the modified files so someone is able to look at
them and explain me what I'm doing wrong so I can properly
learn. Links:
[toml.d](https://gist.github.com/rempas/8aaab43b71e3da720ce298ef472f0673)
[main.d](https://gist.github.com/rempas/07dfb15295c5f6142c6a8dfab669c40e)
Your type definition is wrong:
```D
struct toml_datum_t {
int ok;
union u {
toml_timestamp_t* ts; /* ts must be freed after use */
char* s; /* string value. s must be freed after use */
int b; /* bool value */
int64_t i; /* int value */
double d; /* double value */
}
}
```
If you check the size of this struct, it's going to be 4,
because `u` is a type definition. What you want is either:
```D
struct toml_datum_t {
int ok;
union {
toml_timestamp_t* ts; /* ts must be freed after use */
char* s; /* string value. s must be freed after use */
int b; /* bool value */
int64_t i; /* int value */
double d; /* double value */
}
}
```
Which you access via `host.s` or:
```D
struct toml_datum_t {
int ok;
/// This is the type definition
union U {
toml_timestamp_t* ts; /* ts must be freed after use */
char* s; /* string value. s must be freed after use */
int b; /* bool value */
int64_t i; /* int value */
double d; /* double value */
}
/// This is the field
U u;
}
```
Note that instead of doing this work yourself, I would highly
recommend the excellent
[dstep](https://github.com/jacob-carlborg/dstep).