buke / quickjs-go

Go bindings to QuickJS
MIT License
71 stars 10 forks source link

【error】测试编译失败 疑似未链接 pthread #356

Open CsVeryLoveXieWenLi opened 1 week ago

CsVeryLoveXieWenLi commented 1 week ago

1. 各类信息

Os: Windows 11 X64 GoLang: go version go1.23.2 windows/amd64 Compiler: Mingw-X64 14.2.0 (x86_64-14.2.0-release-win32-seh-ucrt-rt_v12-rev0) Command: PowerShell 5.1.22621.4111

2. 输出信息

gcc -v

Using built-in specs.
COLLECT_GCC=D:\App\Compress\mingw64\bin\gcc.exe
COLLECT_LTO_WRAPPER=D:/App/Compress/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/14.2.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-14.2.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/buildroot/x86_64-1420-win32-seh-ucrt-rt_v12-rev0/mingw64 --enable-host-shared --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=win32 --enable-libstdcxx-threads=yes --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --enable-libstdcxx-filesystem-ts=yes --disable-libssp --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/buildroot/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/buildroot/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/buildroot/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/buildroot/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-win32-seh-rev0, Built by MinGW-Builds project' --with-bugurl=https://github.com/niXman/mingw-builds LD_FOR_TARGET=/c/buildroot/x86_64-1420-win32-seh-ucrt-rt_v12-rev0/mingw64/bin/ld.exe --with-boot-ldflags='-pipe -fno-ident -L/c/buildroot/x86_64-1420-win32-seh-ucrt-rt_v12-rev0/mingw64/opt/lib -L/c/buildroot/prerequisites/x86_64-zlib-static/lib -L/c/buildroot/prerequisites/x86_64-w64-mingw32-static/lib  -Wl,--disable-dynamicbase -static-libstdc++ -static-libgcc'
Thread model: win32
Supported LTO compression algorithms: zlib
gcc version 14.2.0 (x86_64-win32-seh-rev0, Built by MinGW-Builds project)
g++ -v

Using built-in specs.
COLLECT_GCC=D:\App\Compress\mingw64\bin\g++.exe
COLLECT_LTO_WRAPPER=D:/App/Compress/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/14.2.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-14.2.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/buildroot/x86_64-1420-win32-seh-ucrt-rt_v12-rev0/mingw64 --enable-host-shared --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=win32 --enable-libstdcxx-threads=yes --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --enable-libstdcxx-filesystem-ts=yes --disable-libssp --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/buildroot/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/buildroot/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/buildroot/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/buildroot/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-win32-seh-rev0, Built by MinGW-Builds project' --with-bugurl=https://github.com/niXman/mingw-builds LD_FOR_TARGET=/c/buildroot/x86_64-1420-win32-seh-ucrt-rt_v12-rev0/mingw64/bin/ld.exe --with-boot-ldflags='-pipe -fno-ident -L/c/buildroot/x86_64-1420-win32-seh-ucrt-rt_v12-rev0/mingw64/opt/lib -L/c/buildroot/prerequisites/x86_64-zlib-static/lib -L/c/buildroot/prerequisites/x86_64-w64-mingw32-static/lib  -Wl,--disable-dynamicbase -static-libstdc++ -static-libgcc'
Thread model: win32
Supported LTO compression algorithms: zlib
gcc version 14.2.0 (x86_64-win32-seh-rev0, Built by MinGW-Builds project)
go env

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\CsVlXwl\AppData\Local\go-build
set GOENV=C:\Users\CsVlXwl\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\CsVlXwl\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\CsVlXwl\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=D:\App\Install\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=D:\App\Install\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.23.2
set GODEBUG=
set GOTELEMETRY=local
set GOTELEMETRYDIR=C:\Users\CsVlXwl\AppData\Roaming\go\telemetry
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\CsVlXwl\AppData\Local\Temp\go-build1384828636=/tmp/go-build -gno-record-gcc-switches

3. 使用代码

/*
 * @Author: CsVeryLoveXieWenLi
 * @LastEditTime: 2024-11-16 13:31:49
 * @Description: 入口
 * Copyright (c) 2024 by CsVeryLoveXieWenLi, All Rights Reserved.
 */

package main

import (
    "fmt"

    "github.com/buke/quickjs-go"
)

func main() {
    // Create a new runtime
    rt := quickjs.NewRuntime(
        quickjs.WithExecuteTimeout(30),
        quickjs.WithMemoryLimit(128*1024),
        quickjs.WithGCThreshold(256*1024),
        quickjs.WithMaxStackSize(65534),
        quickjs.WithCanBlock(true),
    )
    defer rt.Close()

    // Create a new context
    ctx := rt.NewContext()
    defer ctx.Close()

    ret, err := ctx.Eval("'Hello ' + 'QuickJS!'")
    if err != nil {
        println(err.Error())
    }
    fmt.Println(ret.String())
}

4. 编译输出

go build

# QJs
D:\App\Install\Go\pkg\tool\windows_amd64\link.exe: running gcc failed: exit status 1
D:\App\Compress\mingw64\bin\gcc.exe -m64 -mconsole -Wl,--tsaware -Wl,--nxcompat -Wl,--major-os-version=6 -Wl,--minor-os-version=1 -Wl,--major-subsystem-version=6 -Wl,--minor-subsystem-version=1 -Wl,--dynamicbase -Wl,--high-entropy-va -o $WORK\b001\exe\a.out.exe -Wl,--no-insert-timestamp C:\Users\CsVlXwl\AppData\Local\Temp\go-link-3349602128\go.o C:\Users\CsVlXwl\AppData\Local\Temp\go-link-3349602128\000000.o C:\Users\CsVlXwl\AppData\Local\Temp\go-link-3349602128\000001.o C:\Users\CsVlXwl\AppData\Local\Temp\go-link-3349602128\000002.o C:\Users\CsVlXwl\AppData\Local\Temp\go-link-3349602128\000003.o C:\Users\CsVlXwl\AppData\Local\Temp\go-link-3349602128\000004.o C:\Users\CsVlXwl\AppData\Local\Temp\go-link-3349602128\000005.o C:\Users\CsVlXwl\AppData\Local\Temp\go-link-3349602128\000006.o C:\Users\CsVlXwl\AppData\Local\Temp\go-link-3349602128\000007.o C:\Users\CsVlXwl\AppData\Local\Temp\go-link-3349602128\000008.o C:\Users\CsVlXwl\AppData\Local\Temp\go-link-3349602128\000009.o C:\Users\CsVlXwl\AppData\Local\Temp\go-link-3349602128\000010.o C:\Users\CsVlXwl\AppData\Local\Temp\go-link-3349602128\000011.o C:\Users\CsVlXwl\AppData\Local\Temp\go-link-3349602128\000012.o C:\Users\CsVlXwl\AppData\Local\Temp\go-link-3349602128\000013.o C:\Users\CsVlXwl\AppData\Local\Temp\go-link-3349602128\000014.o -O2 -g -LC:/Users/CsVlXwl/go/pkg/mod/github.com/buke/quickjs-go@v0.4.15/deps/libs/windows_amd64 -lquickjs -lm -O2 -g -Wl,-T,C:\Users\CsVlXwl\AppData\Local\Temp\go-link-3349602128\fix_debug_gdb_scripts.ld -Wl,--start-group -lmingwex -lmingw32 -Wl,--end-group -lkernel32
D:/App/Compress/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/CsVlXwl/go/pkg/mod/github.com/buke/quickjs-go@v0.4.15/deps/libs/windows_amd64/libquickjs.a(quickjs.nolto.o): in function `JS_NewClassID':
D:/a/quickjs-go/quickjs-go/deps/quickjs/quickjs.c:3389:(.text+0x6075): undefined reference to `pthread_mutex_lock'
D:/App/Compress/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/a/quickjs-go/quickjs-go/deps/quickjs/quickjs.c:3397:(.text+0x6094): undefined reference to `pthread_mutex_unlock'
D:/App/Compress/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/CsVlXwl/go/pkg/mod/github.com/buke/quickjs-go@v0.4.15/deps/libs/windows_amd64/libquickjs.a(quickjs.nolto.o): in function `js_atomics_notify':
D:/a/quickjs-go/quickjs-go/deps/quickjs/quickjs.c:55844:(.text+0x3abb0): undefined reference to `pthread_mutex_lock'
D:/App/Compress/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/a/quickjs-go/quickjs-go/deps/quickjs/quickjs.c:55859:(.text+0x3ac45): undefined reference to `pthread_cond_signal'
D:/App/Compress/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/a/quickjs-go/quickjs-go/deps/quickjs/quickjs.c:55861:(.text+0x3ac56): undefined reference to `pthread_mutex_unlock'
D:/App/Compress/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/CsVlXwl/go/pkg/mod/github.com/buke/quickjs-go@v0.4.15/deps/libs/windows_amd64/libquickjs.a(quickjs.nolto.o): in function `js_atomics_wait':
D:/a/quickjs-go/quickjs-go/deps/quickjs/quickjs.c:55776:(.text+0x40614): undefined reference to `pthread_mutex_lock'
D:/App/Compress/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/a/quickjs-go/quickjs-go/deps/quickjs/quickjs.c:55789:(.text+0x4064f): undefined reference to `pthread_cond_init'
D:/App/Compress/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/a/quickjs-go/quickjs-go/deps/quickjs/quickjs.c:55798:(.text+0x406a8): undefined reference to `clock_gettime'
D:/App/Compress/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/a/quickjs-go/quickjs-go/deps/quickjs/quickjs.c:55805:(.text+0x4070e): undefined reference to `pthread_cond_timedwait'
D:/App/Compress/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/a/quickjs-go/quickjs-go/deps/quickjs/quickjs.c:55810:(.text+0x40746): undefined reference to `pthread_mutex_unlock'
D:/App/Compress/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/a/quickjs-go/quickjs-go/deps/quickjs/quickjs.c:55811:(.text+0x4074e): undefined reference to `pthread_cond_destroy'
D:/App/Compress/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/a/quickjs-go/quickjs-go/deps/quickjs/quickjs.c:55783:(.text+0x407d3): undefined reference to `pthread_mutex_unlock'
D:/App/Compress/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/a/quickjs-go/quickjs-go/deps/quickjs/quickjs.c:55794:(.text+0x407ff): undefined reference to `pthread_cond_wait'
D:/App/Compress/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/a/quickjs-go/quickjs-go/deps/quickjs/quickjs.c:55810:(.text+0x40864): undefined reference to `pthread_mutex_unlock'
D:/App/Compress/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/a/quickjs-go/quickjs-go/deps/quickjs/quickjs.c:55811:(.text+0x4086c): undefined reference to `pthread_cond_destroy'
collect2.exe: error: ld returned 1 exit status

