基于MingGW64 GCC编译Windows平台上的 libuvc

devtools/2024/10/11 11:17:45/

安装cmake

打开cmake官网 https://cmake.org/download/,下载安装包:

安装时选择将cmake加到系统环境变量里。安装完成后在新的CMD命令窗口执行cmake --version可看到输出:

D:\>cmake --version
cmake version 3.29.3
CMake suite maintained and supported by Kitware (kitware.com/cmake).

安装Mingw64 GCC

Mingw目标是为支持Windows平台上的GCC编译,它主要提供头文件和支持库,Mingw自身不包括GCC和binutils,所以官网提供了集成这些组件的各种安装包。列表里有Linux平台的安装包,那些是用来在Linux平台生成Widows程序的。

https://www.mingw-w64.org/

这里我选择了w64devkit,点击后安装链接指向了github:https://github.com/skeeto/w64devkit/releases ,下载w64devkit-1.23.0.zip。

解压后放在C盘,将目录 C:\w64devkit\bin\ 加入系统PATH环境变量。

然后新打开的CMD命令窗口输入gcc –v 可以看到输出:

D:\>gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=C:/w64devkit/bin/../libexec/gcc/x86_64-w64-mingw32/14.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: /gcc-14.1.0/configure --prefix=/w64devkit --with-sysroot=/w64devkit/x86_64-w64-mingw32 --with-native-system-header-dir=/include --target=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --enable-static --disable-shared --with-pic --with-gmp-include=/deps/include --with-gmp-lib=/deps/lib --with-mpc-include=/deps/include --with-mpc-lib=/deps/lib --with-mpfr-include=/deps/include --with-mpfr-lib=/deps/lib --enable-languages=c,c++ --enable-libgomp --enable-threads=posix --enable-version-specific-runtime-libs --disable-dependency-tracking --disable-lto --disable-multilib --disable-nls --disable-win32-registry --enable-mingw-wildcard CFLAGS_FOR_TARGET=-Os CXXFLAGS_FOR_TARGET=-Os LDFLAGS_FOR_TARGET=-s CFLAGS=-Os CXXFLAGS=-Os LDFLAGS=-s
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 14.1.0 (GCC)

下载usb>libusb

Libuvc依赖usb>libusbusb>libusb是一个低级的通用访问USB设备的用户空间库。我们直接下载官网已经编译好的二进制库。

https://usb>libusb.info/

点击图中Latest Windows Binaries下载 https://github.com/usb>libusb/usb>libusb/releases/download/v1.0.27/usb>libusb-1.0.27.7z 解压后将 include\usb>libusb.h, MinGW64下面的 .a 及 .dll 库提取到一个目录,如D:\usb>libusb:

下载编译uvc>libuvc

https://github.com/uvc>libuvc/uvc>libuvc

从官网以git clone下载源码,源码所放位置为D:\uvc>libuvc

由于cmake最小版本过低会产生如下告警:

CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):Compatibility with CMake < 3.5 will be removed from a future version ofCMake.Update the VERSION argument <min> value or use a ...<max> suffix to tellCMake that the project does not need compatibility with older versions.

所以我们把uvc>libuvc/CmakeList.txt开头的cmake最低版本改为3.10

如果直接执行cmake .. 会提示找不到usb>libusb,打开D:\uvc>libuvc\CmakeLists.txt,删除find_package(LibUSB)一行,替换为下面内容(目录分隔符这里用的是 /, 如果用 \ 分隔,转义的原因还要改为 \\):

# Locate the usb>libusb-1.0 package manually if not found
#find_path(LIBUSB_INCLUDE_DIR NAMES usb>libusb.h PATH_SUFFIXES usb>libusb-1.0)
#find_library(LIBUSB_LIBRARY NAMES usb-1.0 usb>libusb-1.0)
set(LIBUSB_INCLUDE_DIR D:/usb>libusb)
set(LIBUSB_LIBRARY   D:/usb>libusb/usb>libusb-1.0.dll  D:/usb>libusb/usb>libusb-1.0.dll.a)
#set(LIBUSB_LIBRARY  D:/usb>libusb/usb>libusb-1.0.a)   # for static linkif(LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARY)set(LIBUSB_FOUND TRUE)
else()set(LIBUSB_FOUND FALSE)message(WARNING "LibUSB not found. Please install it.")
endif()

静态链接用lbusb-1.0.a就注释掉46行,打开47行, 这样会更方便一点,不会产生找不到dll的问题。

然后在130行左右添加usb>libusb.h头文件目录和链接库,

