使用 CMake 链接外部库
Linking against external library with 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 运行这些文件没有错误,但编译我的项目给我未定义的引用错误。怎么了?
I think I might be missing a target_link_libraries()
in my CMakeLists.txt.
是的,你应该明确地 link 使用 Spotify:
target_link_libraries(spotti Spotify)
find_package()
不执行自动 linking。
我目前在我的 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 运行这些文件没有错误,但编译我的项目给我未定义的引用错误。怎么了?
I think I might be missing a
target_link_libraries()
in my CMakeLists.txt.
是的,你应该明确地 link 使用 Spotify:
target_link_libraries(spotti Spotify)
find_package()
不执行自动 linking。