我正在 docker 容器 ( golang:1.18-bullseye) 中運行 go 程式。
我嘗試使用go run main.go和運行它go run .
我的代碼看起來像這樣,兩個頭檔案都位于IncludeCFLAGS 中給出的目錄中:
/*
#cgo LDFLAGS: -Lvendor/MyCoolLibrary/1.14.2/Bin/Linux/libSDK-Linux-Shipping.so
#cgo CFLAGS: -I vendor/MyCoolLibrary/1.14.2/Include/
#include "my_cool_sdk.h"
#include "my_cool_logging.h"*/
import "C"
import (
"fmt"
"log"
"os"
"runtime"
)
func main() {
ret := C.MyCoolFunc()
}
當我運行此代碼時,我收到以下錯誤訊息:
/usr/bin/ld: $WORK/b001/_x002.o: in function `_cgo_6bb9bcf96ac6_Cfunc_MyCoolFunc':
/tmp/go-build/cgo-gcc-prolog:110: undefined reference to `MyCoolFunc'
我怎樣才能解決這個問題?
編輯:我將標題更改為:
/*
#cgo LDFLAGS: -L${SRCDIR}/vendor/MyCoolLibrary/1.14.2/Bin/Linux/libSDK-Linux-Shipping.so -lSDK-Linux-Shipping
#cgo CFLAGS: -I ${SRCDIR}/vendor/MyCoolLibrary/1.14.2/Include/
#include "my_cool_sdk.h"
#include "my_cool_logging.h"*/
并運行go build -x .,這是輸出:
WORK=/tmp/go-build1175764972
mkdir -p $WORK/b001/
cd /home/helloworld
TERM='dumb' CGO_LDFLAGS='"-g" "-O2" "-L/home/helloworld/vendor/MyCoolLibrary/1.14.2/Bin/Linux/libSDK-Linux-Shipping.so" "-lSDK-Linux-Shipping"' /usr/local/go/pkg/tool/linux_amd64/cgo -objdir $WORK/b001/ -importpath go-sandbox -- -I $WORK/b001/ -g -O2 -I ./vendor/MyCoolLibrary/1.14.2/Include/ ./main.go
cd $WORK
gcc -fno-caret-diagnostics -c -x c - -o /dev/null || true
gcc -Qunused-arguments -c -x c - -o /dev/null || true
gcc -fdebug-prefix-map=a=b -c -x c - -o /dev/null || true
gcc -gno-record-gcc-switches -c -x c - -o /dev/null || true
cd $WORK/b001
TERM='dumb' gcc -I /home/helloworld -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -I /home/helloworld/vendor/MyCoolLibrary/1.14.2/Include/ -o ./_x001.o -c _cgo_export.c
TERM='dumb' gcc -I /home/helloworld -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -I /home/helloworld/vendor/MyCoolLibrary/1.14.2/Include/ -o ./_x002.o -c main.cgo2.c
TERM='dumb' gcc -I /home/helloworld -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -I /home/helloworld/vendor/MyCoolLibrary/1.14.2/Include/ -o ./_cgo_main.o -c _cgo_main.c
cd /home/helloworld
TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -o $WORK/b001/_cgo_.o $WORK/b001/_cgo_main.o $WORK/b001/_x001.o $WORK/b001/_x002.o -g -O2 -L/home/helloworld/vendor/MyCoolLibrary/1.14.2/Bin/Linux/libSDK-Linux-Shipping.so -lSDK-Linux-Shipping
# go-sandbox
/usr/bin/ld: cannot find -lSDK-Linux-Shipping
collect2: error: ld returned 1 exit status
uj5u.com熱心網友回復:
我能夠重現并解決此問題。還有一些額外的陷阱。從專注于跑步開始go build:
好的,所以 go 編譯器找到了頭檔案,但找不到共享庫。我認為您針對該問題稍微修改了代碼,這不是問題,但是-Lin的路徑LDFLAGS必須是:
- 相對于源目錄使用
${SRCDIR} - 絕對路徑
- 完全避免這種情況并利用 pkg-config 我只是使用包含
so檔案的相對目錄作為-L.
好的,除此之外,您還必須-l在 LDFLAGS 中提供一個引數以在您指向的路徑中查找檔案(即:libvendera.so需要-lvendora)。
一旦go build作業,你有一個應用程式仍然需要知道檔案在哪里so運行(因此是一個共享庫)。為此,您可能需要設定LD_LIBRARY_PATH并指向包含該so檔案的目錄,就像您使用-L.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/457422.html
上一篇:GO:在任何一個中都找不到包“ihelp/pkg/ihelp”
下一篇:io閱讀器客戶端使用方法
