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: macros for C field names not recognized by cgo #9601

Open
jeeger opened this Issue Jan 15, 2015 · 2 comments

Comments

Projects
None yet
3 participants
@jeeger

jeeger commented Jan 15, 2015

The linux manpage ipv6(7) states that in6_addr is a C struct containing an unsigned char array[16] with name s6_addr. When compiling a C program, this works:

#include <stdio.h>
#include <netinet/in.h>

int main() {
  struct in6_addr addr;
  printf("%s\n", addr.s6_addr);
}

However, the following Go program does not compile with the error addr.s6_addr undefined (type C.struct_in6_addr has no field or method s6_addr):

package main

// #include <netinet/in.h>
// #include <sys/socket.h>
import "C"

import "fmt"

func main() {
    // See man 7 ipv6
    var addr C.struct_in6_addr
    fmt.Printf("%v", addr.s6_addr)
}

Testing with go tool cgo reveals that the struct used is defined in in.h, and thus using addr.__in6_u works with go.

When the include file linux/in6.h is included, and go tool cgo -- -D__USE_KERNEL_IPV6_DEFS test.go is run, the correct (man page) definition of struct in6_addr shows up in _obj/_cgo_gotypes.go. I do not know how to get go to compile this correctly however.

Go version used is go version go1.3.3 linux/amd64. Contact me if you need any more information.

@jeeger

This comment has been minimized.

jeeger commented Jan 15, 2015

Ah, Linux version is Gentoo with Kernel 3.18.1.

@ianlancetaylor ianlancetaylor changed the title from in6_addr differences between C and Cgo to cmd/cgo: macros for C field names not recognized by cgo Jan 15, 2015

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Jan 15, 2015

As you probably know, this doesn't work because struct in6_addr has no field named s6_addr. Instead, the header file has

#define s6_addr __in6_u.__u6_addr8

So if we are going to fix this, the fix would be to somehow get cgo to recognize defined macro names when they are used as field names. I don't think this is likely to happen any time soon, but I will leave the issue open since it would be nice if it worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment