Search code examples
cmakegstreamer

How to write a CMAKE file which successfully links gstreamer basic tutorial 5


I'm trying to write a CMAKE file which will compile the code found here.

My current CMAKE file looks like this and has successfully compiled and linked the previous tutorials. It also compiles this one but when linking I get the following error:

undefined reference to `gst_video_overlay_get_type'
undefined reference to `gst_video_overlay_set_window_handle'

The CMAKE file looks like so:

cmake_minimum_required(VERSION 3.18)
project(gstreamer)

set(CMAKE_CXX_STANDARD 20)

find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)

pkg_search_module(GST REQUIRED gstreamer-1.0>=1.4
        gstreamer-sdp-1.0>=1.4
        gstreamer-app-1.0>=1.4
        gstreamer-video-1.0>=1.4
        )


add_executable(gstreamer main.cpp)

target_include_directories(gstreamer PRIVATE ${GTK3_INCLUDE_DIRS} ${GST_INCLUDE_DIRS})
target_link_libraries(gstreamer ${GTK3_LIBRARIES} ${GST_LIBRARIES})

Other SO posts have suggested linking gstreamer-video-1.0 which I believe I am in:

target_link_libraries(gstreamer ${GST_LIBRARIES})

If I've misunderstood how PkgConfig works I'd much appreciate an explanation.

Thanks


Solution

  • This is how it works with modern target-based CMake:

    cmake_minimum_required(VERSION 3.15.3)
    project(gstreamer)
    
    set(CMAKE_CXX_STANDARD 20)
    
    find_package(PkgConfig REQUIRED)
    pkg_check_modules(gtk3 REQUIRED IMPORTED_TARGET gtk+-3.0)
    pkg_search_module(gstreamer REQUIRED IMPORTED_TARGET gstreamer-1.0>=1.4)
    pkg_search_module(gstreamer-sdp REQUIRED IMPORTED_TARGET gstreamer-sdp-1.0>=1.4)
    pkg_search_module(gstreamer-app REQUIRED IMPORTED_TARGET gstreamer-app-1.0>=1.4)
    pkg_search_module(gstreamer-video REQUIRED IMPORTED_TARGET gstreamer-video-1.0>=1.4)
    
    add_executable(my-gstreamer-app main.cpp)
    
    target_link_libraries(my-gstreamer-app
        PkgConfig::gtk3
        PkgConfig::gstreamer
        PkgConfig::gstreamer-sdp
        PkgConfig::gstreamer-app
        PkgConfig::gstreamer-video
    )
    

    Note the absence of target_include_directories. The imported targets contain all of this information, including linker flags, compiler flags, library paths, include paths, etc. Linking to the imported targets via target_link_libraries will pull all PUBLIC and INTERFACE properties from them into the my-gstreamer-app target.