Cross compatibility issues with cgo

Hi!

I tried creating C binding library using cgo on Windows as follows:

go build -o libexample.a -buildmode=c-archive

This step generates libexample.h & libexample.a.

When I copy these 2 in the folder where my C++ code exists, then the C++ code compiles successfully

However, when I copy these 2 files on the same C++ code folder on MacOS, then on compilation of C++ code, I get this error:

In file included from ..........cpp:9:
cgo-gcc-export-header-prolog:106:8: error: unknown type name '_Bool'
extern _Bool function1();
^
cgo-gcc-export-header-prolog:186:8: error: unknown type name '_Bool'
extern _Bool function2();
^

These 2 functions are defined in the .go files as follows:

//export function1
func function1() C.bool {
.
.
.
}
//export function2
func function2() C.bool {
.
.
.
}

The C.bool return type generates _Boolreturn type instead of bool on using cgo

The temporary fix (i.e. manually changing _Bool to bool in both these declarations in the generated .h file) works, but is not a proper fix as I need to regenerate the libexample.h library with some custom functions

Kindly suggest a proper fix.

Thanks

Hi, @test, _Bool was defined in C99. It sounds like the C compiler on your Mac doesn’t support C99 (or perhaps needs additional configuration/switches to enable C99 support). You could look into your C compiler’s options on how to enable C99 support and pass those options in your build command. An alternative is you could just return an int and handle converting the result of that function to a bool in Go.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.