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

gopherbot opened this issue Apr 7, 2012 · 5 comments


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 -
Sample code that fails to link with this error -

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


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.

This comment has been minimized.

Copy link

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() {
import "C"
func main() {
# cat b.go
package main
import "fmt"
import "C"
//export AGoFunction
func AGoFunction() {
# go run a.go b.go
I think, your program is breaking rule described in
"... 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 So, perhaps, documentation is lacking.
Also, example in is, obviously, wrong too.
Leaving for others to decide what to do.

Labels changed: added cgo.


This comment has been minimized.

Copy link

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.

This comment has been minimized.

Copy link

commented Apr 10, 2012

Comment 3:

The restriction is new in Go 1, and it is mentioned at, 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.


This comment has been minimized.

Copy link

commented Dec 10, 2012

Comment 4:

Labels changed: added size-s.


This comment has been minimized.

Copy link

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.
None yet
3 participants
You can’t perform that action at this time.