CMake configuration for ffmpeg in C++ project
Ok, I've found the solution. It appears that FFmpeg doesn't support find_package in CMake. I had to manually link the libraries as suggested here.
Final CMakeLists.txt looks like this
cmake_minimum_required(VERSION 2.6)
project(decode_encode)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "-D__STDC_CONSTANT_MACROS")
find_path(AVCODEC_INCLUDE_DIR libavcodec/avcodec.h)
find_library(AVCODEC_LIBRARY avcodec)
find_path(AVFORMAT_INCLUDE_DIR libavformat/avformat.h)
find_library(AVFORMAT_LIBRARY avformat)
find_path(AVUTIL_INCLUDE_DIR libavutil/avutil.h)
find_library(AVUTIL_LIBRARY avutil)
find_path(AVDEVICE_INCLUDE_DIR libavdevice/avdevice.h)
find_library(AVDEVICE_LIBRARY avdevice)
add_executable(decode_encode main.cpp)
target_include_directories(decode_encode PRIVATE ${AVCODEC_INCLUDE_DIR} ${AVFORMAT_INCLUDE_DIR} ${AVUTIL_INCLUDE_DIR} ${AVDEVICE_INCLUDE_DIR})
target_link_libraries(decode_encode PRIVATE ${AVCODEC_LIBRARY} ${AVFORMAT_LIBRARY} ${AVUTIL_LIBRARY} ${AVDEVICE_LIBRARY})
I am sure there is a better way to aggregate all the libraries, though.
PkgConfig can be used to link the libraries more conveniently, as mentioned in a comment. With CMake 3.17, this links all libav libraries:
cmake_minimum_required(VERSION 3.17)
project(Foo)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBAV REQUIRED IMPORTED_TARGET
libavdevice
libavfilter
libavformat
libavcodec
libswresample
libswscale
libavutil
)
add_executable(${PROJECT_NAME}
main.cpp
)
target_link_libraries(${PROJECT_NAME}
PkgConfig::LIBAV
)
You need to tell CMAKE
where to find headers and libraries for ffmpeg
in your system.
You can use the find_package(ffmpeg
to look into your system for you and then use the CMAKE variables it defines to set up the headers for the compiler and the libraries for the linker correctly.
- header:
include_directories(${FFMPEG_INCLUDE_DIRS})
- libraries:
target_link_libraries(decode_encode ${FFMPEG_LIBRARIES})
Something like the following should serve the purpouse.
cmake_minimum_required(VERSION 2.6)
project(decode_encode)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "-D__STDC_CONSTANT_MACROS")
find_package(FFmpeg COMPONENTS AVCODEC AVFORMAT AVUTIL AVDEVICE REQUIRED) #add here the list of ffmpeg components required
if(FFMPEG_FOUND)
# FFMPEG_INCLUDE_DIRS - Include directory necessary for using the required components headers.
# FFMPEG_LIBRARIES - Link these to use the required ffmpeg components.
# FFMPEG_DEFINITIONS - Compiler switches required for using the required ffmpeg components.
message("FFMPEG_INCLUDE_DIRS = ${FFMPEG_INCLUDE_DIRS} ")
message("FFMPEG_LIBRARIES = ${FFMPEG_LIBRARIES} ")
message("FFMPEG_DEFINITIONS = ${FFMPEG_DEFINITIONS} ")
include_directories(${FFMPEG_INCLUDE_DIRS})
endif()
add_executable(decode_encode main.cpp)
target_link_libraries(decode_encode ${FFMPEG_LIBRARIES})
NOTE I have not tried this, so you might need to tweak it.