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

news/2024/10/11 11:24:04/

安装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/news/1465825.html

相关文章

Python魔法之旅-魔法方法(10)

目录 一、概述 1、定义 2、作用 二、应用场景 1、构造和析构 2、操作符重载 3、字符串和表示 4、容器管理 5、可调用对象 6、上下文管理 7、属性访问和描述符 8、迭代器和生成器 9、数值类型 10、复制和序列化 11、自定义元类行为 12、自定义类行为 13、类型检…

非线性系统:相平面法

非线性系统&#xff1a;相平面法 非线性系统的相平面法是一种重要的分析工具&#xff0c;用于研究系统的动力学行为。通过相平面法&#xff0c;可以直观地观察系统状态变量的变化&#xff0c;分析系统的稳定性、周期性和其他动力学特性。本文将详细介绍相平面法的基本思想、步…

查看和修改自己的git提交时的作者信息

首先查看自己本地配置的信息:git config --list 修改姓名和邮箱: 配置用户名 git config --global user.name "真实姓名" 配置邮箱 git config --global user.email "公司分配的企业邮箱" 示例 git config --global user.name "张三" git…

Yocto - bitbake任务中clean和cleanall的区别

在 BitBake 中&#xff0c;clean 和 cleanall 命令都用于删除构建工件&#xff0c;但它们的范围和执行的清理程度不同。 1. clean 命令&#xff1a; 目的&#xff1a;clean命令用于删除与特定任务或配方相关的临时构建文件和工件。 范围&#xff1a;它只清除指定任务或配方生…

探索AI去衣技术中的反射应用

在当今数字时代&#xff0c;人工智能&#xff08;AI&#xff09;技术的飞速发展已经渗透到了我们生活的方方面面。其中&#xff0c;图像处理和计算机视觉作为AI的重要分支&#xff0c;正不断推动着创新应用的边界。今天&#xff0c;我们要探讨的是一个颇具争议但又技术上颇为有…

使用 Navicat 工具查看 SQLite 数据库中的 PNG 图片

Navicat 是一款功能强大的数据库管理工具&#xff0c;支持多种数据库类型&#xff0c;包括 SQLite。它提供了一个直观的用户界面&#xff0c;可以轻松查看、编辑和管理数据库数据。 SQLite 是一种轻量级的嵌入式数据库&#xff0c;常用于移动应用程序和小型项目。它支持存储各…

算法每日一题(python,2024.05.31)

题目来源&#xff08;力扣. - 力扣&#xff08;LeetCode&#xff09;&#xff0c;简单&#xff09; 解题思路&#xff1a; 二次遍历&#xff0c;第一次遍历用哈希表记录每个字母的出现次数&#xff0c;出现一次则将它的value值赋为True&#xff0c;将它的下标赋为key值&#x…

[原创]C++ 11的thread_local线程局部变量与Lambda表达式配合使用, 却引发致命的, 难以发现的冲突.

[简介] 常用网名: 猪头三 出生日期: 1981.XX.XX QQ联系: 643439947 个人网站: 80x86汇编小站 https://www.x86asm.org 编程生涯: 2001年~至今[共22年] 职业生涯: 20年 开发语言: C/C、80x86ASM、PHP、Perl、Objective-C、Object Pascal、C#、Python 开发工具: Visual Studio、D…