sdk icon indicating copy to clipboard operation
sdk copied to clipboard

golang sdk gcc failure : undefined reference to `pow'

Open rorycl opened this issue 1 year ago • 3 comments

Steps To Reproduce

  1. using sdk-go on linux (github.com/bitwarden/sdk-go v1.0.2)
  2. and a simplified version of the example go program in the repo
  3. running go run . fails with a gcc error.

Expected Result

no gcc complication failure

Actual Result

/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/usr/bin/ld: /home/rory/go/pkg/mod/github.com/bitwarden/[email protected]/internal/cinterface/lib/linux-x64/libbitwarden_c.a(bitwarden_c.tokio-db23cc9623df1e2a.tokio.2ce6e619c7e75357-cgu.0.rcgu.o.rcgu.o): in function `tokio::runtime::scheduler::multi_thread::worker::run':
tokio.2ce6e619c7e75357-cgu.0:(.text._ZN5tokio7runtime9scheduler12multi_thread6worker3run17hfc699787cb3496bdE+0x762): undefined reference to `pow'
/usr/bin/ld: tokio.2ce6e619c7e75357-cgu.0:(.text._ZN5tokio7runtime9scheduler12multi_thread6worker3run17hfc699787cb3496bdE+0xcd5): undefined reference to `pow'
collect2: error: ld returned 1 exit status

Screenshots or Videos

No response

Additional Context

I'm working on 6.11.10-amd64 (debian) gcc version 14.2.0

Operating System

Linux

Operating System Version

debian testing

Build Version

github.com/bitwarden/sdk-go v1.0.2

Issue Tracking Info

  • [X] I understand that work is tracked outside of Github. A PR will be linked to this issue should one be opened to address it, but Bitwarden doesn't use fields like "assigned", "milestone", or "project" to track progress.

rorycl avatar Dec 28 '24 14:12 rorycl

I have had quite the issue with this as well I have a private Go Package that I want to be able to go install path/to/my/private/package@latest. The instructions say to build with go build -ldflags '-linkmode external -extldflags "-static -Wl,-unresolved-symbols=ignore-all"' which does technically solve the issue because it ignores the missing symbol pow, and in my case also ceil. Looking for the symbol pow with nm -gD $HOME/go/pkg/mod/github.com/bitwarden/[email protected]/internal/cinterface/lib/linux-x64/libbitwarden_c.a | grep 'pow' reveals that it does not have pow in it, which is why ignoring the missing symbol works.

I don't like this solution because:

  1. It requires anyone who uses the package has to somehow know to add -ldflags '-linkmode external -extldflags "-static -Wl,-unresolved-symbols=ignore-all"
  2. Ignoring errors rather than addressing them feels bad to me.

What is missing is libm with the math.h headers. I got this extra bit below in my error showing that it is trying to link it with -lm. However a certain LLM told me that -lm needs to be linked after -lbitwarden_c.

/usr/bin/gcc -m64 -o $WORK/b001/exe/a.out -Wl,--export-dynamic-symbol=_cgo_panic -Wl,--export-dynamic-
symbol=_cgo_topofstack -Wl,--export-dynamic-symbol=crosscall2 -Wl,--compress-debug-sections=zlib /tmp/go-
link-2769678115/go.o /tmp/go-link-2769678115/000000.o /tmp/go-link-2769678115/000001.o /tmp/go-
link-2769678115/000002.o /tmp/go-link-2769678115/000003.o /tmp/go-link-2769678115/000004.o /tmp/go-
link-2769678115/000005.o /tmp/go-link-2769678115/000006.o /tmp/go-link-2769678115/000007.o /tmp/go-
link-2769678115/000008.o /tmp/go-link-2769678115/000009.o /tmp/go-link-2769678115/000010.o /tmp/go-
link-2769678115/000011.o /tmp/go-link-2769678115/000012.o /tmp/go-link-2769678115/000013.o /tmp/go-
link-2769678115/000014.o /tmp/go-link-2769678115/000015.o /tmp/go-link-2769678115/000016.o /tmp/go-
link-2769678115/000017.o /tmp/go-link-2769678115/000018.o /tmp/go-link-2769678115/000019.o /tmp/go-
link-2769678115/000020.o /tmp/go-link-2769678115/000021.o /tmp/go-link-2769678115/000022.o /tmp/go-
link-2769678115/000023.o /tmp/go-link-2769678115/000024.o /tmp/go-link-2769678115/000025.o -O2 -g -O2 -g -lpthread -
O2 -g -lm -O2 -g -lresolv -O2 -g -lbitwarden_c -L $HOME/go/pkg/mod/github.com/bitwarden/[email protected]/internal/cinterface/
lib/linux-x64 -no-pie

What my ultimate solution turned out to be was to add

/*
#cgo LDFLAGS: -lbitwarden_c -lm
#include <math.h>
*/
import "C"

to my main.go. Explicitly linking -lbitwarden_c -lm in that order.

Update: Not all platforms give the error above, platforms like windows, mac, and other linux distros are fine. This may be a WSL issue (which is what I am using). If you don't want to add the above code to your go code you can also pass the flag to the linker when you install go install -ldflags="-extldflags '-lm'" [YOUR_PACKAGE_HERE]

Nibroc00 avatar Feb 06 '25 20:02 Nibroc00

A possible fix for the dev team if they see this might be to change bitwarden_library.go line 9 to #cgo LDFLAGS: -lbitwarden_c -lm

Nibroc00 avatar Feb 18 '25 21:02 Nibroc00

Thank you @Nibroc00, your solution worked for me! I believe Bitwarden team needs to release a new version of go-sdk with the fix.

debojyoti452 avatar Sep 13 '25 03:09 debojyoti452