How to force cgo to use C++ compiler (g++) instead of gcc ?

1,308 views
Skip to first unread message

Elemer Pixard

unread,
Oct 31, 2021, 1:17:23 PM10/31/21
to golang-nuts
I am developing a binding to a C++ library
using a wrapper module (using extern "C" directive).
The C++ library files are in a subdirectory of the package.

If I create a file in the package directory such as:

lib.cpp
--------------
#include "cpplib/file.cpp"
#include "cpplib/wrapper.cpp"
--------------

It works, but if I create a Go file instead such as:

lib.go
--------------
package lib

// #include "cpplib/file.cpp"
// #include "cpplib/wrapper.cpp"
import "C"
...
--------------

This doesn't compile because cgo uses gcc instead of g++.
I would prefer this second way because I need to
configure the build using tags.
Is there a way to solve this ?

Regards.



Jan Mercl

unread,
Oct 31, 2021, 1:22:47 PM10/31/21
to Elemer Pixard, golang-nuts
On Sun, Oct 31, 2021 at 6:17 PM Elemer Pixard <elp...@gmail.com> wrote:

> Is there a way to solve this ?

Some hopefully related info can be found here:
http://www.swig.org/Doc3.0/Go.html

Andy Polyakov

unread,
Nov 1, 2021, 12:30:30 PM11/1/21
to golang-nuts

lib.go
--------------
package lib

//  #cgo CFLAGS: -x c++
// #include "cpplib/file.cpp"
// #include "cpplib/wrapper.cpp"
import "C"

Formally, the remaining question is if C compilers on all of your target platforms support -x option. In the worst case, if some does not, you should be able to set CC environment variable to call a script that will call C++ compiler behind the curtains.

Andy Polyakov

unread,
Nov 1, 2021, 12:30:36 PM11/1/21
to golang-nuts
Hmm, previous suggestion to use '#cgo CFLAGS: -x c++' doesn't seem to work all the way. [I've only tested class declarations at the time.] Trouble is that '-x c++' is passed even when compiling the glue code, so that even glue symbols get decorated, which doesn't fly with linker at the end. I suppose the only way is to collect extern "C" declarations in a header file, include it in the 'import "C"' section, compile lib.cpp separately and link with it, e.g. with #cgo LDFLAGS: lib.o. Or make it a library and set LDFLAGS to '-L. -lname'...

Ian Lance Taylor

unread,
Nov 1, 2021, 4:13:14 PM11/1/21
to Elemer Pixard, golang-nuts
There is no way to get this to work using #include "cpplib/file.cpp"
in the cgo comment.

I don't understand why the lib.cpp approach doesn't work, though. You
say that it needs tags, but what tags does it need?

Ian

Elemer Pixard

unread,
Nov 2, 2021, 9:23:57 AM11/2/21
to golang-nuts
The objective is to allow the end-user to
configure the library using Go build tags.
For example:

lib.go
---------------------------
package lib

#include "cpplib/common.cpp"
#include "cpplib/wrapper.cpp"
import "C"
---------------------------

backend1.go
---------------------------
// +build backend1

package lib

#include "cpplib/backend1.cpp"
import "C"
---------------------------

backend2.go
---------------------------
// +build backend2

package lib

#include "cpplib/backend2.cpp"
import "C"
---------------------------

So the user  build the library for different backends:
>go install -tags backend1
or
>go install -tags backend2

Ian Lance Taylor

unread,
Nov 2, 2021, 4:29:07 PM11/2/21
to Elemer Pixard, golang-nuts
Why not just put a build tag in your lib.cpp files?

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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/ba041b6b-764c-4b30-90cb-c2cc28479935n%40googlegroups.com.

Elemer Pixard

unread,
Nov 2, 2021, 7:33:06 PM11/2/21
to golang-nuts
I didn't even tried because I assumed that build tags were processed only  for ".go" files.
But it worked !!!
Thanks !
Reply all
Reply to author
Forward
0 new messages