英文:
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':
dlclose'
stdlib_system.c:(.text+0xe6f0): undefined reference to
/my/path/to/target/libtarget.a(stdlib_system.o): In function nimLoadLibrary':
dlopen'
stdlib_system.c:(.text+0xe71b): undefined reference to
/my/path/to/target/libtarget.a(stdlib_system.o): In function nimGetProcAddr':
dlsym'
stdlib_system.c:(.text+0xe750): undefined reference to
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论