Cross compile from darwin/64 to windows/*

xuanbao · · 532 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I can&#39;t really get it working. I wrote a small go app that just downloads a file from the web and displays the progress using the library <code>andlabs/ui</code> found on github. Here is the code:</p> <pre><code>package main import ( &#34;encoding/json&#34; &#34;fmt&#34; &#34;io&#34; &#34;io/ioutil&#34; &#34;math&#34; &#34;net/http&#34; &#34;os&#34; &#34;github.com/andlabs/ui&#34; ) var bar *ui.ProgressBar var filesize float64 = 20971520 type PassThru struct { io.Reader total int64 } func Round(f float64) int { return int(math.Floor(f + .5)) } func (pt *PassThru) Read(p []byte) (int, error) { n, err := pt.Reader.Read(p) pt.total += int64(n) if err == nil { var progress float64 = float64(pt.total) / filesize // fmt.Println(&#34;Read&#34;, n, &#34;bytes for a total of&#34;, pt.total, &#34; &#34;, progress * 100) ui.QueueMain(func() { bar.SetValue(Round(progress * 100)) }) } return n, err } func main() { url := &#34;https://sirwindfield.github.io/taskflow/application-support/test.jar&#34; file := &#34;test.zip&#34; err := ui.Main(func() { box := ui.NewVerticalBox() bar = ui.NewProgressBar() box.Append(ui.NewLabel(&#34;Updating launcher...&#34;), true) box.Append(bar, true) box.SetPadded(false) window := ui.NewWindow(&#34;Hello&#34;, 200, 24, false) window.SetChild(box) window.SetMargined(true) window.OnClosing(func(*ui.Window) bool { ui.Quit() return true }) window.Show() go downloadFile(file, url) }) if err != nil { panic(err) } } func downloadFile(filepath string, url string) (err error) { // create destination file out, err := os.Create(filepath) if err != nil { return err } defer out.Close() // target file needs get request resp, err := http.Get(url) if err != nil { return err } defer resp.Body.Close() reader := &amp;PassThru{Reader: resp.Body} count, err := io.Copy(out, reader) if err != nil { fmt.Println(err) os.Exit(1) } fmt.Println(&#34;Transferred&#34;, count, &#34;bytes&#34;) return nil } </code></pre> <p>It is just a small test app for later on. I tried to cross compile it. Compiling it using <code>go build github.com/sirwindfield/launcher</code> works fine for my OS (darwin 64Bit). But as soon as I tried to compile it for linux the build process failed because the ui dependencies where not found (<code>undefined: ui.ProgressBar</code>). Strange, because compiling it for OS X works. Trying it for windows gives me an error message that tells me that either go nor SWIG are being used. I googled for like an hour and couldn&#39;t find any fix for it. Tried <code>gox,</code>xgo`but both didn&#39;t work. </p> <p>Would be nice if someone could help me out. My code follows the GO code structure guide. <code>GOPATH</code>is <code>~/golang</code> and I run those commands in the <code>GOPATH</code>directory.</p> <hr/>**评论:**<br/><br/>SportingSnow21: <pre><p>This isn&#39;t a problem with your Go code at all. The <code>andlabs/ui</code> library uses cgo, which doesn&#39;t come with the same cross-compilation guarantees as Go code. Since it depends on the platform-native C/C++ compiler and linker, you would need to compile this on the platform you&#39;re targeting. </p></pre>positivedown: <pre><p>so there is no way to do it on my OS X machine? Do you know another UI library for Go that is cross platform and can be compiled cross platform too?</p></pre>ZenSwordArts: <pre><p>To my knowledge there is no stable (or near-stable), cross-platform GUI library for Go.. </p> <p>However you could serve your &#39;GUI&#39; via an http server + html/css/js frontend. I know it&#39;s not the same as a native library but I think it&#39;s the best option you have at the moment.. </p> <p>If someone has a better idea I would be very interested too..</p></pre>positivedown: <pre><p>the idea is to write a native launcher that downloads my main application written in java. The only UI I need is literally a ProgressBar. Are there any other languages that are relatively easy to learn and can be compiled into executables for each major platform? Maybe an alternative would do it.</p></pre>ZenSwordArts: <pre><p>OK for that case it really isn&#39;t the best idea to spawn an http server.. I guess you chose Go because the binaries are statically linked and thus easily deployable?</p> <p>For Go maybe just display the progress bar in the terminal ( e.g. <a href="https://github.com/cheggaaa/pb" rel="nofollow">pb</a> )</p> <p>You will have similar problems with other languages.. cross-platform gui libraries will probably always bring some dependencies. You should really think about sticking to the terminal.. maybe it&#39;s just good enough.</p></pre>positivedown: <pre><blockquote> <p>OK for that case it really isn&#39;t the best idea to spawn an http server.. I guess you chose Go because the binaries are statically linked and thus easily deployable?</p> </blockquote> <p>Yep, that&#39;s the reason. I guess a terminal shouldbe enough though. It just needs to be shown if a download is needed. The idea was to just deliver a small Go stub that will handle the download of my app + a java installation (if necessary). Is there some way to only open a terminal window if needed?</p></pre>SportingSnow21: <pre><p>One option, since <code>andlabs/ui</code> library has its cgo tied up neatly and promises static linking, is to spin up a couple barebones vm instances to compile the binaries you need with the proper toolchains. </p> <p>Otherwise, you can just stick to cli, which will pop up when the application is run. If it&#39;s a dev tool, or targeted at a similar community, a terminal window isn&#39;t a bad way to go.</p></pre>positivedown: <pre><p>It is targeted at normal consumers. That&#39;s the problem. A terminal is not that streamlined and may be &#34;scary&#34; to others.</p></pre>SportingSnow21: <pre><p>Build VMs are probably your best bet, then. That allows you the native toolchains without the cost or hassle of hardware.</p></pre>positivedown: <pre><p>Ok, thank you. Will probably end up with this.</p></pre>shovelpost: <pre><p>Just curious. You said that your main application is written in Java. Why don&#39;t you use Java for the launcher as well? Java has everything you need to write a &#34;native&#34; UI. Also using just one language for both will probably make maintenance easier in the future.</p> <p>Meanwhile, writing a native, cross-platform UI in Go is not easy since there&#39;s no pure Go library yet and you are forced into the C world which throws all of Go&#39;s advantages out of the window.</p> <p>On the other hand, if you insist on using Go and since the only thing you need is just a progress bar, it might be worth it to do some extra work and write one &#34;from scratch&#34; using <a href="https://github.com/golang/exp/tree/master/shiny" rel="nofollow">exp/shiny</a>. I don&#39;t know how easy it is but check it out.</p></pre>positivedown: <pre><blockquote> <p>Why don&#39;t you use Java for the launcher as well? </p> </blockquote> <p>Because that means that people need to install Java themselves. We publish our application to different companies and not all of them have Java. We got a request if it would be possible to implement a pre launcher capable of downloading Java if needed. </p></pre>shovelpost: <pre><p>Oh aha. Makes sense. In this case take a look at <a href="https://github.com/golang/exp/tree/master/shiny" rel="nofollow">exp/shiny</a>.</p></pre>driusan: <pre><p>shiny uses cgo for the low-level bindings to the OS to create the window too. It would have the same problems. (In fact, I doubt that it&#39;s a problem that anyone can get around without cgo in a way that supports every OS that Go supports.)</p></pre>positivedown: <pre><p>Just in theory, legally that wouldn&#39;t be possible, am I right? Running OS X on hardware not specific to Apple is against the ToS. Windows would need a valid license to work too.</p></pre>Yojihito: <pre><p>Depends if the ToS is enforcable. And that depends on the country you&#39;re in.</p> <p>Law &gt;&gt;&gt; ToS.</p></pre>positivedown: <pre><p>Thank you for your help :)</p></pre>

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

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