使用DLL进行CGo测试

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

CGo tests with a DLL

问题

我目前正在使用CGO通过DLL来使用C库。
一切都正常,我可以在库的基础上使用我的代码,构建等等。

我遇到的问题是如何在DLL中实现测试。目前我的项目结构如下:

  • 项目
    • apis/
      • probes.go
      • probes_test.go <- 使用包含C定义的结构体
    • lib/
      • binding.go <- 定义CGO绑定
    • main.go
    • myLib.dll

所以我的lib文件夹中包含了所有的CGO绑定,以及一个在其上的Go结构体(作为一种包装)。而probes使用这个包装来暴露一些信息。

问题在于执行go test时,执行上下文被设置在包内。这意味着probes_test.go在apis/上下文中执行,并且无法看到DLL。这导致测试以特殊的退出状态失败。

解决方法是暂时在包中复制dll,如下所示:

  • 项目
    • apis/
      • probes.go
      • probes_test.go
      • myLib.dll <- 复制的dll
    • lib/
      • binding.go
    • main.go
    • myLib.dll

这样可以正常工作(测试通过),但是这种方法不够优雅。我还尝试将DLL仅放在lib/中,但也不起作用。

你有办法在测试中指定DLL在其他文件夹中吗?

英文:

I'm currently using CGO to use a library in C through a DLL.
Everything works and I can use my code on top of the library, build it & so on.

The problem I have is to implement tests with a DLL. Right now my project looks like:

  • project
    • apis/
      • probes.go
      • probes_test.go <- uses Struct containing C defs
    • lib/
      • binding.go <- defines CGO binding
    • main.go
    • myLib.dll

So my lib folder got all CGO binding and a Go struct on top of it (as a wrapper kind of). And the probes are using this wrapper to expose some infos.

The problem with doing go test is that the execution context is set within the package. Meaning probes_test.go executes in apis/ context and doesn't see the DLL. Making the test fail with a special exit status.

The workaround is to replicate for now the dll in the package like:

  • project
    • apis/
      • probes.go
      • probes_test.go
      • myLib.dll <- replicated
    • lib/
      • binding.go
    • main.go
    • myLib.dll

And this works (the test passes) but is hacky and not clean. I also tried to put the DLL only in lib/ but isn't working neither.

Do you have a way of specifying for test that the DLL is in an other folder ?

答案1

得分: 1

如果您需要在执行时访问其他路径中的DLL文件,您需要通过将额外的目录添加到PATH环境变量来增加搜索路径范围:

SET PATH=<MY_LIB_DLL_DIR>;%PATH%

通过这种方式,无论您从何处执行程序,都可以找到DLL文件。

在可重定位环境中进行测试(在不同系统的不同目录中进行检出)的推荐做法是使用一个批处理文件打开控制台,该文件设置您构建所需的环境。

echo off
:: 确保当前目录在批处理文件所在的目录中
cd /D %~dp0
:: TODO:将dll_dir替换为您的dll目录
pushd %dll_dir%
:: 设置路径
set PATH=%CD%;%PATH%
popd
:: 在此处打开控制台
cmd /k

将此批处理文件放在项目的根目录中,以从那里打开一个可工作的控制台。

英文:

If you need DLLs from other paths to be accessed during execution time, you have to increase the search path range by adding additional directories to the PATH environment variable:

SET PATH=&lt;MY_LIB_DLL_DIR&gt;;%PATH%

In this way the DLL is found regardless from where you execute the program.

A recommended practice for testing in relocatable environments (checking out on different systems in different directories) is to open a console with a batch file which sets the environment you need for building.

echo off
:: ensure current dir is in directory of batchfile location
cd /D %~dp0 
:: TODO replace dll_dir below your dll directory
pushd %dll_dir%
:: set path
set PATH=%CD%;%PATH%
popd
:: open console here
cmd /k

put this batchfile in the root of your project to open a working console from there

huangapple
  • 本文由 发表于 2022年4月7日 21:50:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/71783408.html
匿名

发表评论

匿名网友

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

确定