Link Go againsta a static 32 bit 3rd party library

84 views
Skip to first unread message

Luis Furquim

unread,
Feb 21, 2021, 1:08:16 PM2/21/21
to golang-nuts
Hello gophers,


I am reposting here the question I just made on Stack Overflow at https://stackoverflow.com/questions/66305363/link-go-against-a-static-32-bit-3rd-party-library
I don't know if it is acceptable posting there and here. I saw that sometimes people ask here and in the Golang developer list and get advised to not post on both lists.
But there is a focus difference between the two lists. So, I think that posting here and on Stack Overflow is not the same case. 
If I misunderstood things, just let me know and this will not happen again.

So, about my question, 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.

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.

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:
```
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.

So, how can I statically link Go against a 32 bit (.a) library?



--
Luis Otavio de Colla Furquim

ma...@eliasnaur.com

unread,
Feb 21, 2021, 1:32:47 PM2/21/21
to golang-nuts
On Sunday, 21 February 2021 at 19:08:16 UTC+1 Luis Furquim wrote:

That seems to me that when I set GOARCH=386 it disables CGO, silently ignoring any code that references to C symbols.



Go disables Cgo when cross-compiling (GOARCH is different from your native GOARCH). You can enable Cgo manually with CGO_ENABLED=1.

Elias
 

Luis Furquim

unread,
Feb 21, 2021, 4:02:30 PM2/21/21
to golang-nuts
Great! It works!
May I post your answer (also giving the credits) on stack overflow?

Thank you!
Luis Otavio

Elias Naur

unread,
Feb 21, 2021, 4:11:11 PM2/21/21
to Luis Furquim, golang-nuts


On Sun, Feb 21, 2021, 22:03 Luis Furquim <luisf...@gmail.com> wrote:
Great! It works!
May I post your answer (also giving the credits) on stack overflow?


Sure. No credit necessary, you're doing the work of posting it :)

Elias
Reply all
Reply to author
Forward
0 new messages