-1

I'm trying to link ffmpeg on ubuntu, i downloaded ffmpeg source and added to my lib/ffmpeg library then ran ./configure, i created a cmake on my project directory and one inside ffmpeg dir. The cmake compiles just fine but when i run make it just gives me an error saying Undefined referance to avformat_alloc_context(), it says this for all functions but the structs, typedefs and defines compile just fine. Here are my files:

./cmake:

cmake_minimum_required(VERSION 3.14)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(Interview C CXX)

add_definitions(-DGL_SILENCE_DEPRECATION)

SET(EXECUTABLE_OUTPUT_PATH ${dir}/bin )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin )

file(GLOB_RECURSE SOURCE_FILES 
    ${CMAKE_SOURCE_DIR}/src/*.c
    ${CMAKE_SOURCE_DIR}/src/*.cpp
    # last resort
    ${CMAKE_SOURCE_DIR}/lib/ffmpeg/*/*.c
)
    
# Add header files
file(GLOB_RECURSE HEADER_FILES 
    ${CMAKE_SOURCE_DIR}/src/*.h
    ${CMAKE_SOURCE_DIR}/src/*.hpp
    # last resort
    ${CMAKE_SOURCE_DIR}/lib/ffmpeg/*/*.h
)

list(APPEND SOURCES
    src/main.cpp
    src/vipch.h
    src/VideDecoder.cpp
    src/VideoDecoder.h
    src/Application.cpp
    src/Application.h
    src/OpenGLRender.cpp
    src/OpenGLRender.h
    src/Layer.h
    src/Layer.cpp

    # last resort
    lib/ffmppeg/*/*.h
    lib/ffmppeg/*/*.c
)
add_executable(Interview src/main.cpp)

# ffmpeg linking -----------------------------------------------


add_library(ffmpeg STATIC IMPORTED)
add_subdirectory(lib/ffmpeg)

target_include_directories(Interview PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/lib/ffmpeg)

link_directories(${CMAKE_SOURCE_DIR}/lib)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")

The cmake files are a bit messy since i am trying to figure this out. ./lib/ffmpeg/cmake:

cmake_minimum_required(VERSION 3.14)
project(ffmpeg)

list(APPEND SOURCES
    libavformat/avformat.h
    libavformat/avformat.c
)

file(GLOB_RECURSE SOURCE_FILES 
    ${CMAKE_SOURCE_DIR}/*/*.c
)   
# Add header files
file(GLOB_RECURSE HEADER_FILES 
    ${CMAKE_SOURCE_DIR}/*/*.h
)

include_directories(${CMAKE_SOURCE_DIR}/libavformat)
link_directories(${CMAKE_SOURCE_DIR}/libavformat)

and my main file that i run my test src/main.cpp:

  extern "C"{
    #include <libavformat/avformat.h>
}

#include <iostream>
int main(int argc, char* argv[]) {
    AVFormatContext* context; 
    context = avformat_alloc_context();
    std::cout << context << std::endl;
}

Again, no problem when using type's like AVFormatContext and defines but when i use a function like avformat_alloc_context it gives of this error (the source files are inside ffmpeg, i can inspect them with ctrl right-click on VSCode):

 /usr/bin/ld: CMakeFiles/Interview.dir/src/main.cpp.o: in function main': main.cpp: 
   (.text+0x4e): undefined reference to avformat_alloc_context' collect2: error: ld returned 1 
   exit status make[2]: *** [CMakeFiles/Interview.dir/build.make:84: bin/Interview] Error 1 
   make[1]: *** [CMakeFiles/Makefile2:96: CMakeFiles/Interview.dir/all] Error 2 make: *** 
   [Makefile:84: all] Error 2

Edit: I edited the cmake file but now im getting a different error:

find_path(AVCODEC_INCLUDE_DIR NAMES "avformat.h" PATHS 
${CMAKE_CURRENT_SOURCE_DIR}/lib/ffmpeg/libavformat)
find_library(AVCODEC_LIBRARY avformat)

link_directories(${CMAKE_SOURCE_DIR}/lib)

add_executable(Interview src/main.cpp)

target_include_directories(Interview PRIVATE ${AVCODEC_INCLUDE_DIR})
target_link_libraries(Interview PRIVATE avformat)

add_library(ffmpeg STATIC IMPORTED)
add_subdirectory(lib/ffmpeg)

target_include_directories(Interview PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/lib/ffmpeg)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")

Note: AVCODEC_LIBRARY returns notfound cmake error but just using avcodec in target_link_libraries somehow doesn't result in a cmake error, however when i run make like this it gives this error:

usr/bin/ld: cannot find -lavformat collect2: error: ld returned 1 exit status make[2]: *** [CMakeFiles/Interview.dir/build.make:84: bin/Interview] Error 1 make[1]: *** [CMakeFiles/Makefile2:96: CMakeFiles/Interview.dir/all] Error 2 make: *** [Makefile:84: all] Error 2
  • 1
    You may want to look at this question: [https://stackoverflow.com/questions/50760024/cmake-configuration-for-ffmpeg-in-c-project](https://stackoverflow.com/questions/50760024/cmake-configuration-for-ffmpeg-in-c-project) – drescherjm May 19 '22 at 16:36
  • 2
    `link_directories` does not link required libs to the target. Use `target_link_libraries`. – 273K May 19 '22 at 16:41
  • @drescherjm I mixed the accepted best answer with my ./cmake, now i get "Cannot find -lavformat" error when tring to run make. – user19068953 May 19 '22 at 17:56
  • "now i get "Cannot find -lavformat" error" - Please, update the code in the question post. Otherwise we can only guess which code causes that error. – Tsyvarev May 19 '22 at 18:10
  • @Tsyvarev I edited the question with everything i did. – user19068953 May 19 '22 at 18:39
  • For affect on the target, `link_directories` should be issued **before creation** of the target. In you case it is `add_executable(Interview src/main.cpp)` call. See [that answer](https://stackoverflow.com/a/40554704/3440745) to the duplicate question for more details. – Tsyvarev May 19 '22 at 20:21
  • @Tsyvarev still the same error – user19068953 May 19 '22 at 22:22
  • That mean you don't have `avformat` **library** under `lib/` directory in your source tree. As far as I understand, you doesn't have prebuilt libraries in your source tree. All you have is **source files** under `lib/ffmpeg`. You need to **build** them into the library for being able to use that library. Probably you forgot to call `add_library` in `lib/ffmpeg/CMakeLists.txt`: currently that script has no effect (variables, include and link directories are lost after exiting this script). – Tsyvarev May 20 '22 at 06:54
  • @Tsyvarev it seems that i forgot to call make install after configuring and calling make when compiling ffmpeg, that is resolved but now i have a different problem which i have just posted... – user19068953 May 21 '22 at 17:19

0 Answers0