CMake 成功,但链接时 make 失败

CMake is successful, but make fails when linking

我正在尝试使用 CMake 在 macOS 上编译我的项目。我通过 brew 安装了 gstreamer,我可以访问 include 目录。例如,这是 gstreamer 的包含目录:

/usr/local/Cellar/gstreamer/1.16.2/include/gstreamer-1.0/

当 运行 cmake 和以下 CMakeLists.txt 时,一切运行成功,但是当我尝试 link 时 make 失败并出现以下错误:

[ 25%] Linking CXX executable multiviewer
ld: library not found for -lgstreamer-1.0

CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)
project(application)

set(CMAKE_CXX_STANDARD 20)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

find_package(Qt5Core REQUIRED)
find_package(Qt5Quick REQUIRED)
find_package(Qt5WebSockets REQUIRED)

# Required for GStreamer
find_package(PkgConfig)

# Look for GStreamer installation
pkg_check_modules(GST REQUIRED gstreamer-1.0)

add_executable(application
        main.cpp qml.qrc server.cpp server.h
        ${PROTO_SRCS} ${PROTO_HDRS} client.cpp client.h)

# Qt5
target_link_libraries(application Qt5::Core Qt5::Quick Qt5::WebSockets)

# GStreamer
target_include_directories(application PUBLIC ${GST_INCLUDE_DIRS})
target_compile_options(application PUBLIC ${GST_CFLAGS})
target_link_libraries(application ${GST_LIBRARIES})

这是我安装的软件包:

brew install pkg-config
brew install gstreamer
brew install gst-plugins-base
brew install gst-plugins-good
brew install gst-plugins-bad
brew install gst-plugins-ugly
brew install gst-libav

pkg-config --cflags gstreamer-1.0 的输出:

-I/usr/local/Cellar/libffi/3.2.1/lib/libffi-3.2.1/include -I/usr/local/Cellar/gstreamer/1.16.2/include/gstreamer-1.0 -I/usr/local/Cellar/glib/2.62.4/include -I/usr/local/Cellar/glib/2.62.4/include/glib-2.0 -I/usr/local/Cellar/glib/2.62.4/lib/glib-2.0/include -I/usr/local/opt/gettext/include -I/usr/local/Cellar/pcre/8.43/include

pkg-config --libs gstreamer-1.0 的输出:

-L/usr/local/Cellar/gstreamer/1.16.2/lib -L/usr/local/Cellar/glib/2.62.4/lib -L/usr/local/opt/gettext/lib -lgstreamer-1.0 -lgobject-2.0 -lglib-2.0 -lintl

我还需要安装其他东西吗?或者我做错了什么?

错误:

ld: library not found for -lgstreamer-1.0

表示库 gstreamer-1.0 已传递给链接器,但链接器不知道在哪里可以找到它(可能是因为它不在典型的系统路径中)。由 CMake 提供 GST 库的 完整 路径。从 pkg_check_modules 文档中,这里有一些为通用包填充的与库相关的变量 XXX:

  • <XXX>_LIBRARIES: only the libraries (without the ‘-l’)

  • <XXX>_LINK_LIBRARIES: the libraries and their absolute paths

  • <XXX>_LIBRARY_DIRS: the paths of the libraries (without the ‘-L’)

GST_LIBRARIES 变量只会列出库名称 (gstreamer-1.0;gobject-2.0;glib-2.0;intl),但在这种情况下,我们还需要提供库路径。因此,将 target_link_libraries() 调用更改为使用 GST_LINK_LIBRARIES:

target_link_libraries(application PUBLIC ${GST_LINK_LIBRARIES})