How to use a relative path for LDFLAGS in golang

为君一笑 提交于 2019-12-03 14:38:00

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.

eyotang

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!