5. 项目结构

image

CsVeryLoveXieWenLi commented 1 week ago

当CGO_ENABLED=1, 进行编译时, 会将文件中引用libc的库(比如常用的net包),以动态链接的方式生成目标文件。 当CGO_ENABLED=0, 进行编译时, 则会把在目标文件中未定义的符号(外部函数)一起链接到可执行文件中。

不明所以,但是不启用则编译成功,或许是符号没定义就检测了。

buke commented 1 week ago

谢谢反馈 ,另外windows 编译可参考 https://github.com/buke/quickjs-go/issues/151#issuecomment-2134307728

CsVeryLoveXieWenLi commented 1 week ago

我又看了一会,确实是CGO的开关? 如果不启用,使用了CGO的文件会被排除在外,导致缺定义,并非编译成功。

CsVeryLoveXieWenLi commented 1 week ago

谢谢反馈 ,另外windows 编译可参考 #151 (comment)

谢谢您的答复,另外问一下,怎么链接动态库呢?我编译了动态库,也生成了.a文件,相当于vc的.lib文件。 我也查看了符号导出,可是,不论我如何在CGO中编写链接信息,最终结果就是找不到导出的函数。

buke commented 1 week ago

原quickjs https://github.com/bellard/quickjs 并不支持编译为动态库,因此本项目是采取预编译为静态库,然后通过cgo调用。关于编译静态库的步骤请参考 https://github.com/buke/quickjs-go/blob/main/.github/workflows/build_static_lib.yml

CsVeryLoveXieWenLi commented 1 week ago

原quickjs https://github.com/bellard/quickjs 并不支持编译为动态库,因此本项目是采取预编译为静态库,然后通过cgo调用。关于编译静态库的步骤请参考 https://github.com/buke/quickjs-go/blob/main/.github/workflows/build_static_lib.yml

好的,谢谢。

CsVeryLoveXieWenLi commented 1 week ago

链接pthread后,则编译成功

image

CsVeryLoveXieWenLi commented 1 week ago

经过我的再次编译,如果使用了win32协议的线程模型,链接时会报错,如果使用posix协议的线程模型,则不会。

CsVeryLoveXieWenLi commented 1 week ago

有没有可能判断并链接?

CsVeryLoveXieWenLi commented 1 week ago

奇怪,我搜索了一下,又说win32没有多线程,posix有?不知道,目前来看,使用posix即可,这也是推荐使用的线程模型。