How do I build a cross platform go program?

agolangf · · 961 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I made a small gui program in go using github.com/andlabs/ui and am able to run and build for linux successfully but i don&#39;t know how to do so for windows and mac. I was told I can&#39;t make it cross platform if I use a c based library but that doesn&#39;t make sense since this lib is made for windows mac and linux. If this was on the site and i missed it, please point me out to it. Thanks!</p> <hr/>**评论:**<br/><br/>DualRearWheels: <pre><p>Copy paste from my comment on similar question:</p> <p>Cross compilation works out of the box for pure Go libraries with Go 1.5. If you have C code included (like SQLite database library) you will have to use also a compiler that can cross compile C code. Don&#39;t forget to enable CGO. For eg. if you want to compile 32 bit windows executable on 64 bit linux you could use i586-mingw32msvc-gcc so command would be:</p> <pre><code>export GOOS=windows export GOARCH=386 export CGO_ENABLED=1 export CC=i586-mingw32msvc-gcc go build -ldflags &#34;-extldflags -static&#34; </code></pre> <p>-ldflags &#34;-extldflags -static&#34; at the end makes sure C code is statically linked so resulting binary truly has no dependencies even for C code.</p></pre>netzdamon: <pre><blockquote> <p>i586-mingw32msvc-gcc</p> </blockquote> <p>Where does this come from? For example if I&#39;m on a linux host what package would I install to get this? Is it something I have to install by hand?</p></pre>THEHIPP0: <pre><blockquote> <p>586-mingw32msvc-gcc</p> </blockquote> <p>On Ubuntu it&#39;s in <code>mingw32</code>.</p></pre>DualRearWheels: <pre><p>You must google for C cross compiler that will work on your platform and that compiles for target platform, then install it, of course. GCC/MingW are best (only?) option in most cases.</p></pre>netzdamon: <pre><p>Thanks</p></pre>netzdamon: <pre><p>I&#39;m trying this with one of my own projects right now and I&#39;m getting this.</p> <pre><code>CC=i686-pc-linux-gnu-gcc CGO_ENABLED=1 GOOS=linux GOARCH=386 go build -ldflags &#34;-extldflags -static&#34; # runtime/cgo /usr/bin/i686-pc-linux-gnu-ld: cannot find /usr/lib32/libpthread.so.0 inside /usr/i686-pc-linux-gnu /usr/bin/i686-pc-linux-gnu-ld: cannot find /usr/lib32 /libpthread_nonshared.a inside /usr/i686-pc-linux-gnu collect2: error: ld returned 1 exit status </code></pre></pre>jayjay097531: <pre><p>What about mac? Shouldn&#39;t there be info in the docs on this?</p></pre>m9dhatter: <pre><p>You might just want to give this a go: <a href="https://github.com/laher/goxc" rel="nofollow">https://github.com/laher/goxc</a></p> <p>Cross-compilation has always been hairy. It might just be easier to just get a machine and compile from there. </p></pre>

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

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