Home Php C# Sql C C++ Javascript Python Java Go Android Git Linux Asp.net Django .net Node.js Ios Xcode Cocoa Iphone Mysql Tomcat Mongodb Bash Objective-c Scala Visual-studio Apache Elasticsearch Jar Eclipse Jquery Ruby-on-rails Ruby Rubygems Android-studio Spring Lua Sqlite Emacs Ubuntu Perl Docker Swift Amazon-web-services Svn Html Ajax Xml Java-ee Maven Intellij-idea Rvm Macos Unix Css Ipad Postgresql Css3 Json Windows-server Vue.js Typescript Oracle Hibernate Internet-explorer Github Tensorflow Laravel Symfony Redis Html5 Google-app-engine Nginx Firefox Sqlalchemy Lucene Erlang Flask Vim Solr Webview Facebook Zend-framework Virtualenv Nosql Ide Twitter Safari Flutter Bundle Phonegap Centos Sphinx Actionscript Tornado Register | Login | Edit Tags | New Questions | 繁体 | 简体


4 questions online user: 15

343
votes
answers
27 views
+10

Cross compile Go on OSX?

I am trying to cross-compile a go app on OSX to build binaries for windows and linux. I have read everything what I could find on the net. Closest example that I have found has been published on (apart from many unfinished discussions on go-nuts mailing list):

http://solovyov.net/en/2012/03/09/cross-compiling-go/

yet it does not work on my installation. I have go 1.0.2. As 1.0.2 is quite recent it looks to me that all above examples do not apply to this version.

Tried to do ./make.bash --no-clean with ENV vars set to 386/windows, it does build go, however it builds go for my installation which is darwin/amd64 and completely ignores what is set in ENV that suppose to build different compiler.

Any advises how it can be done (if it can be done at all)?

up vote 119 down vote accepted favorite
沙发
+1190
+50

使用Go 1.5,它們似乎改進了交叉編譯過程,這意味著它現在已經內置。不需要./make.bashbrew不需要。這裡描述這個過程但對於TLDR-ers(像我一樣):你只需設置GOOSGOARCH環境變量並運行go build。

對於那些更加懶散的複制貼紙(像我一樣),如果你在* nix系統上做這樣的事情:

env GOOS=linux GOARCH=arm go build -v github.com/path/to/your/app

你甚至學會了這個env技巧,它讓你只為該命令設置環境變量,完全免費。

env命令僅在自定義環境中運行該調用,並在完成後“重置”它。例如,運行導出GOOS = windows,然後是帶或不帶env和echo $ GOOS的命令。隨著env GOOS沒有改變。 - meonlol 2016年6月13日9:17

在沒有環境的情況下也是如此(至少在Bash中)。我運行導出GOOS = windows然後GOOS = linux bash -c'echo“GOOS:$ GOOS”'然後回顯“GOOS:$ GOOS”。env是否提供與其他shell方言或其他平台的更好兼容性?如果不是這裡似乎是多餘的。 - davidchambers 2016年6月13日14:43

@davidchambers在BASH中它們是等價的。而在其他一些Shell中,例如FISH shell,它不支持FOO = bar cmd,因此你必須使用env FOO = bar cmd。因此我認為使用env FOO = bar cmd的最大優點是兼容性。 - PickBoy 2016年12月1日12:12

難以置信的答案就在這裡。你解決了我的問題,教了我一個新的伎倆,讓我自言自語。 - T Blank 2017年1月16日6:07

很好的答案,謝謝!為了編譯在heroku(intel x86)上使用,我稍微修改了一行到env GOOS = linux GOARCH = 386 go build -v github.com/path/to/your/app它就像一個冠軍--Ira Herman Aug 18 '17在19:19

+1340

感謝golang-nuts的親切和耐心的幫助,配方如下:

1)需要為不同的目標平台和體系結構編譯Go編譯器。這是在go安裝中從src文件夾完成的。在我的案例中,Go安裝就位於/usr/local/go編譯需要發布make實用程序的編譯器在此之前,您需要了解一些注意事項。

交叉編譯時存在關於CGO庫的問題,因此需要禁用CGO庫。

編譯是通過將位置更改為源目錄來完成的,因為編譯必須在該文件夾中完成

cd /usr/local/go/src

然後編譯Go編譯器:

sudo GOOS=windows GOARCH=386 CGO_ENABLED=0 ./make.bash --no-clean

您需要通過更改GOOS和GOARCH參數,為希望交叉編譯的每個操作系統和體系結構重複此步驟。

如果您像我一樣在用戶模式下工作,則需要sudo,因為Go編譯器位於系統目錄中。否則,您需要以超級用戶身份登錄。在Mac上,您可能需要啟用/配置SU訪問(默認情況下不可用),但如果您已設法安裝Go,則可能已具有root訪問權限。

