golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.03k stars 17.67k forks source link

cgo: generate invalid header files #60112

Closed KnisterPeter closed 1 year ago

KnisterPeter commented 1 year ago

What version of Go are you using (go version)?

$ go version
go version go1.20.4 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/xxx/.cache/go-build"
GOENV="/home/xxx/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/xxx/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/xxx/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/xxx/sdk/go1.20.4"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/xxx/sdk/go1.20.4/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.4"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/xxx/source/xxx/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1261571602=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

/*
#include <security/pam_modules.h>
*/
import "C"

//export pam_sm_authenticate
func pam_sm_authenticate(pamh *C.pam_handle_t, flags C.int, argc C.int, argv **C.char) C.int {
    return C.PAM_USER_UNKNOWN
}

Compiling this with go build -buildmode=c-shared -o pam_module.so

What did you expect to see?

This is expected to compile to a shared library

What did you see instead?

It fails with the following compiler error:

# github.com/xxx/pam_module
In file included from _cgo_export.c:4:
cgo-gcc-export-header-prolog:43:12: error: conflicting types for ‘pam_sm_authenticate’
In file included from c.go:4,
                 from _cgo_export.c:4:
/usr/include/security/pam_modules.h:34:5: note: previous declaration of ‘pam_sm_authenticate’ was here
   34 | int pam_sm_authenticate(pam_handle_t *pamh, int flags,
      |     ^~~~~~~~~~~~~~~~~~~
_cgo_export.c:24:5: error: conflicting types for ‘pam_sm_authenticate’
   24 | int pam_sm_authenticate(pam_handle_t* pamh, int flags, int argc, char** argv)
      |     ^~~~~~~~~~~~~~~~~~~
In file included from c.go:4,
                 from _cgo_export.c:4:
/usr/include/security/pam_modules.h:34:5: note: previous declaration of ‘pam_sm_authenticate’ was here
   34 | int pam_sm_authenticate(pam_handle_t *pamh, int flags,
      |     ^~~~~~~~~~~~~~~~~~~
make: *** [Makefile:3: build] Error 2
seankhliao commented 1 year ago

sorry, not a supported thing see ex https://stackoverflow.com/questions/32938558/cgo-cant-find-way-to-use-callbacks-with-const-char-argument https://stackoverflow.com/questions/63207929/unable-to-export-go-function-with-const-c-char-parameter

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions