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