你使用的Go版本是什么( go version
)?
$ go version
go version go1.20.3 android/arm64
这个问题在最新版本中是否重现?
你正在使用什么操作系统和处理器架构( go env
)?
go env
输出
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/data/data/com.termux/files/home/.cache/go-build"
GOENV="/data/data/com.termux/files/home/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="android"
GOINSECURE=""
GOMODCACHE="/data/data/com.termux/files/home/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="android"
GOPATH="/data/data/com.termux/files/home/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/data/data/com.termux/files/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/data/data/com.termux/files/usr/lib/go/pkg/tool/android_arm64"
GOVCS=""
GOVERSION="go1.20.3"
GCCGO="gccgo"
AR="ar"
CC="aarch64-linux-android-clang"
CXX="aarch64-linux-android-clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -pthread -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/data/data/com.termux/files/usr/tmp/go-build3459929408=/tmp/go-build -gno-record-gcc-switches"
你做了什么?
我编写了一个名为Foo()的Go函数,它调用os.UserConfigDir()并打印结果或错误。通过CGO将Foo()函数导出到共享库,然后在c程序中调用它。
~/issue $ ls -R
.:
foo.c test
./test:
go.mod test.go
~/issue $ cat foo.c
#include
#include
#include
#include
int main() {
// Call the Foo() function and print the result
char* result = Foo();
printf("Result: %s\n", result);
return 0;
}
~/issue $ cat test/test.go
package main
import (
"C"
"os"
)
func main() {
}
//export Foo
func Foo() *C.char{
dir, err := os.UserConfigDir()
if err != nil {
return C.CString(err.Error())
}
return C.CString(dir)
}
~/issue $ cat test/go.mod
module mytest
go 1.20
~/issue $ cd test
~/issue/test $ go build -v -buildmode=c-shared -o libtest.so
runtime/cgo
mytest
~/issue/test $ cd ../
~/issue $ cc foo.c -I./test -L./test -ltest
~/issue $ LD_LIBRARY_PATH=./test ./a.out
Result: neither $XDG_CONFIG_HOME nor $HOME are defined
你期望看到什么?
$ LD_LIBRARY_PATH=./test ./a.out
/data/data/com.termux/files/home/.config
你实际看到了什么?
$ LD_LIBRARY_PATH=./test ./a.out
Result: neither $XDG_CONFIG_HOME nor $HOME are defined
4条答案
按热度按时间jbose2ul1#
CC @golang/android。
pgvzfuti2#
相同的代码在Linux和Mac下运行正常。
4dc9hkyq3#
A Go共享库使用
-buildmode=c-shared
构建,依赖动态链接器支持获取命令行参数和环境变量。可能Android动态链接器不支持这一点。这基本上与#13492的问题相同。hts6caw34#
不确定go build是否使用与c相同的链接器。但我确实在c中编写了一个类似的共享库,该库的代码也在库中读取环境变量,在Android下运行正常。