Swig and go

xuanbao · · 1006 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<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 &#34;go build&#34;, I don´t have any error. So, now, I want to execute the go program, so I execute &#34;go run example.go&#34;, 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&#39;t really run it.</p> <p>In go the &#34;main&#34; function is called &#34;main&#34; in a package &#34;main&#34;. 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 &#34;swig&#34; 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: &#34;package example&#34;.</p></pre>justinisrael: <pre><p>I&#39;ve just been manually writing a C shim for wrapping C++ (done about 3 C++ libs so far, this way) so I don&#39;t know much about the swig process.. but I thought you could just put a &#34;.swig&#34; or &#34;.swigcxx&#34; 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 &#34;slimsag commented on Apr 2, 2015&#34; - this comment has very useful step by step instructions</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1006 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传