> On Thu, Apr 30, 2015 at 5:02 PM, Ian Lance Taylor <
ia...@golang.org> wrote:
>> The new -buildmode=c-archive and -buildmode=c-shared options create an
>> archive or shared library that is callable from C code. The only
>> functions that are callable are those that are marked with //export in
>> files that import "C", as described in the cgo documentation.
>>
>> The only purpose to using these options is to then write C (or C++,
>> etc.) code that will call these functions. The C code will need a
>> function declaration, implicit or explicit, to make the call. That
>> declaration will have to match the function in the Go code. Currently
>> there is no way to check that those declarations match.
>>
>> The cgo tool already generates a header file that contains exactly the
>> declarations that we need. I propose that when the package a/b/c is
>> built for -buildmode=c-archive or -buildmode=c-shared, we install that
>> header file in $GOROOT/pkg/$GOOS_$GOARCH/a/b/c.h. That is, the header
>> file will appear right next to the .a file.
>
> I guess you mean GOPATH, not GOROOT? If so, sounds reasonable.
Yes, GOPATH, sorry.
> But right now -buildmode=c-archive produces an archive file like the
> go command does a binary. That is, it puts it in the current working
> directory, not $GOPATH/pkg. Would you change it to put it in both? Or
> just the pkg directory? And if just the pkg directory, what would be
> the effect of -o?
Yes, I should have mentioned this issue. The problem is that if there
are multiple packages that export symbols, you can't reliably combine
the header files. The header files must contain the cgo preamble
comment, which will define types that the exported function may refer
to. There is no reason to expect that the preamble of multiple
packages is compatible. So you need one header file per package, so
you can't use the -o option to decide where to put them.
The header files can also depend on tags, which means they can depend
on GOOS/GOARCH. So they need to go in some directory heirarchy that
depends on GOOS/GOARCH, and we already have that under pkg.
Ian