我试图在C++项目中使用ffmpeg中的libavformat。我已经安装了自制的。
我的CMakeLists.txt:
cmake_minimum_required(VERSION 3.14)
project(av_test)
set(CMAKE_CXX_STANDARD 11)
INCLUDE_DIRECTORIES(/usr/local/Cellar/ffmpeg/4.2.1_2/include)
LINK_DIRECTORIES(/usr/local/Cellar/ffmpeg/4.2.1_2/lib)
add_executable(av_test main.cpp)
TARGET_LINK_LIBRARIES(av_test libavformat)在运行cmake时,我会得到以下错误:
ld: library not found for -llibavformat
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [av_test] Error 1
make[2]: *** [CMakeFiles/av_test.dir/all] Error 2
make[1]: *** [CMakeFiles/av_test.dir/rule] Error 2
make: *** [av_test] Error 2进入/usr/local/Cellar/ffmpeg/4.2.1_2/lib的快速find libavformat*返回:
libavformat.58.29.100.dylib
libavformat.58.dylib
libavformat.a
libavformat.dylib在/usr/local/Cellar/ffmpeg/4.2.1_2/include/libavformat中也有avformat.h
我的main.cpp:
#include <libavformat/avformat.h>
int main() {
AVFormatContext *pFormatContext;
return 0;
}我在mac 10.14上运行cmake版本3.15.5,ffmpeg版本4.2.1
发布于 2019-11-29 19:17:46
这里的问题是从错误中发现的:
ld: library not found for -llibavformat如您所见,它以'-l‘和'lib’作为前缀,即使'-l‘应该在库名前面替换'lib’。TARGET_LINK_LIBRARIES函数似乎会自动解析带有前缀-l (或lib)的库名。因此,您需要编写以下任一项:
TARGET_LINK_LIBRARIES(av_test avformat) TARGET_LINK_LIBRARIES(av_test -lavformat)
在开始时添加-l并没有什么区别,但仍然为cmake所接受。
https://stackoverflow.com/questions/59109722
复制相似问题