cgo: now I have 2 problems

agolangf · · 1750 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Trying to write cross-platform top equivalent. On FreeBSD, to get information about swap, I need to use cgo.<br/> It fails with error <code>could not determine kind of name for C.kvm_swap</code></p> <p>abridged example code </p> <pre><code>// +build freebsd darwin package main // #include &lt;kvm.h&gt; import &#34;C&#34; var kd *C.kvm_t var swapary *C.kvm_swap </code></pre> <p>go env</p> <pre><code>GOARCH=&#34;amd64&#34; GOBIN=&#34;&#34; GOEXE=&#34;&#34; GOHOSTARCH=&#34;amd64&#34; GOHOSTOS=&#34;freebsd&#34; GOOS=&#34;freebsd&#34; GORACE=&#34;&#34; GOROOT=&#34;/usr/local/go&#34; GOTOOLDIR=&#34;/usr/local/go/pkg/tool/freebsd_amd64&#34; GO15VENDOREXPERIMENT=&#34;&#34; CC=&#34;cc&#34; GOGCCFLAGS=&#34;-fPIC -m64 -pthread -fmessage-length=0&#34; CXX=&#34;g++&#34; CGO_ENABLED=&#34;1&#34; </code></pre> <p>Anyone can suggest any solution? No, I can&#39;t go and use syscall.Sysctl - variables aren&#39;t in any of system information trees.</p> <p>Note about kvm.h : <code>typedef struct __kvm kvm_t;</code> , but just <code>struct kvm_swap { ... }</code></p> <hr/>**评论:**<br/><br/>postman_: <pre><p>Have you tried</p> <p><code> var swapary *C.struct_kvm_swap </code></p> <p>as it says in <a href="https://golang.org/cmd/cgo/">https://golang.org/cmd/cgo/</a> :</p> <blockquote> <p>To access a struct, union, or enum type directly, prefix it with struct<em>, union</em>, or enum_, as in C.struct_stat. </p> </blockquote></pre>RandNho: <pre><p>Thank you, apparenly, I need to read, not to skim.</p> <p>Also, it demanded ldflags next, but that problem was easier to solve.</p></pre>

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

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