打开CMD窗口,执行下面命令:

cd D:\uvc>libuvc
mkdir build install
cd build
rm –fr *;  #如果cmake有错,重新执行时建议把目录清空。
cmake -G "MinGW Makefiles" -DCMAKE_INSTALL_PREFIX=D:\uvc>libuvc\install -DBUILD_SHARED_LIBS=ON  ..

第一次执行cmake报错, 系统会报病毒拦截,再执行一次上面的cmake  命令即可。 不确定是误报还是真有病毒。

CMake Error at C:/Program Files/CMake/share/cmake-3.29/Modules/CMakeDetermineCompilerId.cmake:922 (file):file STRINGS file "D:/uvc>libuvc/build/CMakeFiles/3.29.3/CompilerIdC/a.exe"cannot be read.

执行cmake成功后生成Makefie,接下来执行make命令进行编译。编译过程中会提示ARRAYSIZE重复定义的警告:

D:/uvc>libuvc/include/uvc>libuvc/uvc>libuvc_internal.h:75:9: warning: "ARRAYSIZE" redefined75 | #define ARRAYSIZE(arr) (sizeof(arr) / (IS_ARRAY(arr) ? sizeof(arr[0]) : 0))|         ^~~~~~~~~
In file included from C:/w64devkit/x86_64-w64-mingw32/include/minwindef.h:163,from C:/w64devkit/x86_64-w64-mingw32/include/windef.h:9,from C:/w64devkit/x86_64-w64-mingw32/include/windows.h:69,from D:/usb>libusb/usb>libusb.h:64,from D:/uvc>libuvc/include/uvc>libuvc/uvc>libuvc_internal.h:14:
C:/w64devkit/x86_64-w64-mingw32/include/winnt.h:681:
note: this is the location of the previous definition681 | #define ARRAYSIZE(A) RTL_NUMBER_OF_V2(A)

如果不想看到太多输出警告信息,可以编辑D:\uvc>libuvc\include\uvc>libuvc\uvc>libuvc_internal.h, 在ARRAYSIZE外面加上#ifndef ARRAYSIZE 进行防御。

编译成功:

D:\uvc>libuvc\build>make
[  5%] Building C object CMakeFiles/uvc.dir/src/ctrl.c.obj
[ 10%] Building C object CMakeFiles/uvc.dir/src/ctrl-gen.c.obj
[ 15%] Building C object CMakeFiles/uvc.dir/src/device.c.obj
[ 20%] Building C object CMakeFiles/uvc.dir/src/diag.c.obj
[ 25%] Building C object CMakeFiles/uvc.dir/src/frame.c.obj
[ 30%] Building C object CMakeFiles/uvc.dir/src/init.c.obj
[ 35%] Building C object CMakeFiles/uvc.dir/src/stream.c.obj
[ 40%] Building C object CMakeFiles/uvc.dir/src/misc.c.obj
[ 45%] Linking C shared library uvc>libuvc.dll
[ 45%] Built target uvc
[ 50%] Building C object CMakeFiles/uvc_static.dir/src/ctrl.c.obj
[ 55%] Building C object CMakeFiles/uvc_static.dir/src/ctrl-gen.c.obj
[ 60%] Building C object CMakeFiles/uvc_static.dir/src/device.c.obj
[ 65%] Building C object CMakeFiles/uvc_static.dir/src/diag.c.obj
[ 70%] Building C object CMakeFiles/uvc_static.dir/src/frame.c.obj
[ 75%] Building C object CMakeFiles/uvc_static.dir/src/init.c.obj
[ 80%] Building C object CMakeFiles/uvc_static.dir/src/stream.c.obj
[ 85%] Building C object CMakeFiles/uvc_static.dir/src/misc.c.obj
[ 90%] Linking C static library uvc>libuvc.a
[ 90%] Built target uvc_static
[ 95%] Building C object CMakeFiles/example.dir/src/example.c.obj
[100%] Linking C executable example.exe
[100%] Built target example

然后安装到install目录,

