为什么我无法使用CGO将DLL链接到包文件夹中?

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

Why can't I link to this DLL in a package folder using CGO?

问题

我正在尝试使用CGO来使用realsense2.dll(Intel的RealSense相机库)。我使用的是Windows 11 x64。

当我的目录布局如下时,我的测试程序可以正常工作,可以计算连接的设备数量:

  • rs2test
    • go.mod
    • realsense2.dll
    • context.go(C代码的包装器)
    • devicelist.go(C代码的包装器)
    • error.go(C代码的包装器)
    • main.go

在每个C代码的包装器文件中,我有以下指令:

/*
#cgo CFLAGS: -I../../../include/librealsense2
#cgo LDFLAGS: -L${SRCDIR} -lrealsense2
#include "../../../include/librealsense2/rs.h"
#include "../../../include/librealsense2/h/rs_context.h"
#include "../../../include/librealsense2/h/rs_pipeline.h"
#include "../../../include/librealsense2/h/rs_option.h"
#include "../../../include/librealsense2/h/rs_frame.h"
*/

然而,当我尝试将包装器文件放入它们自己的包中时,布局如下:

  • rs2test
    • rs2/context.go
    • rs2/devicelist.go
    • rs2/error.go
    • rs2/realsense2.dll
    • go.mod
    • main.go

并将指令更改为以下内容:

#cgo CFLAGS: -I../../../../include/librealsense2
#cgo LDFLAGS: -L${SRCDIR} -lrealsense2
#include "../../../../include/librealsense2/rs.h"
#include "../../../../include/librealsense2/h/rs_context.h"
#include "../../../../include/librealsense2/h/rs_pipeline.h"
#include "../../../../include/librealsense2/h/rs_option.h"
#include "../../../../include/librealsense2/h/rs_frame.h"

我得到了退出状态0xc0000135,Google说这意味着应用程序初始化失败。

我还尝试过 -L${SRCDIR}/rs2,但是它说找不到 -lrealsense2。

英文:

I am trying to use CGO to use realsense2.dll (Intel's RealSense camera library). I am on Windows 11 x64.

My test program that counts the number of devices connected works when my directory layout is:

  • rs2test
    • go.mod
    • realsense2.dll
    • context.go (wrapper for C code)
    • devicelist.go (wrapper for C code)
    • error.go (wrapper for C code)
    • main.go

and in each wrapper for C code file I have the following directives:

/*
#cgo CFLAGS: -I../../../include/librealsense2
#cgo LDFLAGS: -L${SRCDIR} -lrealsense2
#include "../../../include/librealsense2/rs.h"
#include "../../../include/librealsense2/h/rs_context.h"
#include "../../../include/librealsense2/h/rs_pipeline.h"
#include "../../../include/librealsense2/h/rs_option.h"
#include "../../../include/librealsense2/h/rs_frame.h"
*/

However, when I try to put the wrapper files into their own package like this:

  • rs2test
    • rs2/context.go
    • rs2/devicelist.go
    • rs2/error.go
    • rs2/realsense2.dll
    • go.mod
    • main.go

And change the directives to this:

#cgo CFLAGS: -I../../../../include/librealsense2
#cgo LDFLAGS: -L${SRCDIR} -lrealsense2
#include "../../../../include/librealsense2/rs.h"
#include "../../../../include/librealsense2/h/rs_context.h"
#include "../../../../include/librealsense2/h/rs_pipeline.h"
#include "../../../../include/librealsense2/h/rs_option.h"
#include "../../../../include/librealsense2/h/rs_frame.h"

I get exit status 0xc0000135 which Google says means the application failed to initialize correctly.

I've also tried -L${SRCDIR}/rs2 but then it says -lrealsense2 is not found.

答案1

得分: 2

一个 DLL 不仅在构建应用程序时需要,而且在运行应用程序时也需要(这是最重要的)。

为了能够找到 DLL,它必须位于可执行文件所在的文件夹、当前工作目录或者在 PATH 环境变量中。这是 Windows 的工作原理。有关更多详细信息,请参考 DLL 搜索顺序

英文:

A DLL is needed not only when building the application but also (and most importantly) when running it.

The DLL must be in the same folder as the executable, the current working directory, or on the PATH in order to be found. This is how Windows works. For more details refer to DLL search order.

huangapple
  • 本文由 发表于 2021年12月27日 15:04:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/70492538.html
匿名

发表评论

匿名网友

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

确定