JPGygax68
1/14/2016 - 10:27 PM

CMake: how to add the FFmpeg libraries to a target

CMake: how to add the FFmpeg libraries to a target

# Detect architecture 

if (CMAKE_SIZEOF_VOID_P MATCHES 8)
	set( PROJECT_ARCH "x86_64" )
else(CMAKE_SIZEOF_VOID_P MATCHES 8)
	set( PROJECT_ARCH "x86" )
endif(CMAKE_SIZEOF_VOID_P MATCHES 8)

# FFmpeg

set(FFmpeg_ROOT "" CACHE PATH "Root of FFmpeg library (libAV)")

foreach (lib "avformat" "avcodec" "avutil" "swscale")

	# Library files
	find_library(${lib}_LIB_RELEASE "${lib}" PATHS ${FFmpeg_ROOT}/lib/${PROJECT_ARCH} ${FFmpeg_ROOT}/lib)
	if (NOT ${lib}_LIB_RELEASE)
		message(FATAL_ERROR "Could not find release version of library \"${lib}\"")
	endif()
	find_library(${lib}_LIB_DEBUG "${lib}" PATH ${FFmpeg_ROOT}/lib/${PROJECT_ARCH} ${FFmpeg_ROOT}/lib)
	if (NOT ${lib}_LIB_DEBUG)
		message(FATAL_ERROR "Could not find debug version of library \"${lib}\"")
	endif()
	target_link_libraries(${PROJECT_NAME} PUBLIC 
		$<$<CONFIG:Release>:${${lib}_LIB_RELEASE}>
		$<$<CONFIG:Debug>:${${lib}_LIB_DEBUG}>
	)

	# Header file
	find_path(${lib}_INCLUDE "lib${lib}/${lib}.h" PATHS ${FFmpeg_ROOT}/include)
	if (NOT ${lib}_INCLUDE)
		message(FATAL_ERROR "Could not find header file of library \"${lib}\"")
	endif()
	target_include_directories(${PROJECT_NAME} PRIVATE ${${lib}_INCLUDE})
	
endforeach()