Golang Cross-compiling

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Golang Pass the very ox X, but can not cross-compile, for me a little use is not, there is an open source project is written with Golang, if not in arm, then I can only "translate" into C, and then integrated into my product, but I do not have this time.

Fortunately, cross-compilation finally succeeded.

1. Preparatory work

1.1 Golang environment and arm cross-compiler tool

You can refer to the "Golang installation Codex" this article.

1.2 gcc Cross Compiler

On the basis of 1.1, it is possible to compile the binary of pure Golang written on arm, but there is an unresolved problem: Golang and C jointly developed module, compile times wrong:

Can ' t load package:no buildable Go source files in/...

The problem is that, when CGO is turned off, the Golang compiler automatically "ignores" the source file with the word "import" C "". To resolve this issue, you need to open CGO support, such as the compile-time command instead:

Goarch=arm cgo_enabled=1 Go Build hello.go

However, this is not enough and requires the installation of the arm platform compiler, such as Gcc-4.7-arm-linux-gnueabi (the version number to match the version number of the target platform ), And the support of Gcc-4.7-multilib-arm-linux-gnueabi. You will also need to re-/USR/BIN/GCC LN to arm's cross-compiler.

After the completion of the above work, import "C" will be no problem.

1.3 Third-party libraries

The main purpose of using the import "C" is to use third-party open source libraries, Golang find third-party libraries using pkg-config, first of all to ensure that pkg-config normal work, in addition, to ensure that third-party libraries, and third-party libraries of the dependent libraries, are arm version, To link successfully. Link the successful Golang program, you can happily run on the arm board.

Golang's initial adventure to some end, from the next code to complete this blog, time spent more than a day, my program is also happy to run on the board.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.