cgo and C constants/enums

1,401 views
Skip to first unread message

Ostsol

unread,
Nov 15, 2009, 12:51:01 AM11/15/09
to golang-nuts
Maybe I'm doing something wrong, but cgo is giving me strange values
when I tell it to read a constant or an enum.

wrap.go:

package myenum

// typedef enum {
// NONE = 0,
// ONE = 1,
// TWO = 2,
// THREE = 3
// } numbers;
// const int cone = 1;
// int enum_to_int (numbers enu) {
// return (int) enu;
// }
// int return_int (int num) {
// return num;
// }
import "C"
import "fmt"

func PrintEnum () {
fmt.Printf ("%d\n", int (C.cone));
fmt.Printf ("%d\n", int (C.ONE));
fmt.Printf ("%d\n", int (C.enum_to_int (C.numbers (C.ONE))));
fmt.Printf ("%d\n", int (C.return_int (C.int (C.ONE))));
fmt.Printf ("%d\n", int (C.enum_to_int (C.numbers (C.cone))));
fmt.Printf ("%d\n", int (C.return_int (C.cone)));
}

Ok, not all of those Printfs will work. All should print 1, but only
the last two actually do. The first and second cause segmentation
faults, while the third and fourth will report the error:

./hello: symbol lookup error: ./hello: undefined symbol: ONE

Basically, C enums aren't working at all through cgo. The last two
Printfs work and print the correct value: 1. So, constants work, but
only if I first pass them through a redundant C function.

To make matters even more interesting, here's the wrap.cgo2.go:

// Created by cgo - DO NOT EDIT
package myenum

import "unsafe"

type _ unsafe.Pointer

type _C_int int32
type _C_void [0]byte
var _C_ONE *_C_int
var _C_cone *_C_int
func _C_return_int(_C_int) _C_int

Why is my constant and my enum declared as a pointer to an integer?
They should be normal integers, shouldn't they?

I guess this is an issue, but I'd like to know that I haven't done
anything wrong before I post a report.

-Ostsol

Adam Langley

unread,
Nov 15, 2009, 12:50:54 PM11/15/09
to Ostsol, golang-nuts
On Sat, Nov 14, 2009 at 9:51 PM, Ostsol <ost...@gmail.com> wrote:
> Maybe I'm doing something wrong, but cgo is giving me strange values
> when I tell it to read a constant or an enum.

Seems like it might be a bug. Please file at code.google.com/p/go/issues.



AGL
Reply all
Reply to author
Forward
0 new messages