You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
can't build with both inline C function define andGo function export
code:
package main
//
// int add(int a, int b) {
// int ret = a + b;
// return ret;
// }
//
// extern void foo();
//
import "C"
import (
"fmt"
)
//export foo
func foo() {
}
func main() {
var n int32
n = int32(C.add(C.int(2), C.int(3)))
fmt.Println(n)
}
got compile error:
# GOROOT=/opt/go-1.4.2 /opt/go-1.4.2/bin/go build cgo.go
# command-line-arguments
/tmp/go-build570506490/command-line-arguments/_obj/cgo.cgo2.o: In function `add':
./cgo.go:5: multiple definition of `add'
/tmp/go-build570506490/command-line-arguments/_obj/_cgo_export.o:/root/test/testgoc/cgo.go:5: first defined here
collect2: error: ld returned 1 exit status
cgo writes the inline C functions both in cgo.cgo2.c and _cgo_export.h
The text was updated successfully, but these errors were encountered:
This is working as intended and it's documented.
http://golang.org/cmd/cgo/#hdr-C_references_to_Go
Using //export in a file places a restriction on the preamble: since it is
copied into two different C output files, it must not contain any
definitions, only declarations. Definitions must be placed in preambles in
other files, or in C source files.
A trick is that if you only declare functions in the cgo preamble, then
you can label the function static.
mikioh
changed the title
can't build with both inline C function define andGo function export
cmd/cgo: can't build with both inline C function define and Go function export
May 29, 2015
can't build with both inline C function define andGo function export
code:
got compile error:
cgo writes the inline C functions both in cgo.cgo2.c and _cgo_export.h
The text was updated successfully, but these errors were encountered: