Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: does not support c++ (use swig) #1476

Closed
alberts opened this issue Feb 3, 2011 · 14 comments

Comments

@alberts
Copy link
Contributor

commented Feb 3, 2011

What steps will reproduce the problem?

I have some code I want to build in C++ mode but with extern "C" linkage.
However, using -x c++ with gcc breaks some of my other code.

After a make nuke, I build with:

CGO_CFLAGS=-x c++

and then I get:

cmsg.go: In function 'void _cgo_1a2aead0dab6_Cfunc_CMSG_DATA_(void*)':
cmsg.go:32:9: error: invalid conversion from 'void*' to
'_cgo_1a2aead0dab6_Cfunc_CMSG_DATA_(void*)::<anonymous struct>*' [-fpermissive]
cmsg.go: In function 'void _cgo_1a2aead0dab6_Cfunc_CMSG_LEN_(void*)':
cmsg.go:42:9: error: invalid conversion from 'void*' to
'_cgo_1a2aead0dab6_Cfunc_CMSG_LEN_(void*)::<anonymous struct>*' [-fpermissive]
cmsg.go: In function 'void _cgo_1a2aead0dab6_Cfunc_CMSG_SPACE_(void*)':
cmsg.go:52:9: error: invalid conversion from 'void*' to
'_cgo_1a2aead0dab6_Cfunc_CMSG_SPACE_(void*)::<anonymous struct>*' [-fpermissive]
make: *** [cmsg.cgo2.o] Error 1

Then with a make nuke and:

CGO_CFLAGS=-x c++ -fpermissive

I get:

cmsg.go:54:13: call of non-function C.CMSG_SPACE_
cmsg.go:54:27: call of non-function C.size_t
cmsg.go:59:13: call of non-function C.CMSG_LEN_
cmsg.go:59:25: call of non-function C.size_t
cmsg.go:64:13: call of non-function C.cmsghdr_t
cmsg.go:65:32: call of non-function C.CMSG_DATA_
make: *** No rule to make target `_cgo_run', needed by `_cgo_flags'.  Stop.

If I first build with 

CGO_CFLAGS=-x c++

until I get an error and then rebuild with

CGO_CFLAGS=-x c++ -fpermissive

without doing a make nuke in between, it builds. Seems like the -fpermissive build
should just work.

Which compiler are you using (5g, 6g, 8g, gccgo)?

6g

Which operating system are you using?

linux

Which revision are you using?  (hg identify)

tip

gcc (GCC) 4.6.0 20110120 (experimental)

Attachments:

  1. cmsg.go (1725 bytes)
  2. Makefile (139 bytes)
  3. cmsg.c (367 bytes)
@robpike

This comment has been minimized.

Copy link
Contributor

commented Feb 3, 2011

Comment 1:

Cgo works with C, not C++. Isn't that the issue?

Status changed to WaitingForReply.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Feb 3, 2011

Comment 2:

Cgo does not support C++.
If you'd like to add support for this kind of basic
stuff, that would be great.
But it's not super high on our priority list.
The suggested way to wrap C++ is to use SWIG.

Owner changed to r...@golang.org.

Status changed to HelpWanted.

@alberts

This comment has been minimized.

Copy link
Contributor Author

commented Feb 4, 2011

Comment 3:

Thanks. The function implementations might be C++, but the function interface is C
(because of the extern "C" bit).
@rsc

This comment has been minimized.

Copy link
Contributor

commented Feb 4, 2011

Comment 4:

Cgo does not support CGO_CFLAGS=-x c++.
It depends on a very specific way of invoking gcc, and it expects
to find a C compiler when it does that.
@alberts

This comment has been minimized.

Copy link
Contributor Author

commented Feb 7, 2011

Comment 5:

I've figured it out.
The trick is to have a extern "C" header file without code and then include the C++ code
via CGO_OFILES.
Example attached.

Attachments:

  1. murmur3.tar.gz (2880 bytes)
@alberts

This comment has been minimized.

Copy link
Contributor Author

commented Mar 2, 2011

Comment 6:

If anyone plays with this in future, keep issue #1500 in mind.
@rsc

This comment has been minimized.

Copy link
Contributor

commented Oct 6, 2011

Comment 7:

Labels changed: added priority-low, removed priority-medium.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Dec 9, 2011

Comment 8:

Labels changed: added priority-someday, removed priority-low.

@alberts

This comment has been minimized.

Copy link
Contributor Author

commented Apr 7, 2012

Comment 10:

Some discussion on stackoverflow:
http://stackoverflow.com/questions/1713214/how-to-use-c-in-go/1721230
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Apr 7, 2012

Comment 11:

Adding SWIG support to the go tool: http://golang.org/cl/5845071/
@rsc

This comment has been minimized.

Copy link
Contributor

commented Sep 12, 2012

Comment 12:

Possibly 4069 would help.
@rsc

This comment has been minimized.

Copy link
Contributor

commented Mar 11, 2013

Comment 13:

SWIG is the answer for full C++.

Status changed to WorkingAsIntended.

@alberts

This comment has been minimized.

Copy link
Contributor Author

commented Mar 11, 2013

Comment 14:

FWIW, for my test case (CityHash) the latest go build -ldflags '-w -hostobj' worked
perfectly for building a bunch of C++ .sysos (where one has some extern "C" functions)
into a Go binary.
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented May 31, 2013

Comment 15:

This issue was closed by revision 8448536.

Status changed to Fixed.

@alberts alberts added fixed labels May 31, 2013

@golang golang locked and limited conversation to collaborators Jun 24, 2016

This issue was closed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
5 participants
You can’t perform that action at this time.