Undefined reference to 'libvlc_new\" but linke

2019-09-08 16:29发布

问题:

I want to use the VLC library in a ROS-based project using C++. I am using QT Creator as code editor.

I tried to follow the following tutorial to implement a simple playback of a mp3 file: A simple C program to play mp3 using libvlc

Since then I'm getting the following exceptions:
undefined reference to `libvlc_new'
undefined reference to `libvlc_media_new_path'
undefined reference to `libvlc_media_player_new_from_media'
undefined reference to `libvlc_media_release'
undefined reference to `libvlc_media_player_play'
undefined reference to `libvlc_media_player_stop'
undefined reference to `libvlc_media_player_release'
undefined reference to `libvlc_release'
collect2: ld returned 1 exit status
make[2]: *** [../bin/my_face_tracker_demo] Error 1
make[1]: *** [CMakeFiles/my_face_tracker_demo.dir/all] Error 2
make: *** [all] Error 2
The process "/usr/bin/make" exited with code 2.
Error while building project my_qbo_interaction (target: Desktop)
When executing build step 'Make'

Of course I added #include "vlc/vlc.h" to the file. Furthermore, I followed the instructions on http://www.cmake.org/Wiki/CMake:How_To_Find_Libraries to make sure I add the references for the linker. This is what I added to the cmakelist.txt-file:

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
find_package(LIBVLC REQUIRED)
include_directories(${LIBVLC_INCLUDE_DIRS})
set(LIBS ${LIBS} ${LIBVLC_LIBRARIES})

make is able to compile the file. It is also able to find the LIBVLC libraries. Here a part of the output from make:

-- Found LibVLC include-dir path: /usr/include -- Found LibVLC library path:/usr/lib/libvlc.so -- Found LibVLCcore library path:/usr/lib/libvlccore.so -- Found LibVLC version: 1.1.12 (searched for: 0.0) -- Configuring done -- Generating done CMake Warning: Manually-specified variables were not used by the project:

CMAKE_TOOLCHAIN_FILE

But I still get the above error messages... Can anybody help me?

回答1:

It looks like the VLC libraries have been correctly found, but you need to actually link them into your executable.

You do this via the target_link_libraries command. For example:

set(LIBS ${LIBS} ${LIBVLC_LIBRARIES})
add_executable(MyExe ${TheSources})
target_link_libraries(MyExe ${LIBS})