cgo long double

272 views
Skip to first unread message

bruce...@gmail.com

unread,
Oct 7, 2018, 10:26:16 PM10/7/18
to golang-nuts
The header file smi.h from libsmi (https://github.com/mikeowens/libsmi) declares a typedef using "long double"

typedef long double             SmiFloat128;

In Go versions prior to 1.11, programs using smi.h compiled without error. In Go 1.11, cgo calls out "long double" as "unexpected". Here is that changed behaviour:

bruce@calochilus:/tmp$ go version
go version go1.11 linux/amd64
bruce@calochilus:/tmp$ go build longdouble.go 
# command-line-arguments
-: unexpected: 16-byte float type - long double
bruce@calochilus:/tmp$ go1.10/bin/go version
go version go1.10.3 linux/amd64
bruce@calochilus:/tmp$ go1.10/bin/go build longdouble.go 
bruce@calochilus:/tmp$ cat longdouble.go 
package main

/*
#cgo LDFLAGS: -lsmi
#include <stdlib.h>
#include <smi.h>
*/
import "C"

import (
"fmt"
)

type SmiType struct {
smiType *C.struct_SmiType
}

func main() {
fmt.Println("Hello")
}
bruce@calochilus:/tmp$

So this raises two questions for me:

a) is it a bug that the error message does not include a line number (the code in cmd/cgo/gcc.go tries to print a line number)
b) what is a 'best practice' approach to handling such an incompatible C-type?

Regards
Bruce

Ian Lance Taylor

unread,
Oct 7, 2018, 11:01:47 PM10/7/18
to Bruce Smith, golang-nuts
Yes. It would be helpful if you could open an issue, ideally one with
a test case that does not require external header files. Thanks.

> b) what is a 'best practice' approach to handling such an incompatible
> C-type?

If you are passing arguments of type SmiType, use a tiny C wrapper to
convert from double to SmiType when calling the real C function.

If it's a struct field, there isn't really any best practice. Avoid
referring to the struct from Go, I guess.


Note that I don't know why this problem would be new in Go 1.11. It
seems more likely due to some change in the C compiler on your system.
It's possible that 1.11 is somehow invoking the C compiler
differently, but I can't recall any change that would cause that.

Ian

bruce...@gmail.com

unread,
Oct 8, 2018, 2:25:55 AM10/8/18
to golang-nuts
Thanks Ian. I have submitted issue 28069 along with the simplest example I could figure out. I found that the behaviour is triggered when a long double is nested in an inner field of a struct; moving the long double to the outer field of the struct lets the go build complete successfully.

Regards
Bruce
Reply all
Reply to author
Forward
0 new messages