How to use a relative path for LDFLAGS in golang

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

How to use a relative path for LDFLAGS in golang

问题

我正在尝试构建一个使用静态库(.a文件)的Golang程序。

我的项目目录结构如下:

└─testserver
    ├─bin
    ├─pkg
    └─src
        ├─logging
        └─testserver
		    ├─libtest.a
            └─test.go

test.go中的cgo标志如下:

// #cgo LDFLAGS: -L /home/test/testserver/src/testserver -ltest
// #include "test.h"
import "C"

当我在LDFLAGS -L中使用绝对路径时,它可以正常工作,但是当我将路径更改为相对路径时,例如

// #cgo LDFLAGS: -L ./testserver -ltest

然后运行命令

go install testserver

它会返回一个错误,说“找不到-ltest”。

我的问题是如何在LDFLAGS中使用相对路径,以便我可以在任何路径下构建项目。

英文:

I am trying to build a golang program which uses a static lib (.a file)

the directory struct for my project as below

└─testserver
    ├─bin
    ├─pkg
    └─src
        ├─logging
        └─testserver
		    ├─libtest.a
            └─test.go

the flags for cgo in test.go as below

// #cgo LDFLAGS: -L /home/test/testserver/src/testserver -ltest
// #include "test.h"
import "C"

when I am using absolute path for LDFLAGS -L, it works fines, but when I change the path to a relative path, eg

// #cgo LDFLAGS: -L ./testserver -ltest

and then run the command

go install testserver

it returns an error to me, and says "cannot find -ltest"

my question is how can I use a relative path in LDFLAGS ? , so that I can build the project in any path.

答案1

得分: 12

你目前无法这样做。目录在构建命令和链接之间发生了变化。现在,你要么需要链接到绝对路径,要么使用CGO_LDFLAGS环境变量。

在go1.4之后的一个提交中,添加了一个${SRCDIR}变量,该变量在构建时被替换为包含源文件的目录的绝对路径。https://github.com/golang/go/issues/7891。这将在go1.5中实现,并且你可以通过从源代码构建Go来轻松使用它。

英文:

You currently can't. The directory changes between the time the command is built, and linking. For now you either need to link to an absolute path, or use the CGO_LDFLAGS environment variable.

There was a commit just after go1.4 which added a ${SRCDIR} variable which is replaced by the absolute path to the directory containing the source file at build time. https://github.com/golang/go/issues/7891. This will be in go1.5, and you can easily use it now by building Go from source.

答案2

得分: 0

使用**${SRCDIR}来解决相对路径**问题真的非常好。

此外,${SRCDIR}表示当前Go文件的绝对路径**。
使用命令go build -x .来检查输出。

$ go build -x .
...
cd /root/sourcecode/src/tcp/aes
CGO_LDFLAGS="-g" "-O2" "-L/root/sourcecode/src/tcp/aes/aes" "-laes" /usr/local/go/pkg/tool/linux_amd64/cgo -objdir $WORK/tcp/aes/_obj/ -importpath tcp/aes -- -I $WORK/tcp/aes/_obj/ aes.go
cd $WORK
...
$ tcp/aes
/usr/bin/ld: cannot find -laes
collect2: error: ld returned 1 exit status

这是错误的,因为库libaes.a与Go文件位于同一位置。
然后我进行了更改,并通过了。

英文:

It's really very great to use ${SRCDIR} to solve the problem of relative path.

In addition, the ${SRCDIR} indicates the absolute path of current go file.
Use command go build -x . to check the output.

$ go build -x .
...
cd /root/sourcecode/src/tcp/aes
CGO_LDFLAGS="-g" "-O2" "-L/root/sourcecode/src/tcp/aes/aes" "-laes" /usr/local/go/pkg/tool/linux_amd64/cgo -objdir $WORK/tcp/aes/_obj/ -importpath tcp/aes -- -I $WORK/tcp/aes/_obj/ aes.go
cd $WORK
...
$ tcp/aes
/usr/bin/ld: cannot find -laes
collect2: error: ld returned 1 exit status

It's incrrect, because the lib libaes.a locates the same as the go file.
Then I changed it, and passed.

huangapple
  • 本文由 发表于 2015年1月20日 12:36:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/28037827.html
匿名

发表评论

匿名网友

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

确定