2)一旦構建了所有交叉編譯器,您就可以通過使用以下設置來愉快地交叉編譯應用程序,例如:

GOOS=windows GOARCH=386 go build -o appname.exe appname.go

GOOS=linux GOARCH=386 CGO_ENABLED=0 go build -o appname.linux appname.go

將GOOS和GOARCH更改為您要構建的目標。

如果您遇到CGO問題,請在命令行中包含CGO_ENABLED = 0。另請注意,linux和mac的二進製文件沒有擴展名,因此您可以添加擴展名以便擁有不同的文件。-o開關指示Go使輸出文件類似於c / c ++的舊編譯器,因此上面使用的appname.linux可以是任何其他擴展。

最初讓我感到困惑的是,在編譯的第一部分中說:#為主機構建編譯器和Go bootstrap工具,darwin / amd64但後來它實際上最終為:---在/ usr / local中安裝Go for windows / 386 / go / usr / local / go / bin中的已安裝命令,因此必須觀察結束而不是開始編譯編譯器。 - ljgww 2012年8月30日18:23

一切都是通過嘗試來做的:$ GOARCH = 386 GOOS = linux go build app.go並收到錯誤#timetime /usr/local/go/src/pkg/runtime/extern.go:137:undefined:theGoos / usr / local /go/src/pkg/runtime/extern.go:137:在const初始化程序中不能使用Goos作為類型字符串 - ljgww 2012年8月30日18:38

Homebrew中的Go包有一個選項“--cross-compile-all”,它將自動構建所有交叉編譯器。 - nimrodm 2014年4月28日19:30

很棒的提示@nimrodm!重新編譯你的安裝,你需要運行brew重新安裝go --cross-compile-all - linqu 2015年1月14日在17:06

@ljgww'sudo'沒有配置ENV。我最終在/ usr / local / go / pkg / linux_amd64 /上使用chown / - Nuno Silva 2015年10月9日12:51

+630

如果你在OS X上使用Homebrew,那麼你有一個更簡單的解決方案:

$ brew install go --with-cc-common # Linux, Darwin, and Windows

要么..

$ brew install go --with-cc-all # All the cross-compilers

reinstall如果您已經go安裝,請使用

注意到更新的開關是:--cross-compile-all為所有支持的平台構建交叉編譯器和運行時支持--cross-compile-common為darwin,linux和windows構建交叉編譯器和運行時支持 - Chip Tol Feb 17 '15在16:48

--cross-compile-all現在是--with-cc-all - gianebao 2015年5月13日8:32

@ sheeks06 - 修復。謝謝! - docwhat 2015年5月14日15:09

