libavformat无法在macOS上链接

发布于 2025-01-23 13:30:57 字数 2271 浏览 0 评论 0原文

我正在尝试通过FFMPEG教程,但是当我尝试构建项目时,我会收到以下错误。我首先认为Cmake的find_pathfind_library正在链接错误的文件mark setchell表明事实并非如此。现在,我不确定为什么会发生链接器错误。这是未能编译和上述错误的最低示例。

[build] [1/1 100% :: 0.076] Linking CXX executable example
[build] FAILED: example 
[build] : && /usr/bin/clang++ -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names -L/usr/local/opt/llvm/lib CMakeFiles/example.dir/main.cpp.o -o example  /usr/local/lib/libavformat.dylib && :
[build] Undefined symbols for architecture x86_64:
[build]   "avformat_open_input(AVFormatContext**, char const*, AVInputFormat const*, AVDictionary**)", referenced from:
[build]       _main in main.cpp.o
[build]   "avformat_free_context(AVFormatContext*)", referenced from:
[build]       _main in main.cpp.o
[build]   "avformat_alloc_context()", referenced from:
[build]       _main in main.cpp.o
[build] ld: symbol(s) not found for architecture x86_64
[build] clang: error: linker command failed with exit code 1 (use -v to see invocation)
[build] ninja: build stopped: subcommand failed.

cmakelists.txt:

cmake_minimum_required(VERSION 3.21)
set(CMAKE_CXX_STANDARD 17)


find_path(AVFORMAT_INCLUDE_DIRS libavformat/avformat.h REQUIRED)
find_library(AVFORMAT_LIBRARY avformat REQUIRED)

message(WARNING "AVFORMAT_LIBRARY=" ${AVFORMAT_LIBRARY})
message(WARNING "AVFORMAT_INCLUDE_DIRS=" ${AVFORMAT_INCLUDE_DIRS})

add_executable(example main.cpp)
target_link_libraries(example ${AVFORMAT_LIBRARY})
target_include_directories(example PRIVATE ${AVFORMAT_INCLUDE_DIRS})

main.cpp:

#include <libavformat/avformat.h>

#include <stdio.h>


int main(int argc, char **argv) {
    if (argc < 2) {
        printf("example <input_file>");
        exit(EXIT_FAILURE);
    }

    AVFormatContext *pFormatContext = avformat_alloc_context();

    avformat_open_input(&pFormatContext, argv[1], nullptr, nullptr);
    printf("Format %s, duration %lld us", pFormatContext->iformat->long_name, pFormatContext->duration);


    avformat_free_context(pFormatContext);
}

I'm trying to go through an ffmpeg tutorial however when I try to build my project I get the following error. I initialy thought that cmake's find_path and find_library was linking the wrong files Mark Setchell showed that this was not the case. Now I'm not sure why the linker errors are occurring. Here is the minimum example that fails to compile and the aforementioned error.

[build] [1/1 100% :: 0.076] Linking CXX executable example
[build] FAILED: example 
[build] : && /usr/bin/clang++ -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names -L/usr/local/opt/llvm/lib CMakeFiles/example.dir/main.cpp.o -o example  /usr/local/lib/libavformat.dylib && :
[build] Undefined symbols for architecture x86_64:
[build]   "avformat_open_input(AVFormatContext**, char const*, AVInputFormat const*, AVDictionary**)", referenced from:
[build]       _main in main.cpp.o
[build]   "avformat_free_context(AVFormatContext*)", referenced from:
[build]       _main in main.cpp.o
[build]   "avformat_alloc_context()", referenced from:
[build]       _main in main.cpp.o
[build] ld: symbol(s) not found for architecture x86_64
[build] clang: error: linker command failed with exit code 1 (use -v to see invocation)
[build] ninja: build stopped: subcommand failed.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.21)
set(CMAKE_CXX_STANDARD 17)


find_path(AVFORMAT_INCLUDE_DIRS libavformat/avformat.h REQUIRED)
find_library(AVFORMAT_LIBRARY avformat REQUIRED)

message(WARNING "AVFORMAT_LIBRARY=" ${AVFORMAT_LIBRARY})
message(WARNING "AVFORMAT_INCLUDE_DIRS=" ${AVFORMAT_INCLUDE_DIRS})

add_executable(example main.cpp)
target_link_libraries(example ${AVFORMAT_LIBRARY})
target_include_directories(example PRIVATE ${AVFORMAT_INCLUDE_DIRS})

main.cpp:

#include <libavformat/avformat.h>

#include <stdio.h>


int main(int argc, char **argv) {
    if (argc < 2) {
        printf("example <input_file>");
        exit(EXIT_FAILURE);
    }

    AVFormatContext *pFormatContext = avformat_alloc_context();

    avformat_open_input(&pFormatContext, argv[1], nullptr, nullptr);
    printf("Format %s, duration %lld us", pFormatContext->iformat->long_name, pFormatContext->duration);


    avformat_free_context(pFormatContext);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文