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: example for calling Go from C not working #28471

Closed
reijin90 opened this issue Oct 29, 2018 · 5 comments

Comments

@reijin90
Copy link

commented Oct 29, 2018

Please answer these questions before submitting your issue. Thanks!

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

go version go1.10.1 linux/amd64

Does this issue reproduce with the latest release?

not tested

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/master/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/master/go"
GORACE=""
GOROOT="/usr/lib/go-1.10"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.10/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-build757128041=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I tried to follow the example code from here:
https://github.com/golang/go/wiki/cgo#calling-go-functions-from-c
created foo.c and foo.go in a directory with copy&pasted code.
Ran go build -a foo.go

What did you expect to see?

Successful compilation

What did you see instead?

# command-line-arguments
/tmp/go-build805347205/b001/_x002.o: In function `_cgo_cf4738fab0e4_Cfunc_ACFunction':
/tmp/go-build/cgo-gcc-prolog:37: undefined reference to `ACFunction'
collect2: error: ld returned 1 exit status

Edit: I've sucessfully created a PoC code for calling C with Go with the sparse info I could find. Calling Go from C however seems to be even less documented, which makes it even more frustrating, when even the basic examples in the documenation are not working. I volunteer to make an edit if we can find a proper solution to this issue as I feel newbies after me will have the same problems.

@ianlancetaylor ianlancetaylor changed the title CGO example for calling Go from C not working cmd/cgo: example for calling Go from C not working Oct 29, 2018

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Oct 29, 2018

What example are you taking about? Please show us your code.

It sounds like your code was wrong, and that you fixed it. So I'm not sure what concrete step the Go project should take.

@reijin90

This comment has been minimized.

Copy link
Author

commented Oct 29, 2018

What example are you taking about? Please show us your code.

It sounds like your code was wrong, and that you fixed it. So I'm not sure what concrete step the Go project should take.

Hi!
Sorry, I was not clear here. The code I tested was this: https://github.com/golang/go/wiki/cgo#calling-go-functions-from-c

Regarding your question: No, I did not make any changes to this particular code.

@slrz

This comment has been minimized.

Copy link

commented Oct 29, 2018

The undefined reference to 'ACFunction' error message is exactly what I'd expect given that you're compiling the foo.go file but not the foo.c one.

Specify the package instead of a single file and things will work.

@reijin90

This comment has been minimized.

Copy link
Author

commented Oct 29, 2018

The undefined reference to ACFunction'` error message is exactly what I'd expect given that you're compiling the foo.go file but not the foo.c one.

Specify the package instead of a single file and things will work.

Well, go build doesn't throw an error, but I can't find an executable...?

master@thesis:~/go/src/gocallback$ ll
total 16
drwxrwxr-x 2 master master 4096 Okt 29 19:42 ./
drwxrwxr-x 3 master master 4096 Okt 29 19:42 ../
-rw-rw-r-- 1 master master   92 Okt 29 19:42 foo.c
-rw-rw-r-- 1 master master  207 Okt 29 19:42 foo.go
master@thesis:~/go/src/gocallback$ go build gocallback
master@thesis:~/go/src/gocallback$ ll
total 16
drwxrwxr-x 2 master master 4096 Okt 29 19:42 ./
drwxrwxr-x 3 master master 4096 Okt 29 19:42 ../
-rw-rw-r-- 1 master master   92 Okt 29 19:42 foo.c
-rw-rw-r-- 1 master master  207 Okt 29 19:42 foo.go
master@thesis:~/go/src/gocallback$ go run gocallback
go run: no go files listed
master@thesis:~/go/src/gocallback$ go run gocallback ./
go run: no go files listed
master@thesis:~/go/src/gocallback$ go run gocallback ./foo.go
go run: no go files listed
master@thesis:~/go/src/gocallback$ go run package gocallback
go run: no go files listed
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Oct 29, 2018

Calling go build of a package doesn't produce anything in the current directory, as documented at https://golang.org/cmd/go/#hdr-Compile_packages_and_dependencies . The fact that go build succeeded tells you that all is well. If you want to actually use the package, you need to write a main package somewhere else.

It doesn't sound like there is anything for the Go project to change here, so I'm going to close this issue. Please comment if you disagree.

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