链接Go程序与GNU readline静态库

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

Link go program vs GNU readline statically

问题

我正在编写一个使用GNU readline库的Go程序,用于创建一个漂亮的命令行界面。为了简化安装过程并且不用担心库的版本和其他问题,我想要进行静态链接。

问题是我不太清楚如何做到这一点。如果我预编译库,我将不得不提供几个版本的代码,每个版本都使用不同版本的.a或.lib readline库。为了避免这个问题,我考虑将当前的readline代码包含到我的Go项目中,并让Go工具在构建项目时编译它。然而,要构建readline库,需要使用make命令。有没有办法告诉Go工具如何构建C代码呢?

英文:

I'm writing a Go program that uses the GNU readline library for a fancy command line interface. In order to simplify the installing process and not worry about the library version and other stuff, I want to link it statically.

The problem is I don't really know how to do it. If I precompile the library, I would have to provide several versions of my code, with the different versions of the .a or .lib readline library. To avoid this problem I was thinking of just including the current readline code to my go project, and let the go tool compile it when it build the go project. However, to build the readline library, is necessary to use make. Is there a way of telling the go tool how to build the C code?

答案1

得分: 5

是的,你当然可以这样做。我最近在另一个项目中做了类似的事情,主要是因为该代码不作为库提供(Ubuntu只编译了命令行工具)。为了实现这一点,我使用了一些我认为在大多数系统中都合理的选项来运行autoconf脚本,并将C代码以及自动生成的config.h头文件复制到Go包目录中。然后,我使用make编译了原始的C代码,并观察了gcc在编译和链接时使用的选项,并将适当的选项复制到cgo的LDFLAGS和CFLAGS选项中(你也可以查看Makefile,但这样做更容易)。

还有几点需要注意的地方:

  • 你考虑过在Go中自己完成readline的工作吗?ssh终端包至少作为一个很好的起点,如果它不能完全解决你的问题的话。

  • 请记住,尽管readline是一个库,但它是GPL许可的。如果你链接或嵌入它,你的软件也必须使用GPL许可。如果你在意的话,还有其他类似的库可用,它们的许可要求没有那么严格。

英文:

Yes, you can certainly do that. I've recently done something similar with a different project, mainly because the code was not available as a library (Ubuntu compiles just the command line tool for it). To achieve it, I've run the autoconf script with options that I figured would be sensible in most systems, and copied the C code together with the automatically built config.h header file into the Go package directory. Then, I've built the original C code with make once and observed which options gcc would get while compiling and linking it, and copied the appropriate ones into cgo's LDFLAGS and CFLAGS options (you can also inspect the Makefile, but that was easier).

A couple of side notes:

  • Have you considered doing the readline work in Go itself? The ssh terminal package works at least as a pretty good seed, if it doesn't solve your problem completely.

  • Remember that readline, although being a library, is GPL. You'll necessarily have to license your own software as GPL as well if you link or embed it. There are other smilar libraries available with less strict licenses, if you care.

答案2

得分: 1

我建议避免使用readline,存在更好的替代方案,比如https://github.com/edsrzf/fineline。

英文:

I recommend avoiding readline, better alternatives exist; like https://github.com/edsrzf/fineline

huangapple
  • 本文由 发表于 2013年9月10日 02:57:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/18705030.html
匿名

发表评论

匿名网友

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

确定