23

I'm trying to use FFmpeg in a C++ project in Visual Studio 2010. I want to include the libraries as statically linked files. Simple programs like libavcodec/api-example.c compile without error and no linker error appears in the error view when starting them. However, a message box shows up after starting the application, saying that avutil-51.dll is missing. Do you have any hints on how to fix that?

I used the latest dev build from http://ffmpeg.zeranoe.com/builds/. Then I specified include as additional include directory, avcodec.lib;avfilter.lib;avformat.lib;avutil.lib as additional dependencies and lib as additional library directory.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
box
  • 3,156
  • 3
  • 26
  • 36

1 Answers1

52

With FFmpeg you can either:

  1. use pre-built .lib/.dll files and your binary produced with Visual Studio will be dependent on av*.dll files
  2. compile FFmpeg from source code into static libraries using non-Microsoft compiler, and then link to your Visual Studio project (mind the LGPL/GPL license in this case)

You built your project as per item 1 above. You have to use and redistribute the av*.dll dependent files with your binary to have it working.

"Static" on Zeranoe means that libraries are statically linked into binaries like ffmpeg.exe. Do not confuse this with static .lib libraries that link into your binary. Zeranoe does not provide such.

On Zeranoe you will find archives like this:

  • "Shared" ffmpeg-20120726-git-236ecc3-win32-shared.7z:
  • bin/avcodec-54.dll
  • bin/avutil-51.dll
  • etc
  • "Dev" ffmpeg-20120726-git-236ecc3-win32-dev.7z:
  • lib/avcodec.lib
  • lib/avutil.lib

"Shared" archive has FFmpeg built with dynamic link to DLL libraries. "Dev" archive has lib files which you can use in your project to link to them as well in a way that ffmpeg.exe does in shared archive.

So, your Visual Studio project can be as simple as this (browse full source here):

extern "C" 
{
        // NOTE: Additional directory ..\zeranoe.com\dev\include gets to the files
        #include "libavcodec\avcodec.h"
}

// NOTE: Additional directory ..\zeranoe.com\dev\lib gets to the files
#pragma comment(lib, "avcodec.lib")

// NOTE: Be sure to copy DLL files from ..\zeranoe.com\shared\bin to the directory of 
//       the FFmpegApp.exe binary
int _tmain(int argc, _TCHAR* argv[])
{
        _tprintf(_T("Trying avcodec_register_all... "));
        avcodec_register_all();
        _tprintf(_T("Done.\n"));
        return 0;
}

You will extract "Dev" archive into dev subdirectory of Visual Studio project and you will add dev\include on the additional include path. This will be sufficient to build the binary, and it will be dependent on av*.dll:

enter image description here

This is when you extract the "Shared" archive, and copy DLLs from its bin to the directory of your binary. And your app will work from there:

C:\FFmpegApp\Release>FFmpegApp.exe
Trying avcodec_register_all... Done.

20 Jan 2016 UPDATE: The project in repository is upgraded to Visual Studio 2013 (older VS 2010 code) and checked against current Zeranoe builds. The sample and instructions remain in good standing.

Note that Win32 builds in Visual Studio assume that you use 32-bit files from Zeranoe. To build 64-bit version, download respective files and set up Visual C++ project respectively, to build x64 (or, the best, download both, set up both configurations and configure include/lib paths respectively). Failure to match bitness would result in error, mentioned in the comments below.