D:\uvc>libuvc\build>make install
[ 45%] Built target uvc
[ 90%] Built target uvc_static
[100%] Built target example
Install the project...
-- Install configuration: "Release"
-- Installing: D:/uvc>libuvc/install/lib/uvc>libuvc.dll.a
-- Installing: D:/uvc>libuvc/install/bin/uvc>libuvc.dll
-- Installing: D:/uvc>libuvc/install/include/uvc>libuvc/uvc>libuvc.h
-- Installing: D:/uvc>libuvc/install/include/uvc>libuvc/uvc>libuvc_config.h
-- Installing: D:/uvc>libuvc/install/lib/uvc>libuvc.a
-- Up-to-date: D:/uvc>libuvc/install/include/uvc>libuvc/uvc>libuvc.h
-- Up-to-date: D:/uvc>libuvc/install/include/uvc>libuvc/uvc>libuvc_config.h
-- Installing: D:/uvc>libuvc/install/lib/cmake/uvc>libuvc/uvc>libuvcTargets.cmake
-- Installing: D:/uvc>libuvc/install/lib/cmake/uvc>libuvc/uvc>libuvcTargets-release.cmake
-- Installing: D:/uvc>libuvc/install/lib/cmake/uvc>libuvc/FindLibUSB.cmake
-- Installing: D:/uvc>libuvc/install/lib/cmake/uvc>libuvc/FindJpegPkg.cmake
-- Installing: D:/uvc>libuvc/install/lib/cmake/uvc>libuvc/uvc>libuvcConfigVersion.cmake
-- Installing: D:/uvc>libuvc/install/lib/pkgconfig/uvc>libuvc.pc
-- Installing: D:/uvc>libuvc/install/lib/cmake/uvc>libuvc/uvc>libuvcConfig.cmake

测试

以example.exe来进行测试。

首先利用USBDeview (https://usbdeview.me)工具找到电脑上外置UVC摄像头的VID和PID。

然后打开D:\uvc>libuvc\src\example.c, 修改其中的VID和PID:

重新在build目录执行make,然后执行example.exe, 会提示找不到usb>libusb-1.0.dll,可以将D:\usb>libusb-1.0.dll复制一份放在example.exe同一个目录。 或者一劳永逸的复制到C:\Windows\System32下。 如果给别人发编译好的程序,需要带上usb>libusb-1.0.dll。若前面Cmake配置静态链接usb>libusb则没有此麻烦。

解决完dll问题后,example跑起来会报错,找不到设备:

D:\uvc>libuvc\build>example.exe
UVC initialized
Device found
uvc_open: Not found (-5)
UVC exited

先用USBDeview查看驱动,此UVC设备使用了usbvideo驱动,需要安装WinUSB驱动。

https://learn.microsoft.com/zh-cn/windows-hardware/drivers/usbcon/winusb-installation 面向开发人员的 WinUSB (Winusb.sys) 安装 文档中描述到:

对于某些通用串行总线 (USB) 设备(例如仅由单个应用程序访问的设备),可以在设备的内核模式堆栈中安装 WinUSB (Winusb.sys) 作为 USB 设备的功能驱动程序,而不是实现驱动程序usb>libusb正是此种场景。可以认为WinUSB.sys驱动是一种低层次的驱动,usb>libusb要跟低层的驱动打交道,usbvideo.sys驱动是高层次的驱动。可以31按链接中微软官方的指导安装WinUSB驱动,也可以用Zadig来安装此驱动。

从 https://zadig.akeo.ie下载Zadig。由于在此电脑上,USB Camera(Interface 0)是Video类型,USB Camera(Interface 2)是Audio类型, 所以在从Options菜单中选择List All Devices后选择USB Camera(Interface 0),换用WinUSB驱动。

安装WinUSB驱动后用USBDeview查看:

example跑成功的日志:

D:\uvc>libuvc\build>example.exeUVC initialized
Device found
Device openedDEVICE CONFIGURATION (b349:b182/[none]) ---
Status: idle
VideoControl:bcdUVC: 0x0100VideoStreaming(1):bEndpointAddress: 129Formats:UncompressedFormat(1)bits per pixel: 16GUID: 5955593200001000800000aa00389b71 (YUY2)default frame: 1aspect ratio: 0x0interlace flags: 00copy protect: 00FrameDescriptor(1)capabilities: 01size: 1600x1200bit rate: 153600000-153600000max frame size: 3840000default interval: 1/5interval[0]: 1/5FrameDescriptor(2)capabilities: 01size: 1280x720bit rate: 110592000-110592000max frame size: 1843200default interval: 1/7interval[0]: 1/7FrameDescriptor(3)capabilities: 01size: 640x480bit rate: 147456000-147456000max frame size: 614400default interval: 1/30interval[0]: 1/30MJPEGFormat(2)bits per pixel: 0GUID: 4d4a5047000000000000000000000000 (MJPG)default frame: 1aspect ratio: 0x0interlace flags: 00copy protect: 00FrameDescriptor(1)capabilities: 01size: 1600x1200bit rate: 921600000-921600000max frame size: 3840000default interval: 1/30interval[0]: 1/30FrameDescriptor(2)capabilities: 01size: 1280x720bit rate: 442368000-442368000max frame size: 1843200default interval: 1/30interval[0]: 1/30FrameDescriptor(3)capabilities: 01size: 640x480bit rate: 147456000-147456000max frame size: 614400default interval: 1/30interval[0]: 1/30
END DEVICE CONFIGURATION
First format: (YUY2) 1600x1200 5fps
bmHint: 0001
bFormatIndex: 1
bFrameIndex: 1
dwFrameInterval: 2000000
wKeyFrameRate: 0
wPFrameRate: 0
wCompQuality: 61
wCompWindowSize: 0
wDelay: 0
dwMaxVideoFrameSize: 3840000
dwMaxPayloadTransferSize: 3060
bInterfaceNumber: 1
Streaming...
Enabling auto exposure ...... full AE not supported, trying aperture priority mode... enabled aperture priority auto exposure mode
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
Done streaming.
Device closed
UVC exited

恢复驱动

安装过WinUSB驱动的UVC设备,不会出现在设备管理器的照像机下面。 如果要恢复正常的UVC Camera功能,可以在设备管理器中卸载此设备的驱动。

  

卸载驱动后在设备管理器中右击鼠标,扫描检测硬件改动,USB Camera可以自动识别出来。驱动也恢复为原来的usbvideo.sys。

 


http://www.ppmy.cn/devtools/45739.html

相关文章

AI播客下载:a16z (主题为AI、web3、生物技术等风险投资)

a16z播客是一个综合性的科技和创新领域的媒体平台&#xff0c;通过多种节目形式和丰富的内容&#xff0c;为广大听众提供了一个了解最新科技趋势和创新思维的窗口。a16z播客是由安德里森霍罗威茨&#xff08;Andreessen Horowitz&#xff0c;简称a16z&#xff09;推出的一个科技…

算法(十三)回溯算法---N皇后问题

文章目录 算法概念经典例子 - N皇后问题什么是N皇后问题&#xff1f;实现思路 算法概念 回溯算法是类似枚举的深度优先搜索尝试过程&#xff0c;主要是再搜索尝试中寻找问题的解&#xff0c;当发生不满足求解条件时&#xff0c;就会”回溯“返回&#xff08;也就是递归返回&am…

Android Gradle plugin 版本和Gradle 版本

1.当看到这两个版本时&#xff0c;确实有点迷糊。但是他们是独立的&#xff0c;没有太大关联。 就是说在Android studio中看到的两个版本信息&#xff0c;并无太大关联&#xff0c;是相互独立的。Gradle插件版本决定了你的项目是如何构建的&#xff0c;而Gradle版本是执行构建…

Linux共享内存创建和删除

最近项目中使用到了共享内存记录下 创建共享内存: 删除共享内存: 代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/mman.h> #include <sys/stat.h> #include <u…

一个完整的springboot项目,我们还需要做什么

文章目录 一 从0创建Srpingboot项目1.1 启动springboot项目1.2 导入必要的依赖 二、还缺什么2.1 统一异常捕获2.2 统一MVC返回2.3 数据分层2.4 连接数据库模块2.5 放置常量和工具类2.6 基础controller、基础entity、query查询类2.7 为了方便处理异常&#xff0c;一般还可以入参…

python 删除pdf 空白页

环境 python 3.10 PyPDF2 3.0.1 安装 pip install PyPDF2流程 将空白页和内容页读取出来&#xff0c;看看内部结构有什么不同以此为依据&#xff0c;遍历整个PDF 文件&#xff0c;标记处有内容的页面&#xff0c;写入到另外一个PDF文件。 python 代码 # 每一个页都是一个…

前端组件业务数据选择功能优雅写法

1. 业务场景 后台管理在实际业务中&#xff0c;经常可见的功能为&#xff1a;在当前的页面中从其他列表中选择数据。 例如&#xff0c;在一个商品活动列表页面中 需要选择配置的商品。 2. 遇到问题 从代码划分的角度来说&#xff0c;每个业务列表代码首先分散开来&#xff0…

【LeetCode算法】第94题:二叉树的中序遍历

目录 一、题目描述 二、初次解答 三、官方解法 四、总结 一、题目描述 二、初次解答 1. 思路&#xff1a;二叉树的中序遍历。访问二叉树的左子树&#xff0c;再访问二叉树的根节点&#xff0c;最后访问二叉树的右叉树。 2. 代码&#xff1a; void order(struct TreeNode* r…