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: document preamble/export restriction #3497

Closed
gopherbot opened this issue Apr 7, 2012 · 5 comments

Comments

@gopherbot
Copy link

commented Apr 7, 2012

by wilson.oliveira:

What steps will reproduce the problem?

Expose a go function to C with the //export method as shown here -
http://code.google.com/p/go-wiki/wiki/cgo
Sample code that fails to link with this error - http://play.golang.org/p/HCA_OyEDr4

What do you see instead?

> go build callback.go
> # command-line-arguments
> ...\_obj\callback.cgo2.o: In function `ACFunction':
> ./callback.go:9: multiple definition of `ACFunction'
> ...\_obj\_cgo_export.o:callback.go:9: first defined here
> collect2: ld returned 1 exit status

Which compiler are you using (5g, 6g, 8g, gccgo)?

8g

Which operating system are you using?

Windows 7 32 bits

Which version are you using?  (run 'go version')

go version go1

Please provide any additional information below.

I only tested it on windows.
@alexbrainman

This comment has been minimized.

Copy link
Member

commented Apr 10, 2012

Comment 1:

If I split your file into two, it works:
# ls
a.go  b.go
# cat a.go
package main
/*
#include <stdio.h>
extern void AGoFunction();
void ACFunction() {
        printf("ACFunction()\n");
        AGoFunction();
}
*/
import "C"
func main() {
        C.ACFunction()
}
# cat b.go
package main
import "fmt"
import "C"
//export AGoFunction
func AGoFunction() {
        fmt.Println("AGoFunction()")
}
# go run a.go b.go
ACFunction()
AGoFunction()
#
I think, your program is breaking rule described in
http://golang.org/doc/articles/c_go_cgo.html:
"... There is a limitation: if your program uses any //export directives, then the C
code in the comment may only include declarations (extern int f();), not definitions
(int f() { return 1; }).  ...".
It further says "... The #cgo and //export directives are documented in the cgo
documentation.", but I couldn't find any info about "export directive limitation" in
http://golang.org/cmd/cgo/. So, perhaps, documentation is lacking.
Also, example in http://code.google.com/p/go-wiki/wiki/cgo is, obviously, wrong too.
Leaving for others to decide what to do.
Alex

Labels changed: added cgo.

@gopherbot

This comment has been minimized.

Copy link
Author

commented Apr 10, 2012

Comment 2 by wilson.oliveira:

The sugested workarround works fine for me. Maybe it should be described together with
the limitation warning.
Thanks,
Wilson
@rsc

This comment has been minimized.

Copy link
Contributor

commented Apr 10, 2012

Comment 3:

The restriction is new in Go 1, and it is mentioned at
http://golang.org/doc/go1.html#cmd_cgo, but we neglected to add it to the cgo doc proper.

Labels changed: added priority-later, documentation, go1.1, removed priority-triage.

Status changed to Accepted.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Dec 10, 2012

Comment 4:

Labels changed: added size-s.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Feb 1, 2013

Comment 5:

This issue was closed by revision ed9fc75.

Status changed to Fixed.

@rsc rsc added this to the Go1.1 milestone Apr 14, 2015

@rsc rsc removed the go1.1 label Apr 14, 2015

@golang golang locked and limited conversation to collaborators Jun 24, 2016

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