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: c code that was compiled with go, cannot also be used by go #27019

Closed
adam-hanna opened this issue Aug 16, 2018 · 5 comments

Comments

@adam-hanna
Copy link

commented Aug 16, 2018

Please answer these questions before submitting your issue. Thanks!

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

$ go version
go version go1.10.3 linux/amd64

Does this issue reproduce with the latest release?

I have not tried with any go1.11.x releases

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

$ go env
GOARCH="amd64"
GOBIN="/home/adam/go/bin"
GOCACHE="/home/adam/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/adam/go"
GORACE=""
GOROOT="/home/adam/.gvm/gos/go1.10.3"
GOTMPDIR=""
GOTOOLDIR="/home/adam/.gvm/gos/go1.10.3/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build321160569=/tmp/go-build -gno-record-gcc-switches"

What did you do?

You can find a demo, here.

// lib/lib.go
package main

import "C"
import "log"

//export myPrint
func myPrint(str *C.char) {
    gStr := C.GoString(str)
    log.Println(gStr)
}

func main() {}
//main.go
package main

//#include <stdlib.h>
//#include "./lib/lib.h"
import "C"
import "unsafe"

func main() {
    cs := C.CString("hello, world!")
    defer C.free(unsafe.Pointer(cs))
    C.myPrint(cs)
}
$ go build -o ./lib/lib.so -buildmode=c-shared ./lib/lib.go
$ go run main.go
# command-line-arguments
In file included from ./main.go:3:0:
cgo-builtin-prolog:7:48: error: conflicting types for '_GoString_'
cgo-builtin-prolog:6:44: note: previous declaration of '_GoString_' was here
In file included from ./main.go:3:0:
cgo-gcc-export-header-prolog:27:20: error: 'GoString' redeclared as different kind of symbol
cgo-builtin-prolog:8:12: note: previous declaration of 'GoString' was here

What did you expect to see?

$ go build -o ./lib/lib.so -buildmode=c-shared ./lib/lib.go
$ go run main.go
hello, world!

What did you see instead?

$ go build -o ./lib/lib.so -buildmode=c-shared ./lib/lib.go
$ go run main.go
# command-line-arguments
In file included from ./main.go:3:0:
cgo-builtin-prolog:7:48: error: conflicting types for '_GoString_'
cgo-builtin-prolog:6:44: note: previous declaration of '_GoString_' was here
In file included from ./main.go:3:0:
cgo-gcc-export-header-prolog:27:20: error: 'GoString' redeclared as different kind of symbol
cgo-builtin-prolog:8:12: note: previous declaration of 'GoString' was here

@ALTree ALTree added this to the Go1.12 milestone Aug 16, 2018

@AlexRouSg

This comment has been minimized.

Copy link
Contributor

commented Aug 16, 2018

Possibly related to #22192

@ianlancetaylor ianlancetaylor changed the title cgo: c code that was compiled with go, cannot also be used by go cmd/cgo: c code that was compiled with go, cannot also be used by go Aug 16, 2018

@adam-hanna

This comment has been minimized.

Copy link
Author

commented Aug 16, 2018

@AlexRouSg I think this is a little different. This err is happening because _GoString_ is being defined twice.

It's defined in cmd/cgo/out.go L#1722:

#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H
...
typedef _GoString_ GoString;
...
#endif

But also in the header file ./lib/lib.h L#53:

#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H
...
typedef _GoString_ GoString;
...
#endif

I don't know why the #ifndef GO_CGO_PROLOGUE_H isn't doing the trick. It seems to me like that should prevent this error from happening.

@AlexRouSg

This comment has been minimized.

Copy link
Contributor

commented Aug 16, 2018

@adam-hanna

That issue is generally about c-shared not working (or is not expected to work) with go programs. This is about the generated header defining types twice, so that's why I thought it's related.

@adam-hanna

This comment has been minimized.

Copy link
Author

commented Aug 16, 2018

@AlexRouSg ahh, my apologies. I didn't understand. Thanks!

@gopherbot

This comment has been minimized.

Copy link

commented Nov 30, 2018

Change https://golang.org/cl/152079 mentions this issue: cmd/cgo: use preprocessor macros to avoid prolog redefinitions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.