Search code examples
c++macoscompilationcmakelibvlc

Cant run a c++ program using libvlc library (osx)


I'm trying to compile my c++ project using cmake with the libvlc library. My os is osx v10.9.2. My cmake version is 2.8.12. I can actually compile my program with cmake and make. But when I try to run the program I got the linking error:

dyld: Library not loaded: @loader_path/lib/libvlc.5.dylib

. Some internet search led me to a linking error coming from the fact that the libvlc is installed in the /Applications/VLC.app/...../lib could it be the problem? Should I specify somehow the absolute link to the library ? using rpath ? I'm a bit lost here.

Here is my CMakeLists.txt simplified:

cmake_minimum_required(VERSION 2.8.8)

set(LIBVLC_PATH "/Applications/VLC.app/Contents/MacOs")

find_path(LIBVLC_INCLUDE_DIR
        NAMES vlc/libvlc.h
        PATHS ${LIBVLC_PATH}/include
        PATH_SUFFIXES vlc
)

find_library( LIBVLC_LIBRARY
        NAMES libvlc vlc
        PATHS ${LIBVLC_PATH}/lib
        NO_DEFAULT_PATH
)
find_library( LIBVLC_LIBRARY NAMES libvlc vlc)

find_library( LIBVLC5_LIBRARY
        NAMES libvlc.5 vlc.5
        PATHS ${LIBVLC_PATH}/lib
        NO_DEFAULT_PATH
)
find_library( LIBVLC5_LIBRARY NAMES libvlc.5 vlc.5)

include_directories( ${LIBVLC_INCLUDE_DIR} )
link_directories( ${LIBVLC_PATH}/lib )

add_executable(
        Projet.x
        ${sourcefiles}
        ${headerfiles}
)

target_link_libraries(
        Projet.x
        ${LIBVLC_LIBRARY}
        ${LIBVLC5_LIBRARY}
)  

[edit solution]
Thanks to your link I manage to make it work using the script below:

#!/bin/bash
if [ $# -lt 2 ] ; then
  echo "Usage: $0 <vlc_path> <project_path>"
  exit 1
fi
VLC_PATH=$1
PROJECT_PATH=$2
install_name_tool -change @loader_path/lib/libvlc.5.dylib $VLC_PATH/lib/libvlc.5.dylib $YCSTATS_PATH/build/YCStats.x
install_name_tool -change @loader_path/lib/libvlccore.7.dylib $VLC_PATH/lib/libvlccore.7.dylib $PROJECT_PATH/build/YCStats.x

This script is called in CMakeList.txt with the command:

add_custom_command( TARGET Project.x
    POST_BUILD
    COMMAND ${CMAKE_SOURCE_DIR}/cmake/Modules/FixBundle.sh ${LIBVLC_PATH} ${CMAKE_CURRENT_SOURCE_DIR}
    COMMENT "Fixing application bundle for local run"
)

If you dont see any difference after that, try to modify your main source then save it and execute your make command (If no change applied to your file the binaries file is not compiled again) (Check for the line: "Fixing application bundle for local run")


Solution

  • Disclaimer: I don't know much about MacOSX development.

    I use this script for my project: http://git.videolan.org/?p=vlmc.git;a=blob;f=cmake/FixBundle.sh which is strongly based on recommendations from the lead dev for VLC on Mac.

    It's invoked from this CMakeLists.txt : http://git.videolan.org/?p=vlmc.git;a=blob;f=src/CMakeLists.txt#l288

    I'll admit that the CMakelists needs some cleanup, but it should be readable enough :)

    Sadly, I don't have the skills required to properly explain the reason for this, so I'll let someone who actually knows better explain it. It should nevertheless help you move forward!