2

I ran into a third party library that is written in a mixture of C/C++ where most of its headers have a .h filename suffix where a .hpp suffix would be more appropriate.

Using this third party library from cgo should be possible since it makes almost no use of the higher level C++ abstractions, but the header files need to be compiled by a C++ compiler, otherwise the compilation process fails.

I can make cgo work by forcing it to use a C++ compiler for C code like this:

env CC=g++ go build

but as soon as I actually start using the library from go code this approach does not work because cgo generates C code which can not be compiled when CC is set as g++. If I only generate the the _cgo_export.c and _cgo_export.h files with:

env CC=g++ go tool cgo

and then manually compile them with gcc, the compilation works fine.

So my question here is about what options I have to tell cgo about which files to compile as C++ files, and which files to compile as C files?

Is there a way to specifically inform cgo about the wrongly named C++ headers and that it should compile only those files with a C++ instead of a C compiler?

Andreas Raster
  • 642
  • 7
  • 9
  • I don't think that header files get compiled at all by themselves. Header files gets included in some `.c` or `.cpp` or similar files and are only compiled within this context. Thus it is relevant that this context is right. See also [How to use C++ in Go?](https://stackoverflow.com/questions/1713214/how-to-use-c-in-go). – Steffen Ullrich Jun 06 '18 at 16:10
  • 1
    [`cgo` supports compilation of C++ code directly](https://golang.org/cmd/cgo/)—subject to a `cc`-compatible C++ compiler being available, of course—directly, so nothing special is required; just name your source files properly. – kostix Jun 06 '18 at 16:42

0 Answers0