Best way to deal with namespaces on cgo

263 views
Skip to first unread message

Alonso Vidales

unread,
Nov 21, 2015, 12:28:53 PM11/21/15
to golan...@googlegroups.com
Hello,
I’m working on a project that makes usage of some C++ libraries, this libraries use namespaces like:

namespace ns {
void hello();
}

All compiles, and I can use the functions out of namespaces, but I can’t find docu about how to work with namespaces in order to access to this method from Go.
My first idea was to use a small wrapper using the cgo comments like:
package stuff

// #import <some_libs.h>
/*
void hello() {
ns::hello();
}
*/
import “C”

But this fails with an:
error: expected expression before ‘:'
another option is to prepare the wrapper out of the .go file, but this is going to make a bit more complicated the installation, etc of the libraries.
What is the best approach?, can I just call to this method using the C lib? something like: C.ns.hello()??

Thanks!,

Ian Lance Taylor

unread,
Nov 21, 2015, 4:34:44 PM11/21/15
to Alonso Vidales, golang-nuts
At present cgo only understands C, not C++. You need to use extern
"C" and give C names to the functions outside of cgo. For example,
put a export.cc file in your Go package directory that has extern "C"
wrappers. Then you can refer to those wrappers using cgo.

Ian

Alonso Vidales

unread,
Nov 21, 2015, 5:22:50 PM11/21/15
to Ian Lance Taylor, golang-nuts
Ahh, ok, I was forgetting about the extern “C”, thanks a lot!
Reply all
Reply to author
Forward
0 new messages