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

Time:12-27

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.

CodePudding user response:

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.

  • Related