简体   繁体   English

cgo交叉编译未能找到库

[英]cgo cross-compile failing to find libraries

I've written a wrapper around the semaphore functions in linux. 我已经为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. 过去,这在Go 1.3和Go 1.4上对我有用,但是我需要使用此包装器重新构建应用程序,并且不再使用Go 1.6.2或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\n", err)
        os.Exit(1)
    }
    if err := sema.TryWait(); err != nil {
        fmt.Printf("%v\n", err)
        os.Exit(1)
    }
    if err := sema.Post(); err != nil {
        fmt.Printf("%v\n", 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). 现在程序确实可以本地编译(linux / amd64),我的问题是当我尝试交叉编译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: pthread库确实存在:

$ 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? 关于与CGO交叉编译需要做什么的任何指示? Seems like with each release I've had more trouble cross-compiling with CGO. 似乎在每个发行版中,我与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). 在x86_64机器(linux)上运行时,出现上述问题。 When I compile with Go 1.6 on a 32bit x86 machine (linux), it builds without errors. 当我在32位x86机器(Linux)上使用Go 1.6进行编译时,它的构建没有错误。 Still haven't found a solution of compiling on the 64bit machine. 仍未找到在64位计算机上进行编译的解决方案。

因此,问题在于,为了使交叉编译器正确找到其标题和库,需要使用--sysroot选项 ,在您的情况下--sysroot /am335x_toolchain/添加到了CGO_CFLAGS和CGO_LDFLAGS中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM