calling go functions from c

173 views
Skip to first unread message

webus...@gmail.com

unread,
Sep 29, 2016, 11:59:46 PM9/29/16
to golang-nuts
I have a basic example working of calling back into go from C. If I have a C function as:

func Add(a, b C.Int) C.Int {
return a + b
}

Where is the mapping of C type to C.<type> when I want to pass additional types to Go :

1. Unsigned int
2. char
3. double
4. string
5. short
etc...


Ian Lance Taylor

unread,
Sep 30, 2016, 1:27:13 AM9/30/16
to webus...@gmail.com, golang-nuts
I'm sorry, I don't understand the question. The C type int maps to
the Go type C.int.

Ian

webus...@gmail.com

unread,
Sep 30, 2016, 1:42:16 AM9/30/16
to golang-nuts, webus...@gmail.com
Thanks for reading my question. I'm trying to map c <type> to Go "C.<type>"

C int -> go C.int
C unsigned char -> ?
C char -> ?
C unsigned short -> ?
C double -> ?
C __int64 -> ?
C *char -> C.string

Seb Binet

unread,
Sep 30, 2016, 3:52:55 AM9/30/16
to webus...@gmail.com, golang-nuts
On Fri, Sep 30, 2016 at 7:42 AM, <webus...@gmail.com> wrote:
Thanks for reading my question. I'm trying to map c <type> to Go "C.<type>"

C int -> go C.int
C unsigned char -> ?
C char -> ?
C unsigned short -> ?
C double -> ?
C __int64 -> ?
C *char -> C.string

this won't work from within the playground, but here you go:


hth,
-s

webus...@gmail.com

unread,
Oct 1, 2016, 8:17:01 AM10/1/16
to golang-nuts, webus...@gmail.com
Thanks for putting this together. I was trying to figure out how to do callback from c to go. There are fewer examples on the web for that since most of them deal with calling c code from go. 

C int -> go C.int
C unsigned char -> C.uchar
C char -> C.char
C unsigned int -> C.uint
C double -> C.double
C *char -> *C.char

To get a go string from *C.char I used C.GoString(symbol). 

Couple of followup questions:

- I used the int and char C.<type> directly in fmt.printf and it seemed to work. Is that ok to do without having to convert into a native go type?
- What about c types like 

C short -> ?
C __int64 -> ?

What file are all the go C.<types> defined in. 

Thanks to the go team for making this. It's so easy to work with the binary c lib that I have and I'm actually going to enjoy it...

web user

unread,
Oct 1, 2016, 8:27:19 AM10/1/16
to golang-nuts
On a side not. The functions I call will from c into go will be called very frequently and the overhead of a callback from c might become an issue. I've heard that overhead can be as high as 200ns. Couple of questions:

1. Is the overhead not there when using gccgo?
2. What is the current state of gccgo support? Does it fully support 1.7 on linux?
3. Would the command that I currently use:

go build -buildmode c-archive -o libgolang.a

be the same on gccgo?



Ian Lance Taylor

unread,
Oct 1, 2016, 12:48:11 PM10/1/16
to webus...@gmail.com, golang-nuts
On Sat, Oct 1, 2016 at 5:17 AM, <webus...@gmail.com> wrote:
>
> - I used the int and char C.<type> directly in fmt.printf and it seemed to
> work. Is that ok to do without having to convert into a native go type?

Yes.

> - What about c types like
>
> C short -> ?
> C __int64 -> ?
>
> What file are all the go C.<types> defined in.

For C types that are simple identifiers, the cgo tool simply uses that
same identifier. For C types that are multiple identifiers, see
dwarfToName in cmd/cgo/gcc.go.

Ian

Ian Lance Taylor

unread,
Oct 1, 2016, 12:50:05 PM10/1/16
to web user, golang-nuts
On Sat, Oct 1, 2016 at 5:23 AM, web user <webus...@gmail.com> wrote:
> On a side not. The functions I call will from c into go will be called very
> frequently and the overhead of a callback from c might become an issue. I've
> heard that overhead can be as high as 200ns. Couple of questions:
>
> 1. Is the overhead not there when using gccgo?

There is some overhead when using gccgo, because of the need to
interact with the Go scheduler. The overhead should be slightly less.

> 2. What is the current state of gccgo support? Does it fully support 1.7 on
> linux?

The current development branch of gccgo fully supports 1.7. However,
that is not in any GCC release, due to the different release timing
between GCC releases and Go releases. If you build GCC yourself from
tip, you will get a version of gccgo with full 1.7 support.

> 3. Would the command that I currently use:
>
> go build -buildmode c-archive -o libgolang.a
>
> be the same on gccgo?

Yes.

Ian

webus...@gmail.com

unread,
Oct 2, 2016, 5:37:19 PM10/2/16
to golang-nuts, webus...@gmail.com
There is some overhead when using gccgo, because of the need to
interact with the Go scheduler.  The overhead should be slightly less.

Any idea what (very rough is fine) that overhead is when compared to the standard go toolchain?



 

Ian Lance Taylor

unread,
Oct 3, 2016, 2:31:23 PM10/3/16
to web user, golang-nuts
I've never measured it, sorry. I don't have any idea.

Ian
Reply all
Reply to author
Forward
0 new messages