我正在尝试创建一个非常简单的程序,该程序将使用OpenGL将视频文件的第一帧简单地打印到屏幕上。我刚刚开始编写程序,但是在使用Libav时遇到了链接器错误。我已经用Google搜索问题很多次了,但是在使用Cmake时没有发现任何有用的信息。但是Cmake确实可以编译,但是在链接时会卡住。 该文件似乎是真正的问题:
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <inttypes.h>
}
bool load_frame(const char* filename, int* width, int* height, unsigned char** data) {
AVFormatContext* av_format_ctx = avformat_alloc_context();
if (!av_format_ctx) {
printf("Couldn't created AVFormatContext\n");
return false;
}
if (avformat_open_input(&av_format_ctx, filename, NULL, NULL) != 0) {
printf("Couldn't open video file\n");
return false;
}
return false;
}
两个Cmake文件可以正常工作,但是在这里以防万一: 这是主要的:
cmake_minimum_required(VERSION 3.14)
project(autoedit C CXX)
set(CMAKE_CXX_STANDERD 14)
add_subdirectory(lib/glfw)
add_subdirectory(lib/FFmpeg)
add_definitions(-DGL_SILENCE_DEPRECATION)
if(APPLE)
list(APPEND EXTRA_LIBS
"-framework OpenGL"
)
elseif(WIN32)
list(APPEND EXTRA_LIBS
"-lglu32 -lopengl32"
)
else()
list(APPEND EXTRA_LIBS
"-lGL -lGLU -lX11"
)
endif()
list(APPEND SOURCES
src/main.cpp
src/load_frame.cpp
)
add_executable(autoedit ${SOURCES})
target_link_libraries(autoedit FFmpeg glfw ${EXTRA_LIBS})
第二个:
cmake_minimum_required(VERSION 3.14)
project(FFmpeg)
find_package(PkgConfig REQUIRED)
pkg_check_modules(AVCODEC REQUIRED IMPORTED_TARGET libavcodec)
pkg_check_modules(AVFORMAT REQUIRED IMPORTED_TARGET libavformat)
pkg_check_modules(AVFILTER REQUIRED IMPORTED_TARGET libavfilter)
pkg_check_modules(AVDEVICE REQUIRED IMPORTED_TARGET libavdevice)
pkg_check_modules(AVUTIL REQUIRED IMPORTED_TARGET libavutil)
pkg_check_modules(SWRESAMPLE REQUIRED IMPORTED_TARGET libswresample)
pkg_check_modules(SWSCALE REQUIRED IMPORTED_TARGET libswscale)
add_library(FFmpeg INTERFACE IMPORTED GLOBAL)
target_include_directories(FFmpeg INTERFACE
${AVCODEC_INCLUDE_DIRS}
${AVFORMAT_INCLUDE_DIRS}
${AVFILTER_INCLUDE_DIRS}
${AVDEVICE_INCLUDE_DIRS}
${AVUTIL_INCLUDE_DIRS}
${SWRESAMPLE_INCLUDE_DIRS}
${SWSCALE_INCLUDE_DIRS}
)
target_link_options(FFmpeg INTERFACE
${AVCODEC_LDFLAGS}
${AVFORMAT_LDFLAGS}
${AVFILTER_LDFLAGS}
${AVDEVICE_LDFLAGS}
${AVUTIL_LDFLAGS}
${SWRESAMPLE_LDFLAGS}
${SWSCALE_LDFLAGS}
)
这是非常简单的代码,但是在使用make命令时,我总是得到:
usr/bin/ld: CMakeFiles/autoedit.dir/src/load_frame.cpp.o: in function `load_frame(char const*, int*, int*, unsigned char**)':
load_frame.cpp:(.text+0x2c): undefined reference to `avformat_alloc_context'
/usr/bin/ld: load_frame.cpp:(.text+0x66): undefined reference to `avformat_open_input'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/autoedit.dir/build.make:102: autoedit] Error 1
make[1]: *** [CMakeFiles/Makefile2:136: CMakeFiles/autoedit.dir/all] Error 2
make: *** [Makefile:130: all] Error 2
为错误。我不是很明显,而是我很愚蠢。这应该是一个简单的修复程序,但我不太了解出了什么问题。这些函数似乎存在于头文件中。如果有人对如何解决未定义的引用有任何想法,那将非常有帮助。预先感谢任何可以尝试和帮助的人。