经过对比了visual c++ 与 mingw64,最后决定还是采用mingw64的方案来静态编译
使用mingw64的优势在于,生成的最终文件不需要依赖了,可以直接就用,同时,也方便跨平台的开发与支持.
编译过程,分为以下几步,安装编译工具mingw64,安装visual studio 社区版.
安装visual studio社区版:
在微软官方网址:https://visualstudio.microsoft.com/zh-hans/vs/community/ 下载对应的社区版本,在安装时,只勾选
"使用C++的桌面开发",就够用了.一直下一步,直到安装完成.
安装mingw64
安装包下载网址: https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/
32位版本的就下载: https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/8.1.0/threads-posix/sjlj/i686-8.1.0-release-posix-sjlj-rt_v6-rev0.7z
64位版本下载地址: https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-posix/sjlj/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0.7z
下载完成后,解压到c:mingw32或者c:mingw64即可.添加环境变量等操作,按相应的操作执行即可.
安装完成后,验证是否安装成功,在命令行下输入
i686-w64-mingw32-gcc -v
输出如下显示就没问题了.
Thread model: posix
gcc version 8.1.0 (i686-posix-sjlj-rev0, Built by MinGW-W64 project)
接下来就是安装vcpkg
vcpkg的官方网站在 https://github.com/microsoft/vcpkg
最好的方式是使用git 来拉取相应的代码
git clone https://github.com/microsoft/vcpkg.git
当然也可以直接下载压缩包:
https://github.com/microsoft/vcpkg/archive/refs/tags/2024.04.26.zip
二种方法都可以,看自己喜好.
下载完成后,解压进入vcpkg目录,执行
bootstrap-vcpkg.bat
完成vcpkg.exe等的下载.
接下来是安装编译ffmpeg的库,命令如下
vcpkg install ffmpeg[x264,x265,ffmpeg,ffplay,ffprobe,sdl2,openssl]:x86-mingw-static --recurse
以上命令是编译基于32位的静态库,x86代表32位,mingw表示是用mingw32/mingw64的编译器,static表示静态库.
最好是加上--recurse,缺少什么,会自己去下载安装.
如果一切顺利,就只需要静静的等结果.如果出现什么错误,再针对性的修改.再编译.
同样,如果需要编译64位的静态库的话,可以使用下述命令
vcpkg install ffmpeg[x264,x265,ffmpeg,ffplay,ffprobe,sdl2,openssl]:x64-mingw-static --recurse
如果需要使用vc的编译器来编译的话,可以使用下述命令
vcpkg install ffmpeg[x264,x265,ffmpeg,ffplay,ffprobe,sdl2,openssl]:x86-static --recurse
vcpkg install ffmpeg[x264,x265,ffmpeg,ffplay,ffprobe,sdl2,openssl]:x64-static --recurse
这是使用windows开发的方法,使用mingw64可以在Linux下交叉编译什么的.
经过一段时间的等待,编译安装结束后,将vcpkginstalledx86-mingw-static目录下面的lib和include目录复制到需要使用的项目中就可以了.
vcpkg查看已安装的包:
C:vcpkg>vcpkg list |findstr x86-mingw-static
bzip2:x86-mingw-static 1.0.8#5 bzip2 is a freely available, patent free, high-q...
bzip2[tool]:x86-mingw-static Builds bzip2 executable
curl:x86-mingw-static 8.7.1#2 A library for transferring data with URLs
curl[non-http]:x86-mingw-static Enables protocols beyond HTTP/HTTPS/HTTP2
curl[schannel]:x86-mingw-static SSL support (Secure Channel)
curl[ssl]:x86-mingw-static Default SSL backend
curl[sspi]:x86-mingw-static SSPI support
flatbuffers:x86-mingw-static 24.3.25 FlatBuffers is a cross platform serialization li...
giflib:x86-mingw-static 5.2.2 A library for reading and writing gif images.
hdf5:x86-mingw-static 1.14.2 HDF5 is a data model, library, and file format f...
hdf5[szip]:x86-mingw-static Build with szip
hdf5[zlib]:x86-mingw-static Build with zlib
leptonica:x86-mingw-static 1.84.1 An open source library containing software that ...
libiconv:x86-mingw-static 1.17#3 GNU Unicode text conversion
libjpeg-turbo:x86-mingw-static 3.0.2 libjpeg-turbo is a JPEG image codec that uses SI...
liblzma:x86-mingw-static 5.4.4 Compression library with an API similar to that ...
libpng:x86-mingw-static 1.6.43#1 libpng is a library implementing an interface fo...
libwebp:x86-mingw-static 1.4.0 WebP codec: library to encode and decode images ...
libwebp[libwebpmux]:x86-mingw-static Build the libwebpmux library
libwebp[nearlossless]:x86-mingw-static Enable near-lossless encoding
libwebp[simd]:x86-mingw-static Enable any SIMD optimization.
libwebp[unicode]:x86-mingw-static Build Unicode executables. (Adds definition UNIC...
libxml2:x86-mingw-static 2.11.7 Libxml2 is the XML C parser and toolkit develope...
libxml2[iconv]:x86-mingw-static Add ICONV support
libxml2[lzma]:x86-mingw-static Use LZMA
libxml2[zlib]:x86-mingw-static Use ZLib
openjpeg:x86-mingw-static 2.5.2 OpenJPEG is an open-source JPEG 2000 codec writt...
sdl2:x86-mingw-static 2.30.1 Simple DirectMedia Layer is a cross-platform dev...
szip:x86-mingw-static 2.1.1#12 Szip compression software, providing lossless co...
tiff:x86-mingw-static 4.6.0#4 A library that supports the manipulation of TIFF...
tiff[jpeg]:x86-mingw-static Support JPEG compression in TIFF image files
tiff[lzma]:x86-mingw-static Support LZMA compression in TIFF image files
tiff[zip]:x86-mingw-static Support ZIP/deflate compression in TIFF image files
x264:x86-mingw-static 0.164.3107 x264 is a free software library and application ...
x264[asm]:x86-mingw-static Enable platform-specific assembly optimizations
x264[default-features]:x86-mingw-static Default set of features
x264[gpl]:x86-mingw-static Allow use of GPL code, the resulting libs and bi...
x265:x86-mingw-static 3.5 x265 is a H.265 / HEVC video encoder application...
zlib:x86-mingw-static 1.3.1 A compression library
vcpkg查看ffmpeg 还支持编译哪些包
vcpkg search ffmpeg
要安装ffmpeg全量包的话,可以直接
vcpkg install ffmpeg[all]:x86-mingw-static
这样就不会有什么遗漏,但一般情况下,我们只编译安装需要用到的部分就可以了.
在Linux下测试验证编译好包的库和头文件是否正确.
将include 和 lib文件解压到当前installed 目录
来一个ffmpeg版本的Hello World
//file hello.cpp
include "bits/stdc++.h"
extern "C"
{
include "libavutil/log.h"
}
using namespace std;
int main(int argc, char *argv[])
{
av_log_set_level(AV_LOG_DEBUG);
av_log(NULL, AV_LOG_DEBUG, "%s", "Hello World !\n");
return 0;
}
编译命令:
i686-w64-mingw32-g++ -static -static-libgcc -static-libstdc++ hello.cpp -o hello.exe -I./installed/include -L./installed/lib -lavutil
查看生成的结果:
file hello.exe
hello.exe: PE32 executable (console) Intel 80386, for MS Windows
将文件复制到windows上运行,看看有没有依赖,确认是否静态编译成功.
ls -lh hello.exe
-rwxrwxrwx 1 hesy hesy 2.6M 5月 23 17:24 hello.exe
在windows下使用depends查看依赖,没有依赖ffmpeg相关的东西.
接下来,使用静态库编译成单个dll文件,就不会有其他的依赖.