Search code examples
c++ffmpegcmakebuild

How set CMake to find a local package FFmpeg?


I am trying write CMakeLists with FFmpeg package, with compile on Windows and Linux. First downloaded from FFmpeg-Builds shared releases

I imagine the structure of the project like this:

<project root>
deps/
  ffmpeg/
    win-x64/
      incluve/
      lib/
      bin/
    linux-x64/
      incluve/
      lib/
      bin/
src/
CMakeLists.txt

How to help CMake find libraries: avcodec, avformat, avutil, etc? Maybe in the folder lib/pkgconfig using PkgConfig it is possible to specify the path. But I dont know how


Solution

  • The following worked well for me on Linux with cmake. You will have to find the equivalents for Windows. I used ffmpeg on Windows, but without cmake (i.e. directly in a Visual Studio project).

    1. Install pkg-config, nasm:
        sudo apt-get install -y pkg-config
    
        sudo apt-get install nasm 
    
    1. Download ffmpeg source code:

      https://ffmpeg.org/download.html

    2. Build ffmpeg and install it:

      tar -xvf <downloaded_filename>
      cd /root/folder/with/ffmpeg/src
      ./configure
      make
      sudo make install 
      
    3. Add the following to your CMakeLists.txt:

      In the beginning:

       find_package(PkgConfig REQUIRED)
       pkg_check_modules(LIBAV REQUIRED IMPORTED_TARGET
           libavdevice
           libavfilter
           libavformat
           libavcodec
           libswresample
           libswscale
           libavutil
       )
      
       set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
      

      In the linker area:

       target_link_libraries(${PROJECT_NAME} PUBLIC PkgConfig::LIBAV)