-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/cgo: "initializer element is not constant" for #define structs #58638
Comments
I was able to get this to compile by using Clang ( |
@golang/compiler |
This doesn't have anything to do with cgo or Go. A pure C file #include "ctest.h"
JSValue x = JS_UNDEFINED; will report the same error when using GCC. |
@ianlancetaylor The error you get when compiling your code is slightly different:
You'll notice your const assignment is actually part of the error. No where in my Cgo example do I create a const assignment, so it's unclear where the error is coming from. For instance, if you write a C function similar to my Go function, GCC will compile it:
|
The error string is coming from the cgo tool. It's because the cgo tool itself needs to determine the type of the value. It does by writing out __typeof__(JS_UNDEFINED) *__cgo__1; It compiles that as C code, and looks at the DWARF debug info to determine type. Unfortunately, that code doesn't compile when using GCC. I think that to make this work with GCC you are going to write something like /*
#include "ctest.h"
static JSValue undef() { return JS_UNDEFINED; }
*/
import "C"
import "fmt"
func MyTest() {
var s C.JSValue = C.undef()
fmt.Println(s)
} |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I'm trying to link against the quickjs library. The library builds successfully on my machine, but my Go code fails to compile once the header file for this library is included. I've created a minimal reproduction here that requires only a header file to illustrate the issue:
The code above comes from the quick js library. The goal is to use a macro to define that initializes a struct.
What did you expect to see?
I expected this code to compile on Linux, like it does on MacOS.
What did you see instead?
The text was updated successfully, but these errors were encountered: