After looking in cgo generated files, I found that in fact function 
“c_test_func” is defined in 2 places.

 

1: ./cgo.cgo2.c:29: int c_test_func(int c1, int c2)

 

#line 3 "/nobackup/sbezverk/projects/go/worspace/cgo_test/cgo.go"

 #include <stdio.h>

 

 extern int go_test_func(int c1, int c2);

 

 int c_test_func(int c1, int c2)

 {

   return go_test_func(c1,c2);

 }

 

2: ./_cgo_export.h:27: int c_test_func(int c1, int c2)

 

#line 3 "cgo.go"

 #include <stdio.h>

 

 extern int go_test_func(int c1, int c2);

 

 int c_test_func(int c1, int c2)

 {

   return go_test_func(c1,c2);

 }

 

_cgo_export.h is included in  ./_cgo_export.c:4:#include "_cgo_export.h". 
Making it a second definition of the same function.

 

 

Is it a bug or what cgo.go does Go calling C func which in turn calls Go is not 
supported? 

 

Thank you

Serguei

 

From: sbezverk <sbezv...@gmail.com>
Date: Friday, 6 October 2023 at 00:13
To: <golang-nuts@googlegroups.com>
Subject: cgo.go: multiple definition of `c_test_func'

 

Hello,

 

I struggle to understand why this simple cgo program does not get linked, I 
would provide example in playground, but it does not seem to support cgo.

 

 

```
package main

 

// #include <stdio.h>

//

// extern int go_test_func(int c1, int c2);

//

// int c_test_func(int c1, int c2)

// {

//   return go_test_func(c1,c2);

// }

import "C"

 

import (

          "fmt"

)

 

//export go_test_func

func go_test_func(c1, c2 C.int) C.int {

          return c1 + c2

}

 

func main() {

          fmt.Printf("Result: %d\n", C.c_test_func(2, 2))

}
```

 

I am getting:

 

# command-line-arguments

/sw/packages/xr/go/1.19.4/pkg/tool/linux_amd64/link: running gcc failed: exit 
status 1

/tmp/go-link-349950461/000001.o: In function `c_test_func':

/nobackup/sbezverk/projects/go/worspace/cgo_test/cgo.go:9: multiple definition 
of `c_test_func'

/tmp/go-link-349950461/000000.o:/tmp/go-build/cgo.go:9: first defined here

collect2: error: ld returned 1 exit status

 

 

Thank you

Serguei

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/46785834-2DF9-44BD-9508-043704C39DA7%40gmail.com.

Reply via email to