Skip to content
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
Open
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@bcmills
Copy link
Contributor

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 the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 6, 2016
@adg adg added this to the Go1.7Maybe milestone Jun 6, 2016
@ianlancetaylor
Copy link
Contributor

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)
}

@ianlancetaylor ianlancetaylor added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jun 7, 2016
@ianlancetaylor ianlancetaylor modified the milestones: Unplanned, Go1.7Maybe Jun 7, 2016
@bcmills
Copy link
Contributor Author

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
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants