CGO and STL containers.

798 views
Skip to first unread message

Bruno Albuquerque

unread,
May 9, 2015, 3:53:40 PM5/9/15
to golang-nuts
I have this C header file that includes another header file that includes <map> and <vector>. I do not use either directly, but CGO does not even let me compile the code as it can not find <map>.

$ go build .

# bga/test

In file included from api.h:1:0,

                 from ./test.go:6:

context.h:8:15: fatal error: map: No such file or directory

 #include <map>

               ^

compilation terminated.

Is there any way around this?


Lars Seipel

unread,
May 9, 2015, 7:26:28 PM5/9/15
to Bruno Albuquerque, golang-nuts
On Sat, May 09, 2015 at 07:53:29PM +0000, Bruno Albuquerque wrote:
> I have this C header file that includes another header file that includes
> <map> and <vector>. I do not use either directly, but CGO does not even let
> me compile the code as it can not find <map>.

Don't include C++ headers in C code? This seems broken. Otherwise you're
actually dealing with C++ code and need to use a C++ compiler. If you're
sure the library provides the functions you want (and does so with C
linkage) you could just write a matching header file yourself and feed
that to cgo.

What is it that you want to call?

minux

unread,
May 9, 2015, 8:28:02 PM5/9/15
to Bruno Albuquerque, golang-nuts
Put the code that uses the C++ header files in a separate C++ source file (*.cc or *.cpp),
and create another C header file (with #if __cplusplus guarded extern "C") that is included
by both the C++ file and cgo preamble.

Cgo compiles the code in the cgo preamble with a C compiler, because cgo is a way to
interface with C code, not directly to C++ code.
Reply all
Reply to author
Forward
0 new messages