0

I'm following a youtube video on building a project with CMake and FFmpeg. The author is using a Mac and I'm using Ubuntu 20.04. I've been following along successfully until hitting this problem. cmake --version returns 3.16.3

For some reason it is specifying the libraries too early so nothing is imported:

~/video-app/build# cmake ..
~/video-app/build# make VERBOSE=1
...
/usr/bin/c++ \
   -lavcodec -lavformat \                            # << Too early
   CMakeFiles/video-app.dir/src/main.cpp.o \
   CMakeFiles/video-app.dir/src/load_frame.cpp.o \
   -o video-app \                                    # << Should be here
   lib/glfw/src/libglfw3.a \
   -lGL -lGLU -lpthread /usr/lib/x86_64-linux-gnu/librt.so -lm -ldl

If I manually run the command with the -l (dash ell) reordered, it compiles successfully.

Here is the contents of the two CMakeLists.txt files.

# video-app/CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(video-app C CXX)
set(CMAKE_CXX_STANDARD 14)
add_subdirectory(lib/glfw)
add_subdirectory(lib/FFmpeg)
list(APPEND EXTRA_LIBS "-lGL -lGLU")
list(APPEND SOURCES src/main.cpp src/load_frame.cpp)
add_executable(video-app ${SOURCES})
target_link_libraries(video-app FFmpeg glfw ${EXTRA_LIBS})
# video-app/lib/CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(FFmpeg)
find_package(PkgConfig REQUIRED)
# There are seven libraries in original - only two shown here for brevity.
pkg_check_modules(AVCODEC   REQUIRED IMPORTED_TARGET libavcodec)
pkg_check_modules(AVFORMAT  REQUIRED IMPORTED_TARGET libavformat)
add_library(FFmpeg INTERFACE IMPORTED GLOBAL)
target_include_directories(FFmpeg INTERFACE
    ${AVCODEC_INCLUDE_DIRS}
    ${AVFORMAT_INCLUDE_DIRS})
target_link_options(FFmpeg INTERFACE
    ${AVCODEC_LDFLAGS}
    ${AVFORMAT_LDFLAGS})

I tried:

  • Changing the order of target_link_libraries in top CMake file. Funny thing is, it worked once.
  • I tried adding target_link_libraries to FFmpeg CMake file.
  • Searching for stackover.com for answers and reading CMake documentation.
Jamie Royer
  • 229
  • 1
  • 4
  • Duplicate: Finally found the answer here: https://stackoverflow.com/questions/63139166/linker-errors-with-avformat-with-cmake-undefined-reference – Jamie Royer Jan 18 '21 at 01:42
  • To summarize: If you use `pkg_check_modules` with `IMPORTED_TARGET` keyword, then all you need is to link with this imported target (`PkgConfig::libavcodec` and `PkgConfig::libavformat` in your case). If you don't want to use an imported target, then you need to use `target_link_libraries` with `_LIBRARIES` variable and `target_link_options` with `_LDFLAGS_OTHER` variable, like in [that answer](https://stackoverflow.com/a/48013659/3440745). – Tsyvarev Jan 18 '21 at 09:22

0 Answers0