douli6605 2016-08-05 20:09 采纳率: 100%
浏览 501
已采纳

cgo交叉编译未能找到库

I've written a wrapper around the semaphore functions in linux. This worked for me in the past on Go 1.3 and Go 1.4, but I needed to rebuild my application using this wrapper and it no longer builds with Go 1.6.2 or Go 1.7rc5.

I wrote a simple test application that produces the same errors:

package main

import (
    "fmt"
    "os"
    "linux/semaphore"
)

func main() {
    var sema semaphore.Semaphore
    if err := sema.Open("/test", 0644, 1); err != nil {
        fmt.Printf("%v
", err)
        os.Exit(1)
    }
    if err := sema.TryWait(); err != nil {
        fmt.Printf("%v
", err)
        os.Exit(1)
    }
    if err := sema.Post(); err != nil {
        fmt.Printf("%v
", err)
        os.Exit(1)
    }
}

Now the program does compile natively (linux/amd64), my issue is when I try to cross-compile for linux/arm (Ti am3352).

Previously all I had to do was:

GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 CC=/am335x_toolchain/bin/armv7l-timesys-linux-gnueabi-gcc go build

Which now gives the following error:

# runtime/cgo
/tmp/go-build820923984/runtime/cgo/_obj/_cgo_export.c:2:20: fatal error: stdlib.h: No such file or directory
 #include <stdlib.h>
                    ^
compilation terminated.

So I specified the include path:

GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 CC=/am335x_toolchain/bin/armv7l-timesys-linux-gnueabi-gcc CGO_CFLAGS="-I/am335x_toolchain/include" go build
# runtime/cgo
/am335x_toolchain/bin/../lib/gcc/armv7l-timesys-linux-gnueabi/4.8.3/../../../../armv7l-timesys-linux-gnueabi/bin/ld: cannot find Scrt1.o: No such file or directory
/am335x_toolchain/bin/../lib/gcc/armv7l-timesys-linux-gnueabi/4.8.3/../../../../armv7l-timesys-linux-gnueabi/bin/ld: cannot find crti.o: No such file or directory
/am335x_toolchain/bin/../lib/gcc/armv7l-timesys-linux-gnueabi/4.8.3/../../../../armv7l-timesys-linux-gnueabi/bin/ld: cannot find -lpthread
collect2: error: ld returned 1 exit status

Eventually I got to this, which still doesn't work:

GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 CC=/am335x_toolchain/bin/armv7l-timesys-linux-gnueabi-gcc CGO_LDFLAGS="-L/am335x_toolchain/lib -pthread" CGO_CFLAGS="-I/am335x_toolchain/include" go build
# runtime/cgo
/am335x_toolchain/bin/../lib/gcc/armv7l-timesys-linux-gnueabi/4.8.3/../../../../armv7l-timesys-linux-gnueabi/bin/ld: cannot find Scrt1.o: No such file or directory
/am335x_toolchain/bin/../lib/gcc/armv7l-timesys-linux-gnueabi/4.8.3/../../../../armv7l-timesys-linux-gnueabi/bin/ld: cannot find crti.o: No such file or directory
/am335x_toolchain/bin/../lib/gcc/armv7l-timesys-linux-gnueabi/4.8.3/../../../../armv7l-timesys-linux-gnueabi/bin/ld: cannot find /lib/libpthread.so.0
/am335x_toolchain/bin/../lib/gcc/armv7l-timesys-linux-gnueabi/4.8.3/../../../../armv7l-timesys-linux-gnueabi/bin/ld: cannot find /usr/lib/libpthread_nonshared.a
collect2: error: ld returned 1 exit status

The pthread library does exist:

$ ls /am335x_toolchain/lib | grep "libpthread*"
libpthread-2.18.so
libpthread.a
libpthread_nonshared.a
libpthread.so
libpthread.so.0

Any pointers on what I need to do to cross-compile with CGO? Seems like with each release I've had more trouble cross-compiling with CGO.

EDIT:

I've discovered this is an issue with the host architecture. I get the above issues when running on an x86_64 machine (linux). When I compile with Go 1.6 on a 32bit x86 machine (linux), it builds without errors. Still haven't found a solution of compiling on the 64bit machine.

  • 写回答

1条回答 默认 最新

  • donglang6656 2016-08-08 18:17
    关注

    So the problem is that for the cross-compiler to properly find its headers and libraries it needs a --sysroot option, in your case --sysroot /am335x_toolchain/ added both to CGO_CFLAGS and CGO_LDFLAGS.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数