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 C#读写EXCEL文件,不同编译
  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置