cgo: what is the GoInterface, GoInt for in _cgo_export.h?

1,463 views
Skip to first unread message

施健

unread,
Oct 20, 2015, 7:35:30 AM10/20/15
to golang-nuts
As I know, that I can not declare a C func with GoInterface or GoInt parameter and called by go. But I can export go func to C with GoInterface parameter. If so, I can never get a GoInterface type, because I cannot build a GoInterface type in C code. And I cannot get them form go-call-c. So what is these types defined in _cgo_export.h for? 

If I am wrong, is there a way to declare a C func and export to go with GoInterface type?

here is the simple code.
a.h
void *SomeFunc(GoInterface arg);

a.c
void *SomeFunc(GoInterface arg) {
}

a.go
package main

// #include "a.h"
import "C"

type A struct {
}

func main() {
    var a = new(A)
}

go build
cc errors for preamble:
In file included from ./a.go:3:0:
a.h:1:16: error: unknown type name 'GoInterface'
 void *SomeFunc(GoInterface arg)

I know I can use the void * to hold unsafe.Pointer. I just try to find a better way. this confused me a few days. Any help will be thankful. 

Ian Lance Taylor

unread,
Oct 20, 2015, 10:02:34 AM10/20/15
to 施健, golang-nuts
On Tue, Oct 20, 2015 at 4:35 AM, 施健 <shiji...@gmail.com> wrote:
>
> As I know, that I can not declare a C func with GoInterface or GoInt
> parameter and called by go. But I can export go func to C with GoInterface
> parameter. If so, I can never get a GoInterface type, because I cannot build
> a GoInterface type in C code. And I cannot get them form go-call-c. So what
> is these types defined in _cgo_export.h for?

Those types are used internally by cgo. They aren't documented and
they aren't intended for use by the program using cgo.

What are you really trying to do?

Ian

alex....@gmail.com

unread,
Nov 22, 2015, 11:01:55 PM11/22/15
to golang-nuts, shiji...@gmail.com
For example. I compile my lib: go build -buildmode=c-archive sp.go. It let me link my static library to project on C. 
Golang generate sp.a and sp.h files. Inside sp.h you can see lines:
//...
typedef struct { char *p; GoInt n; } GoString;
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
//...
Byt I need to know how to use this types in C.  

вівторок, 20 жовтня 2015 р. 21:02:34 UTC+7 користувач Ian Lance Taylor написав:

Ian Lance Taylor

unread,
Nov 22, 2015, 11:20:49 PM11/22/15
to alex....@gmail.com, golang-nuts, 施健
On Sat, Nov 21, 2015 at 10:10 PM, <alex....@gmail.com> wrote:
> For example. I compile my lib: go build -buildmode=c-archive sp.go. It let
> me link my static library to project on C.
> Golang generate sp.a and sp.h files. Inside sp.h you can see lines:
> //...
> typedef struct { char *p; GoInt n; } GoString;
> typedef void *GoMap;
> typedef void *GoChan;
> typedef struct { void *t; void *v; } GoInterface;
> typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
> //...
> Byt I need to know how to use this types in C.

I'm sorry, to me it seems like you restated your question rather than
answering mine. What are you really trying to do?

Ian


> вівторок, 20 жовтня 2015 р. 21:02:34 UTC+7 користувач Ian Lance Taylor
> написав:
>>
>> On Tue, Oct 20, 2015 at 4:35 AM, 施健 <shiji...@gmail.com> wrote:
>> >
>> > As I know, that I can not declare a C func with GoInterface or GoInt
>> > parameter and called by go. But I can export go func to C with
>> > GoInterface
>> > parameter. If so, I can never get a GoInterface type, because I cannot
>> > build
>> > a GoInterface type in C code. And I cannot get them form go-call-c. So
>> > what
>> > is these types defined in _cgo_export.h for?
>>
>> Those types are used internally by cgo. They aren't documented and
>> they aren't intended for use by the program using cgo.
>>
>> What are you really trying to do?
>>
>> Ian
>
> --
> 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...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

alex....@gmail.com

unread,
Nov 23, 2015, 1:30:57 AM11/23/15
to golang-nuts, alex....@gmail.com, shiji...@gmail.com
> you restated your question
Question not mine.
I just said that those types are not used only internally by cgo. Because golang generate similar code from my exported function:
t.go
//export testFunc
func testFunc
() (a interface{}) {
 a
= 100
 
return
}
t.h
typedef struct { void *t; void *v; } GoInterface;
// ...
extern GoInterface testFunc();
It looks as this is internal type? I do not think so.

> What are you really trying to do? 
I use static librares (writen on Go) in C projects like nginx modules. And I do it successfully. But I have to convert previously my Go interfaces to C structs in go code.
I'm interested in the possibility using Go types as a result of my methods writen on go.   

ps. sorry for my English)

понеділок, 23 листопада 2015 р. 11:20:49 UTC+7 користувач Ian Lance Taylor написав:

Ian Lance Taylor

unread,
Nov 23, 2015, 10:17:37 AM11/23/15
to alex....@gmail.com, golang-nuts, 施健
On Sun, Nov 22, 2015 at 10:30 PM, <alex....@gmail.com> wrote:
>> you restated your question
> Question not mine.
> I just said that those types are not used only internally by cgo. Because
> golang generate similar code from my exported function:
> t.go
> //export testFunc
> func testFunc() (a interface{}) {
> a = 100
> return
> }
> t.h
> typedef struct { void *t; void *v; } GoInterface;
> // ...
> extern GoInterface testFunc();
> It looks as this is internal type? I do not think so.

Yes, that is true. For an exported function you can wind up with a
function declaration that uses these types. It's the only way for cgo
to describe what the function is doing.


>> What are you really trying to do?
> I use static librares (writen on Go) in C projects like nginx modules. And I
> do it successfully. But I have to convert previously my Go interfaces to C
> structs in go code.
> I'm interested in the possibility using Go types as a result of my methods
> writen on go.

You can't use a Go interface type in C. In fact, in the upcoming Go
1.6 release, an exported Go function won't even be able to return a
normal Go interface type. See the discussion at
https://golang.org/issue/12416 .

Ian
Reply all
Reply to author
Forward
0 new messages