windows 下使用 MinGW+msys 编译 ffmpeg
一 编译准备
您最好新建一个目录专门用来保存以下下载的文件。
1)下载 MinGW 和 MSYS
MinGW(Minimalist GNU on Windows):一个可自由使用和自由发布的 Windows 特定头文件和
使用 GNU 工具集导入库的集合,允许你生成本地的 Windows 程序而不需要第三方 C 运行时
库。
到:http://sourceforge.net/projects/mingw/ 去下载即可
A 点击“DownLoad mingw-get-inst-...exe”
B 切 换 到 页 面 :download? Check your browser's security bar, or try a direct link, or try
another mirror.
C 点击“direct link”下载类似:mingw-get-inst-20111118.exe 的文件
提示:msys 此处就不用再下载了,mingw-get-inst-20111118.exe 中已经包含了 msys1.0,
后面安装的时候就可以看到该选项。
2)下载 yasm(汇编编译器)
ffmpeg 编译的时候里面的汇编代码需要 yasm.exe
去官网:http://yasm.tortall.net/Download.html 下载
在 Latest Release 栏,直接下载:Win32 .exe (for general use on 32-bit Windows)
文件名类似为:yasm-1.2.0-win32.exe
3)下载 ffmpeg
ffmpeg 可以到 http://www.ffmpeg.org/releases/ 下载
选择的一个最新版本下载即可,如:ffmpeg-2.8.1.tar.gz
文件名类似为:ffmpeg-2.8.1.tar.gz
当然你也使用网上介绍的使用 SVN 直接下载源代码的方法。
svn://svn.mplayerhq.hu/ffmpeg/trunk
二 编译步骤
1) 安装 mingw 和 msys
执行下载文件:mingw-get-setup.exe
在第 3 步选择“Download latest repository catalogues”
选择安装路径:C:\MinGW(我直接采用默认路径,未做修改)
在“Select Components”步骤中选择:
√ C Compiler (默认且一定选择)
√ C++ Compiler
√ MSYS Basic System(选择此项将会安装 msys,msys 则不需要另外安装)
√ MinGW Developer ToolKit
等待安装完成!(需要等待一定的时间,因为安装过程需要下载文件,可想而知,网
速越快,安装越快)
安装完成后,在 C:\MinGW 目录下可以看到 msys 文件夹,msys 也已经安装。
2) 配置 msys.bat 批处理文件
为了方便 VS200X 以及 VS2010 调用 ffmpeg 的动态库,可以通过配置让 ffmpeg 编译时产
生 windows 下调用 dll 对应的 lib,当然,如果你没有这个需要,那么就可以省略以下处
理,直接进入步骤 3。
进入:C:\MinGW\msys\1.0\文件夹,使用 UltraEdit 打开 msys.bat 文件,在文件的最最
前面加入如下一行:
call "D:\Program Files\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"
添加后效果如下:
call "D:\Program Files\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"
@echo off
rem Copyright (C): 2001, 2002, 2003, 2004, 2005 Earnie Boyd
rem mailto:earnie@users.sf.net
rem This file is part of Minimal SYStem
rem http://www.mingw.org/msys.shtml
rem
rem File: msys.bat
rem Revision:
2.4
rem Revision Date: December 8th, 2005
rem ember to set the "Start in:" field of the shortcut.
rem A value similar to C:\msys\1.0\bin is what the "Start in:" field needs
rem to represent.
其中“D:\Program Files\Microsoft Visual Studio 10.0”为你机器上安装 VS200X 或 VS2010
的目录,我的电脑安装的是 VS2010,并且安装在了 D 盘。文件修改后保存即可。
3) 系统整合
使用 UltraEdit 修改 C:\MinGW\msys\1.0\etc 目下的 fstab 文件,如果不存在 fstab,请复制
一份 fstab.sample,然后改名为:fstab,再修改其内容为:C:/MinGW /mingw,也即为
MinGW 的安装路径。
4) 配置 yasm
为了减少配置环境变量的麻烦,本处直接把 yasm-1.2.0-win32.exe 文件名修改成:
yasm.exe,并放到 C:\mingw32\mingw32\bin 目录下
5) 编译 ffmpeg
ffmpeg-2.8.1.tar.gz 解压到:D:\ffmpeg\ 目录下,双击“C:\MinGW\msys\1.0\msys.bat”,启
动“MinGW32”,切换到 ffmpeg 目录:D:\ffmpeg\ffmpeg-0.8.6,切换方法如下:
$ cd /d
$ cd ffmpeg
$ cd ffmpeg-2.8.1
如果你的“MinGW32”没有关闭,可以直接切换路径到:D:\ffmpeg\ ffmpeg-2.8.1。
5.1)再执行以下命令(执行该命令前最好执行 make clean,清除以前配置的环境,下载
解压出来的不要执行该步骤):
$
./configure
--enable-cross-compile
--enable-shared --disable-static
--enable-postproc
--enable-pthreads
--extra-cflags="-I../deps/include" --extra-ldflags="-L../deps/lib" --prefix=../export-lib/win32
--enable-runtime-cpudetect
--target-os=mingw32
--arch=x86_32
--enable-memalign-hack --enable-gpl
--enable-avresample
$ ./configure --enable-cross-compile --arch=x86_32 --target-os=mingw32 --enable-postproc
--enable-shared --disable-static --enable-gpl --enable-pthreads --enable-runtime-cpudetect
--enable-avresample
--extra-ldflags="-L../deps/lib"
--prefix=../export-lib/win32
--extra-cflags="-I../deps/include"
5.2)$ make
需要等待很长一段时间,等待编译结束。
编译可能会出现:
Including after is unsupported 错误提示。
解决办法:
在\MinGW\include 目录下的 ptw32_errno.h 文件中:
把:
#if !defined(ETIMEDOUT)
/* Not normally defined in Win32 , but at one time it may
* have been defined in ; this seems to be no longer the
* case, but WSAETIMEDOUT may offer a suitable substitute.
*
* FIXME: this may be incompatible with any application which wants
* to use
*/
# include
改成:
#if !defined(ETIMEDOUT)
/* Not normally defined in Win32 , but at one time it may
* have been defined in ; this seems to be no longer the
* case, but WSAETIMEDOUT may offer a suitable substitute.
*
* FIXME: this may be incompatible with any application which wants
* to use
*/
//# include
# include 即可。
解决办法:
在 \ffmpeg\libavformat 目 录 下 找 到 os_support.c 文 件 , 在 该 文 件 函 数 const char
*ff_gai_strerror(int ecode)中:
把 case EAI_MEMORY:
return "Memory allocation failure";
改成:
case 6://EAI_MEMORY:
return "Memory allocation failure";
即可编译通过。
为了汇总编译结果,请继续执行以下命令:
5.3)$ make install
等待完成,在 C:\MinGW\msys\1.0\local(未指定输出目录)中将会生成 bin、include、lib
等文件夹,或者指定输出目录../export-lib/win32 中将会生成 bin、include、lib 等文件.其
中包含了 ffmpeg.exe、ffplay.exe、ffprobe.exe、dll、lib 和头文件等。
VC6 下:
1) 未定义__STDC_CONSTANT_MACROS 问题
fatal error C1189: #error : missing -D__STDC_CONSTANT_MACROS / #define
__STDC_CONSTANT_MACROS
fatal error C1189: #error : missing -D__STDC_CONSTANT_MACROS / #define
__STDC_CONSTANT_MACROS
今天使用 FFMPEG 出现以上错误
解决方法:
在 libavutil\common.h 文件头部加上以下宏定义即可:
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS
#ifdef _STDINT_H
#undef _STDINT_H
#endif
# include "stdint.h"
#endif
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif
2)
inttypes.h 头文件问题
C99 标准定义了 stdint.h、inttypes.h,用于统一的跨平台数据定义。可惜 VC、BCB 等编译
器对 C99 的兼容性较差,有些版本没有该头文件。本文对此进行探讨,编写了一套兼容 VC、
BCB、GCC 的 stdint.h、inttypes.h。
先检查一下各个编译器对 stdint.h、inttypes.h 的支持情况——
VC6:stdint.h、inttypes.h 都没有。
VC2003:stdint.h、inttypes.h 都没有。
VC2005:stdint.h、inttypes.h 都没有。
VC2010:有 stdint.h,没有 inttypes.h。
BCB6:有 stdint.h,没有 inttypes.h。
GCC 4.7.0(Fedora 17):stdint.h、inttypes 均有。
GCC 4.6.2(MinGW (20120426)):stdint.h、inttypes 均有。
因此从 GCC 下找到该头文件(在 mingW 安装目录下),例如:
C:\MinGW\lib\gcc\mingw32\4.9.3\include\c++。
3) attribute_deprecated 属性错误
阅读 ffmpeg 源码是 发现一些函数前面加了 attribute_deprecated 属性;如:
attribute_deprecated int url_fopen( AVIOContext **s, const char *url, int flags);
在 libavutil/attributes.h 中有如下定义:
define attribute_deprecated __attribute__((deprecated))
83 #ifndef attribute_deprecated
84 #if AV_GCC_VERSION_AT_LEAST(3,1)
85 #
86 #else
87 #
88 #endif
89 #endif
define attribute_deprecated
__attribute__ 语法为 GNU C 的特性,__attribute__可以设置函数属性(Function
Attribute)、变量属性(Variable Attribute)和类型属性(Type Attribute)。
__attribute__语法格式为:__attribute__ ((attribute))
需要注意的是: 使用__attribute__的时候,只能函数的声明处使用__attribute__,并且
在“;“前。