简体   繁体   English

将 Go 与 static 32 位第 3 方库链接

[英]Link Go against a static 32 bit 3rd party library

I am using the flags below我正在使用下面的标志

#cgo CFLAGS: -I.
#cgo LDFLAGS: -L. -lcluto

#include <malloc.h>
#include "cluto.h"

and when I compile with当我编译时

go build clutod.go

The compiler fails with error编译器因错误而失败

/usr/bin/ld: pulando ../../../../gopkg/src/dawn/plugins/cluto/clutobot/libcluto.a incompatível ao pesquisar para -lcluto
/usr/bin/ld: não foi possível localizar -lcluto
collect2: error: ld returned 1 exit status

Which means that it finds that libcluto.a is incompatible because it is a 32 bit library and my system is compiling Go to amd64 architecture.这意味着它发现 libcluto.a 不兼容,因为它是一个 32 位库,而我的系统正在将 Go 编译为 amd64 架构。

But when I compile with但是当我编译时

GOARCH=386 go build clutod.go

The method defined in the same source file where 'import "C"' is defined is simply ignored.在定义了“import "C"”的同一源文件中定义的方法被简单地忽略。

I checked with reflect and the method does not exist:我检查了反射,该方法不存在:

Goose.New.Logf(5,"|methods|=%d",typ.NumMethod())
Goose.New.Logf(5,"type=%s.%s",typ.PkgPath(),typ.Name())
for j=0; j<typ.NumMethod(); j++ {
   mt := typ.Method(j)
   Goose.New.Logf(5,"%d: %s",j,mt.Name)
}

Goose.New.Logf(5,"*|methods|=%d",typPtr.NumMethod())
Goose.New.Logf(5,"*type=%s.%s",typPtr.PkgPath(),typPtr.Name())
for j=0; j<typPtr.NumMethod(); j++ {
   mt := typPtr.Method(j)
   Goose.New.Logf(5,"%d: *%s",j,mt.Name)
}

Goose.New.Logf(1,"Method not found: %s, Data: %#v",methodName,typ)
return nil, errors.New(fmt.Sprintf("Method not found: %s",methodName))

The code above produces the following output:上面的代码生成以下 output:

2021/02/21 14:33:50 {stonelizard}[New.go]<New>(296): |methods|=10
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(297): type=dawn/plugins/cluto/clutobot.ServiceT
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(300): 0: CRLListenAddress
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(300): 1: CertKit
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(300): 2: GetConfig
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(300): 3: GetHost
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(300): 4: GetMasterConfig
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(300): 5: HttpsClient
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(300): 6: ListenAddress
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(300): 7: PageNotFound
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(300): 8: SavePending
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(300): 9: Stop
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(303): *|methods|=21
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(304): *type=.
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 0: *CRLListenAddress
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 1: *CertKit
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 2: *GetConfig
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 3: *GetHost
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 4: *GetId
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 5: *GetMasterConfig
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 6: *GetRest
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 7: *HttpsClient
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 8: *InitConn
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 9: *ListenAddress
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 10: *LoadClientCert
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 11: *LoadConfig
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 12: *NewCK
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 13: *NewSshClientConfig
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 14: *PageNotFound
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 15: *PingAt
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 16: *SavePending
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 17: *SetRest
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 18: *Start
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 19: *Stop
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(307): 20: *Version
2021/02/21 14:33:50 {stonelizard}[New.go]<New>(310): Method not found: VP_ClusterDirect, Data: &reflect.rtype{size:0x100, ptrdata:0x100, hash:0xcfbeab05, tflag:0x7, align:0x4, fieldAlign:0x4, kind:0x19, equal:(func(unsafe.Pointer, unsafe.Pointer) bool)(nil), gcdata:(*uint8)(0x86467d8), str:85076, ptrToThis:937664}

That seems to me that when I set GOARCH=386 it disables CGO, silently ignoring any code that references to C symbols.在我看来,当我设置 GOARCH=386 时,它会禁用 CGO,默默地忽略任何引用 C 符号的代码。

So, how can I statically link Go against a 32 bit (.a) library?那么,如何将 Go 与 32 位 (.a) 库静态链接?

I also asked this question on Golang-nuts list and got a solution from Elias Naur:我还在 Golang-nuts list 上问了这个问题,并从 Elias Naur 那里得到了解决方案:

"Go disables Cgo when cross-compiling (GOARCH is different from your native GOARCH). You can enable Cgo manually with CGO_ENABLED=1." “Go 在交叉编译时禁用 Cgo(GOARCH 与您的本地 GOARCH 不同)。您可以使用 CGO_ENABLED=1 手动启用 Cgo。”

So, the solution is所以,解决方案是

CGO_ENABLED=1 GOARCH=386 go build  clutod.go

After that I got some compiling errors because of missing headers, which I solved installing its 32 bit versions:之后,由于缺少标头,我遇到了一些编译错误,我解决了安装其 32 位版本的问题:

sudo apt-get install gcc-multilib

And then the linker complained about undefined reference to log (math logarith function found in libm.a).然后 linker 抱怨未定义对日志的引用(在 libm.a 中找到的数学对数 function)。 At first I thought it was just a missing 32 bit library.起初我以为它只是一个缺少的 32 位库。 But then I found the library installed on my system.但后来我发现我的系统上安装了这个库。 So I thought the problem was how to show the linker how to find it.所以我认为问题是如何显示 linker 如何找到它。 But not worked.但没有奏效。 Then I found this link linking 3rd party static libs in cgo library and realized that I it was not resolving the reference because the library that contains it was appearing first, then I switched places and it worked.然后我发现这个链接在 cgo 库中链接了第 3 方 static 库,并意识到我没有解析引用,因为包含它的库首先出现,然后我切换了位置并且它工作了。 The LDFLAGS ended up this way: LDFLAGS 以这种方式结束:

#cgo LDFLAGS: -L/usr/lib32 -L . -lcluto -lm

So, the undefined references in libcluto.a got solved by put -lm after -lcluto.因此,libcluto.a 中未定义的引用通过在 -lcluto 之后放置 -lm 得到了解决。

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

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