duanhuokuang5280 2015-01-20 04:36 采纳率: 100%
浏览 3620
已采纳

如何在Golang中为LDFLAGS使用相对路径

我试图建立一个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中使用相对路径?,这样我就可以在任何路径上构建项目。

  • 写回答

2条回答 默认 最新

  • dtkwt62022 2015-01-20 15:54
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的