how to distribute cgo packages?

581 views
Skip to first unread message

Peter Kleiweg

unread,
Apr 10, 2012, 3:42:44 PM4/10/12
to golan...@googlegroups.com

I know I can put all C code as a comment in a Go file, directly
above 'import "C"', but for large C files, I don't think that is
practical.

And what about C++? I have code in C++, but all functions are
available to C. Only the code inside the functions is C++, and
the header file is pure C. So I can compile this separately into
a library, and link it with Go, and it works.

Is there a way to have Go and C (and C++) files in one package
that can be installed with just "go get packagename"?

minux

unread,
Apr 10, 2012, 3:46:51 PM4/10/12
to Peter Kleiweg, golan...@googlegroups.com
On Wed, Apr 11, 2012 at 3:42 AM, Peter Kleiweg <pkle...@xs4all.nl> wrote:

I know I can put all C code as a comment in a Go file, directly
above 'import "C"', but for large C files, I don't think that is
practical.
If one go source file imports "C", all .c file in that directory will automatically be
treated as gcc source code files.
That is, you are free to put your c code in another .c file, as long as your pkg
uses imports "C" in one of the go files.

But, cmd/go won't recognize .cc or .cpp for g++, so you have to devise another
way to bundle your C++ source file.
(This is because in general cgo doesn't support C++.)

André Moraes

unread,
Apr 10, 2012, 3:47:40 PM4/10/12
to Peter Kleiweg, golan...@googlegroups.com
> Is there a way to have Go and C (and C++) files in one package
> that can be installed with just "go get packagename"?

Take a look at:

https://github.com/mattn/go-v8

It compiles a small c++ library exposing a C interface and then use
CGO to compile the go package using the created C library.

I don't tested this package yet so don't know if it will compile in
the latest release.

"go get ...." can handle custom build steps, for such cases make is
your friend :)

--
André Moraes
http://andredevchannel.blogspot.com/

Ugorji Nwoke

unread,
Apr 10, 2012, 3:50:23 PM4/10/12
to golan...@googlegroups.com, Peter Kleiweg
Ian has a pending change previously to support swig in the go tool.

If/When this goes in, will it handle the OP's use-case?
(I'm not really familiar with Swig so didn't look much into it).

minux

unread,
Apr 10, 2012, 3:50:47 PM4/10/12
to André Moraes, Peter Kleiweg, golan...@googlegroups.com


2012/4/11 André Moraes <and...@gmail.com>

> Is there a way to have Go and C (and C++) files in one package
> that can be installed with just "go get packagename"?

Take a look at:

https://github.com/mattn/go-v8

It compiles a small c++ library exposing a C interface and then use
CGO to compile the go package using the created C library.

I don't tested this package yet so don't know if it will compile in
the latest release.
We don't have gomake in Go 1. 
 
"go get ...." can handle custom build steps, for such cases make is
your friend :)
How? 

André Moraes

unread,
Apr 10, 2012, 3:55:31 PM4/10/12
to minux, Peter Kleiweg, golan...@googlegroups.com
>> "go get ...." can handle custom build steps, for such cases make is
>> your friend :)
>
> How?

Typo here, the correct is:

"go get ...." can't handle custom build steps, for such cases make is
your friend :)

--
André Moraes
http://andredevchannel.blogspot.com/

André Moraes

unread,
Apr 10, 2012, 3:57:50 PM4/10/12
to minux, Peter Kleiweg, golan...@googlegroups.com
>> https://github.com/mattn/go-v8
>>
>> It compiles a small c++ library exposing a C interface and then use
>> CGO to compile the go package using the created C library.
>>
>> I don't tested this package yet so don't know if it will compile in
>> the latest release.
>
> We don't have gomake in Go 1.

The Makefile don't use anything special that would require gomake, so
the user can call vanilla "make" and build the required library.

minux

unread,
Apr 10, 2012, 4:07:15 PM4/10/12
to André Moraes, Peter Kleiweg, golan...@googlegroups.com


2012/4/11 André Moraes <and...@gmail.com>

>> https://github.com/mattn/go-v8
>>
>> It compiles a small c++ library exposing a C interface and then use
>> CGO to compile the go package using the created C library.
>>
>> I don't tested this package yet so don't know if it will compile in
>> the latest release.
>
> We don't have gomake in Go 1.

The Makefile don't use anything special that would require gomake, so
the user can call vanilla "make" and build the required library.
its Makefile uses GOOS which normally isn't defined, it should include this to be free from gomake:
GOOS:=$(shell go env GOOS)

Peter Kleiweg

unread,
Apr 10, 2012, 4:12:49 PM4/10/12
to golang-nuts
On Apr 10, 9:57 pm, André Moraes <andr...@gmail.com> wrote:
> The Makefile don't use anything special that would require gomake, so
> the user can call vanilla "make" and build the required library.

So, if there is C++ code in a package, it would need to get installed
like this:

go get -d packagename
cd $GOPATH/src/packagename
make install

With Go and just C, this should work:

go get packagename

André Moraes

unread,
Apr 10, 2012, 4:20:25 PM4/10/12
to Peter Kleiweg, golang-nuts
>
> So, if there is C++ code in a package, it would need to get installed
> like this:
>
>    go get -d packagename
>    cd $GOPATH/src/packagename
>    make install
>
> With Go and just C, this should work:
>
>    go get packagename

Yes

Ian Lance Taylor

unread,
Apr 11, 2012, 1:26:56 AM4/11/12
to Ugorji Nwoke, golan...@googlegroups.com, Peter Kleiweg
Ugorji Nwoke <ugo...@gmail.com> writes:

> Ian has a pending change previously to support swig in the go tool.
> https://groups.google.com/d/msg/golang-dev/4Kaa2FahPrQ/huS0JNKMiicJ
> http://codereview.appspot.com/4287056/
>
> If/When this goes in, will it handle the OP's use-case?

I don't think so.

Ian

Reply all
Reply to author
Forward
0 new messages