<p>Hello,</p>
<p>I´m trying to wrap c++ code in Go code, but I have some problems. I´m using Go for linux, the version of Go is 1.6.2, and to wrap the code I´m using SWIG, and the version of SWIG is 3.0.8. I´m following the SWIG manual and I put a file called example.i in GOPATH/src, and then SWIG manual says that I have to execute:</p>
<pre><code> “swig -go -cgo -intgosize 64 example.i”
</code></pre>
<p>The code of example.i is:</p>
<pre><code> %module example
%{
extern int test(int n);
%}
extern int test(int n);
</code></pre>
<p>This generate two files, example.go and example_wrap.c (I don´t know if it should
generate a file .cpp o .cxx).</p>
<p>When I do “go build” (as SWIG manual says), I get an error because the function test is not implemeted in example_wrap.c. I solve this, adding this code at the end of test_wrap.c:</p>
<pre><code> int test(int a){
return a+1;
}
</code></pre>
<p>Now, when I do "go build", I don´t have any error. So, now, I want to execute the go program, so I execute "go run example.go", but I get this error, that I don´t know how to solve:</p>
<p>go run: cannot run non-main package</p>
<p>I would like to know if I´m doing something wrong and how is the process to generate the files to execute Go code with c++ wrapped.</p>
<hr/>**评论:**<br/><br/>raff99: <pre><p>well, you built a module / package / library, so you can't really run it.</p>
<p>In go the "main" function is called "main" in a package "main". So at the minimum you need another go file that looks like the following:</p>
<p>package main</p>
<p>func main() {
test(42)
}</p>
<p>but this still will not work since test is not in the main package. What package did the "swig" step generate ? Check the first line of example.go and import that package in your main file.</p></pre>RobertoCO: <pre><p>Thanks for your answer,</p>
<p>The first line of example.go is: "package example".</p></pre>justinisrael: <pre><p>I've just been manually writing a C shim for wrapping C++ (done about 3 C++ libs so far, this way) so I don't know much about the swig process.. but I thought you could just put a ".swig" or ".swigcxx" in your package and go would automatically invoke swig for you? </p></pre>RobertoCO: <pre><p>Thanks for your answer.</p>
<p>Yes, it´s another option that I´m trying. But, I don´t understand very well, how I have to name the methods in the classes.</p>
<p>For example, I know that I have to create example.swigcxx, example.cpp and example_test.go. But in example_test.go, I don´t know how I can call the method of example.cpp. I try to do:</p>
<p>*<em>example_test.go: *</em></p>
<pre><code> a := 10
c:= example.test(a) //test is the function of example.swigcxx and example.cpp that only add one unit to a(in this case)
</code></pre></pre>vladimirdogan: <pre><p>go tool knows how to run swig, if you have a .swig file. See <a href="https://github.com/golang/go/issues/6696" rel="nofollow">https://github.com/golang/go/issues/6696</a>. Search for "slimsag commented on Apr 2, 2015" - this comment has very useful step by step instructions</p></pre>