gpt4 book ai didi

c++ - 为什么 CMake 生成的 Makefile 找不到外部库?

转载 作者:搜寻专家 更新时间:2023-10-31 01:51:19 26 4
gpt4 key购买 nike

我是第一次尝试使用 CMake,但收效甚微。我的 CMakeLists.txt 是:

# Sets the version of CMake that is required.
cmake_minimum_required(VERSION 2.8.10)

# Name of the project.
project(QTCODERLIB)

# Adds common directories to the build.
include_directories(/usr/local/include)
link_directories(/usr/local/lib)

# Check for header files that we depend on.
include(CheckIncludeFiles)
check_include_files(libavcodec/avcodec.h HAVE_AVCODEC_H)

# Adds all of the source files.
file(GLOB SOURCE RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cxx)

# Builds a library named `qtcoder` from source.
add_library(qtcoder SHARED ${SOURCE})

# Links the library against third-party dependencies.
target_link_libraries(qtcoder avcodec)

我的 test.cxx 文件是:

#include "config.h"
#include <libavcodec/avcodec.h>

void f(void) {
avcodec_register_all();
}

制作。 && make VERBOSE=1 结果:

/usr/local/Cellar/cmake/2.8.10.1/bin/cmake -H/Users/phowes/Personal/QTCoder/QTCoderLib -B/Users/phowes/Personal/QTCoder/QTCoderLib --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/2.8.10.1/bin/cmake -E cmake_progress_start /Users/phowes/Personal/QTCoder/QTCoderLib/CMakeFiles /Users/phowes/Personal/QTCoder/QTCoderLib/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make -f CMakeFiles/qtcoder.dir/build.make CMakeFiles/qtcoder.dir/depend
cd /Users/phowes/Personal/QTCoder/QTCoderLib && /usr/local/Cellar/cmake/2.8.10.1/bin/cmake -E cmake_depends "Unix Makefiles" /Users/phowes/Personal/QTCoder/QTCoderLib /Users/phowes/Personal/QTCoder/QTCoderLib /Users/phowes/Personal/QTCoder/QTCoderLib /Users/phowes/Personal/QTCoder/QTCoderLib /Users/phowes/Personal/QTCoder/QTCoderLib/CMakeFiles/qtcoder.dir/DependInfo.cmake --color=
make -f CMakeFiles/qtcoder.dir/build.make CMakeFiles/qtcoder.dir/build
/usr/local/Cellar/cmake/2.8.10.1/bin/cmake -E cmake_progress_report /Users/phowes/Personal/QTCoder/QTCoderLib/CMakeFiles 1
[100%] Building CXX object CMakeFiles/qtcoder.dir/test.cxx.o
/usr/bin/c++ -Dqtcoder_EXPORTS -fPIC -I/usr/local/include -o CMakeFiles/qtcoder.dir/test.cxx.o -c /Users/phowes/Personal/QTCoder/QTCoderLib/test.cxx
Linking CXX shared library libqtcoder.dylib
/usr/local/Cellar/cmake/2.8.10.1/bin/cmake -E cmake_link_script CMakeFiles/qtcoder.dir/link.txt --verbose=1
/usr/bin/c++ -dynamiclib -Wl,-headerpad_max_install_names -o libqtcoder.dylib -install_name /Users/phowes/Personal/QTCoder/QTCoderLib/libqtcoder.dylib CMakeFiles/qtcoder.dir/test.cxx.o -L/usr/local/lib -lavcodec
Undefined symbols for architecture x86_64:
"avcodec_register_all()", referenced from:
f() in test.cxx.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [libqtcoder.dylib] Error 1
make[1]: *** [CMakeFiles/qtcoder.dir/all] Error 2
make: *** [all] Error 2

目标文件和库都编译为 64 位:

$ lipo -info CMakeFiles/qtcoder.dir/test.cxx.o
Non-fat file: CMakeFiles/qtcoder.dir/test.cxx.o is architecture: x86_64

$ lipo -info /usr/local/lib/libavcodec.dylib
Non-fat file: /usr/local/lib/libavcodec.dylib is architecture: x86_64

函数存在于库中:

$ nm /usr/local/lib/libavcodec.dylib | grep avcodec_register_all
0000000000033c01 T _avcodec_register_all

我在这里做错了什么?

最佳答案

avcodec 库是一个 C 库,查看您正在为 CXX 编译的 cmake 输出。这个问题解决了如何精确处理这种情况:g++ Linking Error on Mac while compiling FFMPEG

关于c++ - 为什么 CMake 生成的 Makefile 找不到外部库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13960221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com