dtxooq1020 2015-02-03 19:33
浏览 34

交叉编译net / http进行分发

I have build the following code in a docker container with the following architecture:
cat /proc/version Linux version 3.16.7-tinycore64 (root@064f0e1ce709) (gcc version 4.7.2 (Debian 4.7.2-5) ) #1 SMP Tue Dec 16 23:03:39 UTC 2014

package main

import "fmt"

func main() {
    fmt.Println("Hello")
}

The binary distributed, runs with no problem on a busybox container, with the same architecture without installing golang.


The problem

When I do the same for the following code:

package main

import (
    "fmt"
    "net/http"
)

const (
    port = ":80"
)

var calls = 0

func HelloWorld(w http.ResponseWriter, r *http.Request) {
    calls++
    fmt.Fprintf(w, "Hello, world! You have called me %d times.
", calls)
}

func init() {
    fmt.Printf("Started server at http://localhost%v.
", port)
    http.HandleFunc("/", HelloWorld)
    http.ListenAndServe(port, nil)
}

func main() {}

Then I get:
ash: ./hello_world: not found

I might be missing some dependencies - like "net/http"?
But I thought the go build would build all into the binaries.
This is for both go build & go install.

Any idea?

  • 写回答

2条回答 默认 最新

  • douxie1692 2015-02-03 22:15
    关注

    The answer is most probably the one described in this article.

    Some critical parts of the standard library use CGO [...] if you cross-compile Go to Darwin or Linux your programs won’t use the system DNS resolver. They also can’t use the native host certificate store. They also can’t look up the user’s home directory, either.

    And CGO links against some standard system interfaces by default, dynamically.

    The article suggests using gonative to fix the problem. If that's not your cup of tea, some people suggest using:

    go build -ldflags "-linkmode external -extldflags -static" 
    

    Also read: https://groups.google.com/d/topic/golang-nuts/H-NTwhQVp-8/discussion

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP