链接外部库与CMake



当我在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运行没有错误与这些文件,但编译我的项目给了我未定义的引用错误。怎么了?

我想我可能在我的CMakeLists.txt中缺少target_link_libraries()

是的,你应该明确地与Spotify链接:

target_link_libraries(spotti Spotify)

find_package()不自动链接

相关内容

  • 没有找到相关文章

最新更新