0

I am trying to compile a C++ program using some functions from the GStreamer C library. 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. 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.

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:

#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

Sorry for all the code, wanted to be as explicative as possible.

George
  • 124
  • 4
  • 11
  • What does `pkg-config --cflags --libs gstreamer-1.0` return on the two systems? *Looks like* you don't have `gstreamer` installed on the Mac. If you are sure it's installed then maybe `pkg-config` cannot find it. – Marco Bonelli Feb 22 '20 at 22:32
  • The command `pkg-config --libs gstreamer-1.0` on Linux returns `-lgstreamer-1.0 -lgobject-2.0 -lglib-2.0` and on 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` – George Feb 22 '20 at 23:18
  • The command `pkg-config --cflags gstreamer-1.0` on 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`, on 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` – George Feb 22 '20 at 23:20
  • @MarcoBonelli I believe if the issue was that Ubuntu or MacOS couldn't find gstreamer-1.0 with pkg-config then it would fail in a previous stage than the linking stage? – George Feb 22 '20 at 23:38
  • 1
    Yeah you're right, that would have probably failed sooner. – Marco Bonelli Feb 22 '20 at 23:41

1 Answers1

0

I've found the answer to my problem here: 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):

gcc `pkg-config gstreamer-0.10 --cflags` main.c -o player.out `pkg-config gstreamer-0.10 --libs`
George
  • 124
  • 4
  • 11