将Nim代码静态链接到Go中

huangapple go评论92阅读模式
英文:

Statically linking Nim code to Go

问题

我正在尝试在Linux中将一些用Nim编写的代码静态链接到Go应用程序中。我已经按照Nim后端集成文档和一些关于在Go中链接C的文章进行了操作,但还没有成功。

以下是我目前的进展...


Nim代码 target.nim:

proc testnim* {.exportc.} =
  echo "In Nim!"

我使用以下命令编译它:

nim c --app:staticLib --noMain --header target.nim

Go代码 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()
}

我尝试使用以下命令构建它们:

go build

go build --ldflags '-extldflags "-static"' app.go

这是我得到的结果:

# 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(开发分支)。任何建议或提示将不胜感激。

英文:

I'm trying in Linux to statically link some code created in Nim into a Go application. I've followed the Nim Backend Integration docs and some articles for linking C in Go but haven't gotten it working.

Here's where I'm at so far...


Nim code target.nim:

proc testnim* {.exportc.} =
  echo "In Nim!"

I compile it with:

nim c --app:staticLib --noMain --header target.nim

Go code 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()
}

I tried building it both of these:

go build

go build --ldflags '-extldflags "-static"' app.go

Here's what I get:

<pre>

command-line-arguments

/my/path/to/target/libtarget.a(stdlib_system.o): In function nimUnloadLibrary&#39;:
stdlib_system.c:(.text+0xe6f0): undefined reference to
dlclose'
/my/path/to/target/libtarget.a(stdlib_system.o): In function nimLoadLibrary&#39;:
stdlib_system.c:(.text+0xe71b): undefined reference to
dlopen'
/my/path/to/target/libtarget.a(stdlib_system.o): In function nimGetProcAddr&#39;:
stdlib_system.c:(.text+0xe750): undefined reference to
dlsym'
collect2: error: ld returned 1 exit status
</pre>


So I'm missing something(s). I'm using Go 1.5 and Nim 0.11.3 (devel branch). Any advice or hints would be much appreciated.

答案1

得分: 5

你缺少libdl库。在你的LDFLAGS中添加-ldl

英文:

You're missing the libdl library. Add -ldl to your LDFLAGS

huangapple
  • 本文由 发表于 2015年9月11日 05:40:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/32512065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定