【问题标题】:Linking against external library with CMake使用 CMake 链接外部库
【发布时间】:2015-08-05 20:56:03
【问题描述】:

在我的 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})

查找 Spotify.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 运行这些文件时没有错误,但是编译我的项目会给我未定义的参考错误。怎么了?

【问题讨论】:

    标签: cmake libspotify clion


    【解决方案1】:

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

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

    target_link_libraries(spotti Spotify)
    

    find_package() 不执行自动链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-17
      相关资源
      最近更新 更多