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: undefined reference when linking a program that refers to SIG_DFL. #15980

Open
bcmills opened this Issue Jun 6, 2016 · 2 comments

Comments

Projects
None yet
3 participants
@bcmills
Member

bcmills commented Jun 6, 2016

cgo appears to be unable to link Go programs that refer to SIG_DFL or SIG_IGN.

(This is admittedly an esoteric use-case, since Go programs should generally not be using signal.h in any way. However, it may be a symptom of some other bug around cgo's handling of function pointers and macros.)

Platform:

$ go version
go version go1.6.1 linux/amd64
$ uname -srio
Linux 3.13.0-83-generic x86_64 GNU/Linux

Source:

$ cat src/sig_dfl/sigdfl.go
package main

import "fmt"

/*
#include <signal.h>
static const void* sig_dfl = SIG_DFL;
*/
import "C"

func main() {
        fmt.Println(C.SIG_DFL)
        fmt.Println(C.sig_dfl)
}

Error:

$ go build sig_dfl
# sig_dfl
/tmp/go-build592850176/sig_dfl/_obj/_cgo_main.o:(.data.rel+0x0): undefined reference to `sig_dfl'
/tmp/go-build592850176/sig_dfl/_obj/_cgo_main.o:(.data.rel+0x8): undefined reference to `SIG_DFL'
collect2: error: ld returned 1 exit status

@adg adg added this to the Go1.7Maybe milestone Jun 6, 2016

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Jun 7, 2016

In general, cgo does a poor job with macros.

The workaround is to not refer to the macro directly, and not make the variable static.

package main

import "fmt"

/*
#include <signal.h>
const void* sig_dfl = SIG_DFL;
*/
import "C"

func main() {
        fmt.Printf("%p\n", C.sig_dfl)
}
@bcmills

This comment has been minimized.

Member

bcmills commented Jun 7, 2016

The need to introduce a variable seems like a reasonable restriction. The constraint that it cannot be static is surprising given that static functions do work:

package main

import "fmt"

/*
#include <signal.h>
static const void* sig_dfl() { return SIG_DFL; };
*/
import "C"

func main() {
    fmt.Println(C.sig_dfl())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment