├── Makefile ├── README.md ├── cfoo.cpp ├── foo.cpp ├── foo.go ├── foo.h └── foo.hpp /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: clean 2 | 3 | TARGET=howto-go-with-cpp 4 | 5 | $(TARGET): libfoo.a 6 | go build . 7 | 8 | libfoo.a: foo.o cfoo.o 9 | ar r $@ $^ 10 | 11 | %.o: %.cpp 12 | g++ -O2 -o $@ -c $^ 13 | 14 | clean: 15 | rm -f *.o *.so *.a $(TARGET) 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Wrapping C++ with Go 2 | 3 | This is a minimal example of how to wrap a C++ class manually with Go. 4 | 5 | I borrowed most of the code from [a stack overflow answer](http://stackoverflow.com/questions/1713214/how-to-use-c-in-go), 6 | but modified it to use static linking. 7 | 8 | -------------------------------------------------------------------------------- /cfoo.cpp: -------------------------------------------------------------------------------- 1 | #include "foo.hpp" 2 | #include "foo.h" 3 | 4 | Foo FooInit() { 5 | cxxFoo * ret = new cxxFoo(1); 6 | return (void*)ret; 7 | } 8 | 9 | void FooFree(Foo f) { 10 | cxxFoo * foo = (cxxFoo*)f; 11 | delete foo; 12 | } 13 | 14 | void FooBar(Foo f) { 15 | cxxFoo * foo = (cxxFoo*)f; 16 | foo->Bar(); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /foo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "foo.hpp" 3 | 4 | void cxxFoo::Bar(void) { 5 | std::cout<a<