Since the question was originally asked, both the Go and SWIG updates were updated. I will post a solution that worked for me (Go 1.3.3, SWIG 3.0.2). As you can see, the difference with the original message is minimal.
The most recommended approach is to use go build. He currently knows the * .swig and * .swigcxx files and starts both SWIG and GCC / g ++ automatically based on the information found in the swig interface file.
stmain.go
package main import ( "st"
th /st.h
ifndef ST_H #define ST_H #include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <string> #include <iostream> void pinput(const std::string& pstring); #endif
th /st.cpp
#include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <string> #include <iostream> void pinput(const std::string& pstring) { std::cout << pstring; std::cout << std::endl; // Without endl the stdout doesn't get flushed for me -> no output }
th /st.go
package st
th /st.swigcxx
%module st %include "std_string.i" %include "st.h" %{ extern void pinput(const std::string& pstring); %} void pinput(const std::string& pstring);
To create and run
// Quick'n'dirty version
Alternatively, you can create the same sources manually using 6g / c / l / swig / gotool, for example, my simplified version:
Makefile:
all: make -C st /usr/local/go/pkg/tool/linux_amd64/6g -I st stmain.go /usr/local/go/pkg/tool/linux_amd64/6l -o stmain -extldflags -lstdc++ -L st stmain.6 clean: rm -f *.6 6.out make clean -C st test: stmain
th / Makefile:
all: clean swig -go -intgosize 64 -c++ -soname st.so st.swigcxx g++ -c st.cpp -o st.o g++ -c st_wrap.cxx -o st_wrap.o /usr/local/go/pkg/tool/linux_amd64/6c -I /usr/local/go/pkg/linux_amd64/ -D _64BIT st_gc.c /usr/local/go/pkg/tool/linux_amd64/6g st.go go tool pack grc st.a st.6 st_gc.6 st.o st_wrap.o clean: rm -f *.6 rm -f *.a rm -f *.so rm -f *.cxx rm -f *.c rm -f *.o
source share