我所知道的這些旗幟不再存在。我看到的唯一相關選項是--without-cgo :( - rdegges 2015年9月24日5:23

從Go 1.5開始,沒有單獨的交叉編譯器,你現在只使用標誌tip.golang.org/doc/go1.5#compiler_and_tools - chuckus 2016年7月4日14:27

+190

您可以使用Docker輕鬆完成此操作,因此無需額外的庫。只需運行此命令:

docker run --rm -it -v "$GOPATH":/go -w /go/src/github.com/iron-io/ironcli golang:1.4.2-cross sh -c '
for GOOS in darwin linux windows; do
  for GOARCH in 386 amd64; do
    echo "Building $GOOS-$GOARCH"
    export GOOS=$GOOS
    export GOARCH=$GOARCH
    go build -o bin/ironcli-$GOOS-$GOARCH
  done
done
'

您可以在以下文章中找到更多詳細信息:https//medium.com/iron-io-blog/how-to-cross-compile-go-programs-using-docker-beaa102a316d

為什麼有人想安裝Docker來執行此操作時,他們可以在env上執行shell循環GOOS = x GOARCH = y go install something / ...並以$ GOPATH / bin / $ GOOS_ $ GOARCH下的相應二進製文件結束?? 而BTW,Go支持的不僅僅是你列出的三個操作系統,為什麼不喜歡BSD? - Dave C 2015年7月9日13:03

你不會只是為了這樣安裝Docker,但是如果你擁有它,這比替代方案更容易和更清潔。 - Travis Reeder 2015年7月9日23:38

+50

對於需要啟用CGO並從OSX目標窗口進行交叉編譯的人員

因為我導入了https://github.com/mattn/go-sqlite3並且需要它,所以我需要在從我的mac編譯窗口時啟用CGO根據其他答案編譯給了我和錯誤:

/usr/local/go/src/runtime/cgo/gcc_windows_amd64.c:8:10: fatal error: 'windows.h' file not found

如果你像我一樣,你必須用CGO編譯。這就是我做的:

1.我們將使用CGO依賴庫交叉編譯Windows。首先我們需要安裝一個交叉編譯器mingw-w64

brew install mingw-w64

這可能會在這里安裝它/usr/local/opt/mingw-w64/bin/

2.就像其他答案一樣,我們首先需要將windows arch添加到我們的go編譯器工具鏈中。編譯編譯器需要編譯器(奇怪的句子)編譯go編譯器需要一個單獨的預構建編譯器。我們可以在一個文件夾中下載一個預構建的二進製文件或從源代碼構建,例如:~/Documents/go 現在我們可以改進我們的Go編譯器,根據最佳答案,但這次使用CGO_ENABLED=1和我們單獨的預構建編譯器GOROOT_BOOTSTRAP(Pooya是我的用戶名):

cd /usr/local/go/src
sudo GOOS=windows GOARCH=amd64 CGO_ENABLED=1 GOROOT_BOOTSTRAP=/Users/Pooya/Documents/go ./make.bash --no-clean
sudo GOOS=windows GOARCH=386 CGO_ENABLED=1 GOROOT_BOOTSTRAP=/Users/Pooya/Documents/go ./make.bash --no-clean

3.編譯我們的Go代碼時,用於mingw編譯啟用了CGO的go文件定位窗口:

GOOS="windows" GOARCH="386" CGO_ENABLED="1" CC="/usr/local/opt/mingw-w64/bin/i686-w64-mingw32-gcc" go build hello.go
GOOS="windows" GOARCH="amd64" CGO_ENABLED="1" CC="/usr/local/opt/mingw-w64/bin/x86_64-w64-mingw32-gcc" go build hello.go
+30

為許多平台創建可執行文件的過程可能有點乏味,所以我建議使用腳本:

#!/usr/bin/env bash

package=$1
if [[ -z "$package" ]]; then
  echo "usage: $0 <package-name>"
  exit 1
fi
package_name=$package

#the full list of the platforms: https://golang.org/doc/install/source#environment
platforms=(
"darwin/386"
"dragonfly/amd64"
"freebsd/386"
"freebsd/amd64"
"freebsd/arm"
"linux/386"
"linux/amd64"
"linux/arm"
"linux/arm64"
"netbsd/386"
"netbsd/amd64"
"netbsd/arm"
"openbsd/386"
"openbsd/amd64"
"openbsd/arm"
"plan9/386"
"plan9/amd64"
"solaris/amd64"
"windows/amd64"
"windows/386" )

for platform in "${platforms[@]}"
do
    platform_split=(${platform//// })
    GOOS=${platform_split[0]}
    GOARCH=${platform_split[1]}
    output_name=$package_name'-'$GOOS'-'$GOARCH
    if [ $GOOS = "windows" ]; then
        output_name+='.exe'
    fi

    env GOOS=$GOOS GOARCH=$GOARCH go build -o $output_name $package
    if [ $? -ne 0 ]; then
        echo 'An error has occurred! Aborting the script execution...'
        exit 1
    fi
done

我只在OSX上檢查了這個腳本

gist - go-executable-build.sh

10
votes
answers
49 views
+10

Compiling and running an OSGI application in Eclipse

The base of our enterprise application is OSGI and we have several Java projects that are logically OSGI bundles. We use Maven to compile the application using the Maven Bundle plugin. But this process is time consuming and makes it impossible to debug the application. We also use the Runner and Pax(:provison) plugins to run the application. If we could rely on the Auto build function of Eclipse and also debug the application it would make our lives so much easier. Is there a way to configure Eclipse to be able to compile (and may be run) an OSGI-based application?

沙发
+50
+50

I'm not entirely sure if I understand you, but here goes.

Well, running/debugging OSGi applications in Eclipse is really easy, as long as your bundles reside in PDE aware projects or at least are on your target platform.

Do you have the source of all your bundles? Debugging without source isn't all that useful. If you do, can you just import all the source of your bundles into your Eclipse workspace?

Otherwise you can create a target platform, add all your bundles to that. (as a first attempt, I'd say dump all your bundles in a directory and point the target platform there)

Either way, then you should be able to Run (or debug)-> OSGi framework -> New -> Pick your bundles -> Start

You can both pick bundles from or target platform and from your workspace.

哦,你需要一個帶PDE的Eclipse安裝,忘了提一下。 - Frank Lee 2012年8月29日16:27

+30

For building, you can use Eclipse Plugin Development Environment (PDE). Despite its name, it isn't specific to building Eclipse plugins and can be used for working on pure OSGi bundles. Eclipse plugins are OSGi bundles with some extras.

Cannot help you with the running or debugging part, although I do know that some enterprise-oriented OSGi platforms provide extensions to PDE.

+10

If you're already using the maven bundle plugin, you may find that PDE's manifest-first approach isn't a good fit with your existing code-first build (I assume at the end you want both an IDE build for development and debugging, and a command-line build for continuous integration and automated testing).

You have two choices. As others have suggested, you can use Eclipse's integrated PDE, and use Tycho for your maven build. Tycho uses the same data used by PDE, so you don't have to write things down more than once. Alternatively, you can stick with the maven bundle plugin and use bndtools within Eclipse. Like the bundle plugin, bndtools is code-first, so you won't need to worry about maintaining manifests. However, you may find there isn't quite as big a set of features in bndtools as in PDE, and I'd suggest still checking your manifests by hand to make sure you understand what's being generated. Whether you prefer manifest-first or code-first is a bit of a heated philosophical debate.

+10

Look at bndtools. bndtools is using the same bnd that is underlying the maven bundle plugin. You can even use bndtools together with m2e. bndtools is available from the Eclipse market place.

1
votes
answers
32 views
+10

Why does C++ compilation take so long?

Compiling a C++ file takes a very long time when compared to C# and Java. It takes significantly longer to compile a C++ file than it would to run a normal size Python script. I'm currently using VC++ but it's the same with any compiler. Why is this?

The two reasons I could think of were loading header files and running the preprocessor, but that doesn't seem like it should explain why it takes so long.

沙发
+10

There are two issues I can think of that might be affecting the speed at which your programs in C++ are compiling.

POSSIBLE ISSUE #1 - COMPILING THE HEADER: (This may or may not have already been addressed by another answer or comment.) Microsoft Visual C++ (A.K.A. VC++) supports precompiled headers, which I highly recommend. When you create a new project and select the type of program you are making, a setup wizard window should appear on your screen. If you hit the “Next >” button at the bottom of it, the window will take you to a page that has several lists of features; make sure that the box next to the “Precompiled header” option is checked. (NOTE: This has been my experience with Win32 console applications in C++, but this may not be the case with all kinds of programs in C++.)

POSSIBLE ISSUE #2 - THE LOCATION BEING COMPILED TO: This summer, I took a programming course, and we had to store all of our projects on 8GB flash drives, as the computers in the lab we were using got wiped every night at midnight, which would have erased all of our work. If you are compiling to an external storage device for the sake of portability/security/etc., it can take a very long time (even with the precompiled headers that I mentioned above) for your program to compile, especially if it’s a fairly large program. My advice for you in this case would be to create and compile programs on the hard drive of the computer you’re using, and whenever you want/need to stop working on your project(s) for whatever reason, transfer them to your external storage device, and then click the “Safely Remove Hardware and Eject Media” icon, which should appear as a small flash drive behind a little green circle with a white check mark on it, to disconnect it.

I hope this helps you; let me know if it does! :)

0
votes
answers
33 views
+10

Code Composer Studio - MSP430編譯錯誤「目標配方失敗」

0

我嘗試在4年後再次使用MSP430 Launchpad和Code Composer Studio。我之前已經安裝了CCS v5,並且最近將V7安裝到了D驅動器中(我自己選擇)Code Composer Studio - MSP430編譯錯誤「目標配方失敗」

在我最後幾個小時發生編譯錯誤後,我一直無法修復......我發現我有v5文件仍在計算機上。所以我卸載V7,手動刪除所有文件V5,然後重新安裝CCS V7進入C盤(默認目錄)

我得到同樣的錯誤依然:

"C:\ti\ccsv7\utils\bin\gmake" -k -j 4 all -O 
subdir_rules.mk:9: recipe for target 'blink.obj' failed 
gmake: *** [blink.obj] Error 1 
gmake: Target 'all' not remade because of errors. 
**** Build Finished **** 

現在,我的代碼運行是隨包裝附帶的示例代碼,所以我認爲它是正確的。錯誤所指的文件似乎是在嘗試編譯時自動生成的,因此它似乎不太可能在代碼中有任何問題,因爲它是由編譯器/生成文件的人員所做的。

9號線:

@echo 'Building file: $<' 

Code Composer Editor View - Attached Picture

我趕在11行很長的路徑定義輸入幾次,所以所有的包括已經填充到第17行,只是爲了便於在圖像中顯示,但他們其實都在第11行。

沙发
0
1

好的人,我發現了這個問題。這是我的互聯網安全。 我查看了Comodo Internet Security日誌,它幾乎阻止了所有「make」嘗試。

只是禁用防病毒/安全等,它終於建立了目標文件。

需要將CCS添加到批准的應用程序列表我猜。