简体   繁体   English

链接阶段在 Ubuntu 上失败,但在 MacOS 上没有

[英]Linking stage fails on Ubuntu but not MacOS

I am trying to compile a C++ program using some functions from the GStreamer C library.我正在尝试使用 GStreamer C 库中的一些函数编译 C++ 程序。 The program compiles and links just fine when using g++ (version 9.2 installed with homebrew) on MacOS Catelina, however when using g++ on Ubuntu 18.04 it fails at the linking phase due to undefined references.在 MacOS Catelina 上使用 g++(随 homebrew 安装的 9.2 版)时,程序编译和链接得很好,但是在 Ubuntu 18.04 上使用 g++ 时,由于未定义的引用,它在链接阶段失败。 I am not changing anything in terms of code between the two platforms, the code is pretty simple and the only external library being used is GStreamer.我没有在两个平台之间更改任何代码,代码非常简单,唯一使用的外部库是 GStreamer。

Error:错误:

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

Makefile:生成文件:

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 includes: 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: 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

Sorry for all the code, wanted to be as explicative as possible.对不起所有的代码,希望尽可能地解释。

I've found the answer to my problem here: https://stackoverflow.com/a/8359200/4638141我在这里找到了问题的答案: https : //stackoverflow.com/a/8359200/4638141

Turns out newer versions of Ubuntu's gcc need their pkg-config options in a different order like such (taken from the linked post):事实证明,较新版本的 Ubuntu 的 gcc 需要像这样的不同顺序的 pkg-config 选项(取自链接的帖子):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM