阅读Statically compiled Go programs, always, even with cgo, using musl后,我尝试使用描述的方法静态链接LuaJIT。
Go代码我尝试构建:
package main
import "github.com/aarzilli/golua/lua"
import "fmt"
func test(L *lua.State) int {
fmt.Println("hello world! from go!")
return 0
}
func test2(L *lua.State) int {
arg := L.CheckInteger(-1)
argfrombottom := L.CheckInteger(1)
fmt.Print("test2 arg: ")
fmt.Println(arg)
fmt.Print("from bottom: ")
fmt.Println(argfrombottom)
return 0
}
func main() {
L := lua.NewState()
defer L.Close()
L.OpenLibs()
L.GetField(lua.LUA_GLOBALSINDEX, "print")
L.PushString("Hello World!")
L.Call(1, 0)
L.PushGoFunction(test)
L.PushGoFunction(test)
L.PushGoFunction(test)
L.PushGoFunction(test)
L.PushGoFunction(test2)
L.PushInteger(42)
L.Call(1, 0)
L.Call(0, 0)
L.Call(0, 0)
L.Call(0, 0)
// this will fail as we didn't register test2 function
err := L.DoString("test2(42)")
fmt.Printf("Ciao %v\n", err)
}
使用参数和输出构建命令:
$ CC="/usr/local/musl/bin/musl-gcc" go build --ldflags '-linkmode external -extldflags "-static"' basic.go
# command-line-arguments
/usr/local/go/pkg/tool/linux_amd64/link: running /usr/local/musl/bin/musl-gcc failed: exit status 1
/usr/bin/ld: cannot find -lluajit-5.1
collect2: error: ld returned 1 exit status
使用LD_DEBUG=all
我可以获得more information(8000+行)。
我的问题:问题是什么以及如何解决?我认为可能会有一些提示here但我无法理解它。
我采取了以下步骤:
$ make STATIC_CC="/usr/local/musl/bin/musl-gcc" CCOPT="-static -fPIC" BUILDMODE="static"
...
==== Successfully built LuaJIT 2.0.4 ====
$ make DYNAMIC_CC="/usr/local/musl/bin/musl-gcc" BUILDMODE="dynamic"
...
==== Successfully built LuaJIT 2.0.4 ====
$ find . -iname *.a -o -iname *.so
./src/libluajit.a
./src/libluajit.so
$ sudo make install
...
==== Successfully installed LuaJIT 2.0.4 to /usr/local ====
$ pkg-config luajit --cflags
-I/usr/include/luajit-2.0
$ pkg-config luajit --libs
-lluajit-5.1
lua.go
文件以使用硬编码的cgo
参数。我在覆盖这些值时遇到了问题,因此我只是修改了源代码。
带有C
参数的<{{{{{{{{{{{<{{<{>}
cgo
新:
/*
#cgo CFLAGS: -I ${SRCDIR}/lua
#cgo llua LDFLAGS: -llua
#cgo luaa LDFLAGS: -llua -lm -ldl
#cgo linux,!llua,!luaa LDFLAGS: -llua5.1
#cgo darwin,!luaa pkg-config: lua5.1
#cgo freebsd,!luaa LDFLAGS: -llua-5.1
#cgo windows,!llua LDFLAGS: -L${SRCDIR} -llua -lmingwex -lmingw32
#include <lua.h>
#include <stdlib.h>
#include "golua.h"
*/
import "C"
如问题的开头所示。
/*
#cgo CFLAGS: -I/usr/include/luajit-2.0 -I${SRCDIR}/lua
#cgo LDFLAGS: -lluajit-5.1
#include <lua.h>
#include <stdlib.h>
#include "golua.h"
*/
import "C"
我已关注@putu's评论,并将$ find / -iname libluajit* 2> /dev/null
/usr/local/lib/libluajit-5.1.so.2
/usr/local/lib/libluajit-5.1.a
/usr/local/lib/libluajit-5.1.so
/usr/local/lib/libluajit-5.1.so.2.0.4
/usr/lib/libluajit-5.1.so.2.0.5
/usr/lib/libluajit-5.1.so.2
/usr/lib/libluajit-5.1.a
/usr/lib/libluajit-5.1.so
/usr/lib/libluajit.s
更改为
#cgo LDFLAGS
现在我有
#cgo LDFLAGS: -L/usr/local/lib -lluajit-5.1
答案 0 :(得分:0)
@putu向我指出了正确的方向,我所要做的就是调整CFLAGS
以包含musl
。
在C
:
lua.go
条评论
/*
#cgo CFLAGS: -I/usr/include/luajit-2.0 -I/usr/local/musl/include/
#cgo LDFLAGS: -L/usr/local/lib -lluajit-5.1
#include <lua.h>
#include <stdlib.h>
#include "golua.h"
*/
import "C"
看起来一切都很好:
$ CC="/usr/local/musl/bin/musl-gcc" go build --ldflags '-linkmode external -extldflags "-static"' basic.go
$ file basic
basic: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
$ ./basic
Hello World!
test2 arg: 42
from bottom: 42
hello world! from go!
hello world! from go!
hello world! from go!
Ciao [string "test2(42)"]:1: attempt to call global 'test2' (a nil value)