Skip to content

Commit a687d7f

Browse files
b-longMarid de Uill
andcommitted
Add C23 compatibility for bool typedef
Wraps 'typedef uint8_t bool;' with preprocessor guards to avoid conflicts with C23's native bool type. This fixes compilation errors with newer C compilers that default to C23 standard. Based on go-python#379 by @deuill. Co-Authored-By: Marid de Uill <maridlcueto@gmail.com>
1 parent 41742c1 commit a687d7f

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

bind/gen.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ package main
5252
%[3]s
5353
// #define Py_LIMITED_API // need full API for PyRun*
5454
#include <Python.h>
55+
#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 202311L)
5556
typedef uint8_t bool;
57+
#endif
5658
// static inline is trick for avoiding need for extra .c file
5759
// the following are used for build value -- switch on reflect.Kind
5860
// or the types equivalent
@@ -409,8 +411,8 @@ build:
409411
# goimports is needed to ensure that the imports list is valid
410412
$(GOIMPORTS) -w %[1]s.go
411413
# this will otherwise be built during go build and may be out of date
412-
- rm %[1]s.c
413-
echo "typedef uint8_t bool;" > %[1]s_go.h
414+
- rm %[1]s.c
415+
printf "#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 202311L)\ntypedef uint8_t bool;\n#endif\n" > %[1]s_go.h
414416
# this will fail but is needed to generate the .c file that then allows go build to work
415417
- $(PYTHON) build.py >/dev/null 2>&1
416418
# generate %[1]s_go.h from %[1]s.go -- unfortunately no way to build .h only

cmd_build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func runBuild(mode bind.BuildMode, cfg *BuildCfg) error {
125125

126126
if mode == bind.ModeExe {
127127
of, err := os.Create(buildname + ".h") // overwrite existing
128-
fmt.Fprintf(of, "typedef uint8_t bool;\n")
128+
fmt.Fprintf(of, "#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 202311L)\ntypedef uint8_t bool;\n#endif\n")
129129
of.Close()
130130

131131
fmt.Printf("%v build.py # will fail, but needed to generate .c file\n", cfg.VM)

0 commit comments

Comments
 (0)