将Nim代码静态链接到Go

Lye*_*ish 6 go nim-lang

我正在尝试在Linux中将Nim中创建的一些代码静态链接到Go应用程序中.我已经关注了Nim Backend Integration文档和一些关于在Go中链接C但但没有得到它的文章.

这是我到目前为止的地方......


尼姆代码target.nim:

proc testnim* {.exportc.} =
  echo "In Nim!"
Run Code Online (Sandbox Code Playgroud)

我编译它:

nim c --app:staticLib --noMain --header target.nim
Run Code Online (Sandbox Code Playgroud)

去代码app.go:

package main

/*
#cgo CFLAGS: -I/my/path/to/target/nimcache
#cgo CFLAGS: -I/my/path/to/Nim/lib
#cgo LDFLAGS: /my/path/to/target/libtarget.a
#include "/my/path/to/target/nimcache/target.h"
*/
import "C"
import "fmt"

func main() {
  fmt.Println("In Go!")
  C.NimMain()
  C.testnim()
}
Run Code Online (Sandbox Code Playgroud)

我尝试构建它们:

go build

go build --ldflags '-extldflags "-static"' app.go
Run Code Online (Sandbox Code Playgroud)

这是我得到的:

# command-line-arguments
/my/path/to/target/libtarget.a(stdlib_system.o): In function `nimUnloadLibrary':
stdlib_system.c:(.text+0xe6f0): undefined reference to `dlclose'
/my/path/to/target/libtarget.a(stdlib_system.o): In function `nimLoadLibrary':
stdlib_system.c:(.text+0xe71b): undefined reference to `dlopen'
/my/path/to/target/libtarget.a(stdlib_system.o): In function `nimGetProcAddr':
stdlib_system.c:(.text+0xe750): undefined reference to `dlsym'
collect2: error: ld returned 1 exit status

所以我错过了一些东西.我正在使用Go 1.5和Nim 0.11.3(开发分支).任何建议或提示将不胜感激.

Jim*_*imB 5

您缺少 libdl 库。添加-ldl到您的 LDFLAGS