使用CMake链接外部库

时间:2015-08-05 20:56:04

标签: cmake libspotify clion

我尝试在我的CMake项目中使用Libspotify SDK时,目前有点亏本。

的CMakeLists.txt:

cmake_minimum_required(VERSION 3.2)
project(spotti)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")

find_package(Spotify REQUIRED)
include_directories(${Spotify_INCLUDE_DIR})
set(LIBS ${LIBS} ${Spotify_LIBRARIES})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")

set(SOURCE_FILES main.cpp)
add_executable(spotti ${SOURCE_FILES})

FindSpotify.cmake:

# - Find Spotify
# Find the native Google Sparse Hash Etc includes
#
#  Spotify_INCLUDE_DIR - where to find sparse_hash_set, etc.
#  Spotify_FOUND       - True if Spotify found.


if (Spotify_INCLUDE_DIR)
    # Already in cache, be silent
    set(Spotify_FIND_QUIETLY TRUE)
endif ()

find_path(Spotify_INCLUDE_DIR api.h PATHS
    /opt/local/include/libspotify
    /usr/local/include/libspotify
    /usr/include/libspotify
    /home/denvercoder21/Development/cpp/spotti/libspotify/include/libspotify/
    )

set(Spotify_LIB_PATHS /usr/local/lib /opt/local/lib /home/denvercoder21/Development/cpp/spotti/libspotify/lib/)
find_library(Spotify_LIB NAMES spotify PATHS ${Spotify_LIB_PATHS})

if (Spotify_INCLUDE_DIR AND Spotify_LIB)
    set(Spotify_FOUND TRUE)
else ()
    set(Spotify_FOUND FALSE)
endif ()

if (Spotify_FOUND)
    if (NOT Spotify_FIND_QUIETLY)
        message(STATUS "Found Spotify: ${Spotify_INCLUDE_DIR}")
    endif ()
else ()
    message(STATUS "Not Found Spotify: ${Spotify_INCLUDE_DIR}")
    if (Spotify_FIND_REQUIRED)
        message(FATAL_ERROR "Could NOT find Spotify includes")
    endif ()
endif ()


mark_as_advanced(
    Spotify_LIB
    Spotify_INCLUDE_DIR
    )

我的libspotify文件夹位于项目根目录中。我想我的CMakeLists.txt中可能遗漏了target_link_libraries()。或不? 无论如何,CMake运行这些文件没有错误,但编译我的项目给了我未定义的引用错误。怎么了?

1 个答案:

答案 0 :(得分:2)

  

我想我的CMakeLists.txt中可能遗漏了target_link_libraries()

是的,您应该明确链接Spotify:

target_link_libraries(spotti Spotify)

find_package()不执行自动链接。