3

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?

vuco
  • 161
  • 2
  • 6

1 Answers1

3

I also asked this question on Golang-nuts list and got a solution from Elias Naur:

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

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:

sudo apt-get install gcc-multilib

And then the linker complained about undefined reference to log (math logarith function found in libm.a). At first I thought it was just a missing 32 bit library. 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. 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. The LDFLAGS ended up this way:

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

So, the undefined references in libcluto.a got solved by put -lm after -lcluto.

vuco
  • 161
  • 2
  • 6