【问题标题】:Linking stage fails on Ubuntu but not MacOS链接阶段在 Ubuntu 但不是 MacOS 上失败
【发布时间】:2020-02-22 22:29:49
【问题描述】:

我正在尝试使用 GStreamer C 库中的一些函数来编译 C++ 程序。在 MacOS Catelina 上使用 g++(随 homebrew 安装的 9.2 版)时,程序编译和链接很好,但是在 Ubuntu 18.04 上使用 g++ 时,由于未定义的引用,它在链接阶段失败。我没有在两个平台之间更改任何代码,代码非常简单,唯一使用的外部库是 GStreamer。

错误:

g++ `pkg-config --cflags --libs gstreamer-1.0` -std=gnu++11 -Wall -Wextra -c -o img_sys.o img_sys.cpp
g++ -std=gnu++11 -Wall -Wextra   -c -o img_cameras.o img_cameras.cpp
g++ `pkg-config --cflags --libs gstreamer-1.0` -std=gnu++11 -Wall -Wextra -c -o img_cameras_gstreamer.o img_cameras_gstreamer.cpp
g++ `pkg-config --cflags --libs gstreamer-1.0` -std=gnu++11 -Wall -Wextra -o img_sys img_sys.o img_cameras.o img_cameras_gstreamer.o
img_cameras_gstreamer.o: In function `gst_caps_unref':
img_cameras_gstreamer.cpp:(.text+0x14): undefined reference to `gst_mini_object_unref'
img_cameras_gstreamer.o: In function `ImgSys::GStreamerCameras::msgReceivedCallback(_GstBus*, _GstMessage*, void*)':
img_cameras_gstreamer.cpp:(.text+0x75): undefined reference to `gst_message_get_structure'
img_cameras_gstreamer.cpp:(.text+0xa3): undefined reference to `gst_structure_get'
img_cameras_gstreamer.cpp:(.text+0x109): undefined reference to `gst_message_parse_error'
img_cameras_gstreamer.cpp:(.text+0x125): undefined reference to `g_print'
...
collect2: error: ld returned 1 exit status
makefile:10: recipe for target 'img_sys' failed
make: *** [img_sys] Error 1

生成文件:

CXX = g++
CXXFLAGS = -std=gnu++11 -Wall -Wextra
GSTREAMER = `pkg-config --cflags --libs gstreamer-1.0`

img_sys: img_sys.o img_cameras.o img_cameras_gstreamer.o
    $(CXX) $(GSTREAMER) $(CXXFLAGS) -o $@ $^

img_sys.o: img_sys.cpp
    $(CXX) $(GSTREAMER) $(CXXFLAGS) -c -o $@ $^
img_cameras.o: img_sys.hpp
img_cameras_gstreamer.o: img_cameras_gstreamer.cpp
    $(CXX) $(GSTREAMER) $(CXXFLAGS) -c -o $@ $^

img_cameras_gstreamer.cpp 包括:

#include <iostream>
#include <string>
#include <vector>
#include <gst/gst.h>
#include "img_sys.hpp"
#include "img_cameras_gstreamer.hpp"

img_cameras_gstreamer.hpp:

#ifndef IMG_CAMERAS_GSTREAMER_GUARD_H
#define IMG_CAMERAS_GSTREAMER_GUARD_H

#include <gst/gst.h>
#include "img_sys.hpp"

namespace ImgSys {
    class GStreamerCameras: public ImgSys::Cameras {
    private:
        GstElement *pipeline;
        GMainLoop *loop;
        guint bus_watch_id;
        static gboolean msgReceivedCallback(__attribute__((unused)) GstBus*, GstMessage*, gpointer);
        static gboolean pipelineTimeOut(__attribute__((unused)) gpointer);
    public:
        void build();
        void beginCapture();
    };
}

#endif

抱歉所有代码,希望尽可能解释清楚。

【问题讨论】:

  • pkg-config --cflags --libs gstreamer-1.0 在两个系统上返回什么? 看起来您没有在 Mac 上安装 gstreamer。如果您确定它已安装,那么可能pkg-config 找不到它。
  • Linux 上的命令pkg-config --libs gstreamer-1.0 返回-lgstreamer-1.0 -lgobject-2.0 -lglib-2.0,MacOS 上返回-L/usr/local/Cellar/gstreamer/1.16.2/lib -L/usr/local/Cellar/glib/2.62.5/lib -L/usr/local/opt/gettext/lib -lgstreamer-1.0 -lgobject-2.0 -lglib-2.0 -lintl
  • 命令pkg-config --cflags gstreamer-1.0在Linux上:-pthread -I/usr/include/gstreamer-1.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include,在MacOS上:-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.5/include -I/usr/local/Cellar/glib/2.62.5/include/glib-2.0 -I/usr/local/Cellar/glib/2.62.5/lib/glib-2.0/include -I/usr/local/opt/gettext/include -I/usr/local/Cellar/pcre/8.44/include
  • @MarcoBonelli 我相信如果问题是 Ubuntu 或 MacOS 无法使用 pkg-config 找到 gstreamer-1.0,那么它会在链接阶段之前的阶段失败?
  • 是的,你是对的,这可能会很快失败。

标签: c++ c linker g++ gstreamer


【解决方案1】:

我在这里找到了问题的答案:https://stackoverflow.com/a/8359200/4638141

事实证明,较新版本的 Ubuntu 的 gcc 需要以不同的顺序使用它们的 pkg-config 选项(取自链接的帖子):

gcc `pkg-config gstreamer-0.10 --cflags` main.c -o player.out `pkg-config gstreamer-0.10 --libs`

【讨论】:

    猜你喜欢
    • 2019-08-14
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 2020-03-14
    相关资源
    最近更新 更多