`glib.h` should be included in C code when there is `true` or `false` in Vala code
true
and false
are gboolean
so glib.h
should also be included.
A simple example:
#/usr/bin/env vala
void main() {
if (true) {
stdout.printf("test");
}
}
And it will throw out errors:
/tmp/temptest.vala.XXDIT1.c: In function '_vala_main':
/tmp/temptest.vala.XXDIT1.c:11:13: error: 'TRUE' undeclared (first use in this function)
11 | if (TRUE) {
| ^~~~
/tmp/temptest.vala.XXDIT1.c:11:13: note: each undeclared identifier is reported only once for each function it appears in
error: cc exited with status 256
Another example without obvious true
or false
:
#!/usr/bin/env -S vala
void main() {
while (1 == 1) {
stdout.printf("test");
}
}
And the errors are:
/tmp/temptest.vala.9S2JT1.c: In function '_vala_main':
/tmp/temptest.vala.9S2JT1.c:11:16: error: 'TRUE' undeclared (first use in this function)
11 | while (TRUE) {
| ^~~~
/tmp/temptest.vala.9S2JT1.c:11:16: note: each undeclared identifier is reported only once for each function it appears in
error: cc exited with status 256
Using vala without glib may not be so common, but it makes the language uncritical. glib.h
should also be included in these situations or we may use 1
and 0
to replace true
and false
(or to define macros) if glib.h
is not included.
Edited by Zhou Qiankang