20 Jul 2021 UPDATE: (pulled from comments below) Zeranoe builds are no longer available. A good and officially endorsed alternative is Windows builds by BtbN. You will need a (...)-win64-gpl-shared.zip or (...)-win64-lgpl-shared.zip file for this tutorial.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • Thanks for clearing that up! There are some .dll.a files in the project which I also tried to include as additional includes for the linker. However, there is no avutil-51.dll but only a libavutil.dll.a file. Do I have to get additional dlls or where/how do I have to include them? – box Jul 28 '12 at 14:42
  • 1
    I updated answer above (with correction as for "static" builds). You will need Dev archive to link into your projects, and Shared archive wlil get you missing DLLs. – Roman R. Jul 28 '12 at 16:08
  • I did as you described but I couldn't get it to work (error remains). Where do I have to include the DLLs? I tried to put them in various include paths and copied them to the same folder as the executable. Do I have to do anything with the *.dll.a files in "Dev"? – box Jul 28 '12 at 19:38
  • I thought I could be giving you wrong advice... but nope, everything is in good standing. See answer above for even more detail. – Roman R. Jul 30 '12 at 17:09
  • Thanks a lot for helping! I followed your detailed description and now it works. – box Jul 30 '12 at 18:54
  • For MSVC 2012 or earlier these files are needed https://code.google.com/p/msinttypes/ – Javier Barandiaran Martirena May 12 '15 at 13:26
  • 3
    Absolutely do not forget the `extern "C"`!! I missed it in your example and was tearing my hair out as I couldn't see what I was doing differently. – Deanna May 22 '15 at 09:18
  • I get `Error 1 error LNK2019: unresolved external symbol _avcodec_register_all referenced in function _wmain M:\Desktop\testeFFmpeg\FFmpegApp.obj FFmpegApp` when I try to run your example...any reasons why? – Michel Feinstein Jan 19 '16 at 20:34
  • @mFeinstein: something is wrong with `avcodec.lib`, not found or wrong path. – Roman R. Jan 19 '16 at 20:49
  • @RomanR. I downloaded it from Zeranoe's latest dev build. Can your try this please? I am days trying to compile this....I think it's MinGW compilation has names with no underscore and maybe different calling conventions. Also they are only static libs, so I wonder if when you made this Answer, if they weren't shared libs for use with the dlls. – Michel Feinstein Jan 19 '16 at 20:52
  • @mFeinstein: checked with VS2013 and latest Zeranoe FFmpeg - the sample remains in good standing – Roman R. Jan 20 '16 at 10:48
  • @RomanR. are you using which build? dev? and which lib? avcodec.lib? – Michel Feinstein Jan 20 '16 at 10:52
  • Have a look at added comments [there](http://www.alax.info/trac/public/browser/trunk/Utilities/FFmpeg/FFmpegApp/FFmpegApp.cpp) to see what is taken from "dev" and what is taken from "shared". – Roman R. Jan 20 '16 at 10:54
  • @RomanR. I still can't get it to compile. Do you mind running [this example](https://drive.google.com/file/d/0BzcaluqXbeOraVlwVV9rejNjMzQ/view?usp=sharing)? I added the libraries and everything inside. – Michel Feinstein Jan 20 '16 at 11:35
  • @mFeinstein: You took 64-bit build of FFmpeg, so you need to build Debug/x64 in Visual Studio as well. This builds fine. Or, build Win32 and use 32-bit files from Zeranoe. – Roman R. Jan 20 '16 at 11:53
  • @RomanR. ooohhh hahaha I feel so silly, I am wasting days on this! I can't thank you enough!!! – Michel Feinstein Jan 20 '16 at 12:12
  • @RomanR. do you think I there is any real performance advantages using a x64 lib instead of a x86 one? – Michel Feinstein Jan 20 '16 at 12:13
  • 64-bit builds are, generally, not about performance. Performance might slightly differ through too. The typical problem is that you need bitness [for your DLL] to match bitness of the [EXE] application you are using it with. Second typical problem is that with 64-bit builds you don't have virtual address space constraint, and it might be important since a lot of apps nowadays do hit it. – Roman R. Jan 20 '16 at 12:29
  • I thought 64 bits will help in performance since any 64 bit arithmetic will be done in one instruction. – Michel Feinstein Jan 20 '16 at 13:35
  • So the only reason someone will choose a 64 bit build will be to have more virtual address space? – Michel Feinstein Jan 20 '16 at 13:36
  • 1
    Zeranoe builds are no longer available. As of 2021-07-21, a good and officially endorsed alternative is [Windows builds by BtbN](https://github.com/BtbN/FFmpeg-Builds/releases). You will need a `(...)-win64-gpl-shared.zip` or `(...)-win64-lgpl-shared.zip` file for this tutorial. – Krzysztof Bociurko Jul 21 '21 at 10:35
  • Hi, I browsed Windows builds by BtbN, and did not find a dev archive as described here. Nevertheless, I linked the lib in the ./lib directory and copied the .dlls into my project. However, several link error occurred showing unresolved external symbols found in the lib files I linked. Example: ```avformat.lib(tcp.o) : error LNK2001: unresolved external symbol __imp_closesocket``` – Guanyuming He Sep 11 '22 at 04:27
  • Never mind, I have set another unnecessary linker option that caused the problem. – Guanyuming He Sep 11 '22 at 13:44