I'm currently at a bit of a loss when trying to use the Libspotify SDK in my CMake project.
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
)
My libspotify folder is in the project root directory. I think I might be missing a target_link_libraries() in my CMakeLists.txt. Or not?
Anyway, CMake runs without errors with these files, but compiling my project gives me undefined reference errors. What's wrong?
I think I might be missing a target_link_libraries() in my CMakeLists.txt.
Yes, you should explicitly link with Spotify:
target_link_libraries(spotti Spotify)
find_package() does not perform automatic linking.
Related
I recently updated my gtkmm framework to version 4.0.0 and updated code to be compatible with the framework. I'm using msys2 in JetBrains CLion on Windows. However when I'm compiling project I got wall of warnings and errors in various framework headers like gtkmm, glibmm etc. I can't find errors which could be related to my code. Here is my Cmake configurations
cmake_minimum_required (VERSION 3.17.5)
set (CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS " -lstdc++fs")
project (Accounter)
find_package(Boost 1.73 COMPONENTS date_time REQUIRED)
FIND_LIBRARY (LIBZIP_LIBRARY NAMES zip)
FIND_PATH (LIBZIP_INCLUDE_DIR zip.h PATH_SUFFIXES include/zip include ) # Find header
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE(PkgConfig REQUIRED)
pkg_check_modules(GTKMM gtkmm-4.0)
include_directories( ${GTKMM_INCLUDE_DIRS} )
link_directories( ${GTKMM_LIBRARY_DIRS} )
#ADD_DEFINITIONS(${GTK_CFLAGS_OTHER})
FIND_PACKAGE_HANDLE_STANDARD_ARGS(libzip DEFAULT_MSG LIBZIP_LIBRARY LIBZIP_INCLUDE_DIR)
link_directories((Accounting))
add_subdirectory(Accounting)
link_directories(OdsFile)
add_subdirectory(OdsFile)
link_directories(GUI)
add_subdirectory(GUI)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(Accounter main.cpp)
target_link_libraries(Accounter LINK_PUBLIC Accounting OdsFile GUI ${Boost_LIBRARIES} ${LIBZIP_LIBRARY} stdc++fs ${GTKMM_LIBRARIES} )
else()
message( FATAL_ERROR "Boost library is required for this library")
ENDIF().
The compile log is under this link . Do I miss something in cmake configuration? How it should be compiled? I'll provide additional info, files if it will be required. I think errors should be on my side not on framework.
I am trying to link my local version of gflags (which is in ~/mylibs/gflags) while compiling google-test. In gtest's CMakeLists.txt, it's using find_package:
if (WITH_GFLAGS)
find_package (gflags 2.2.0)
if (gflags_FOUND)
set (HAVE_LIB_GFLAGS 1)
determine_gflags_namespace (gflags_NAMESPACE)
endif (gflags_FOUND)
endif (WITH_GFLAGS)
I don't want to modify the CMakeLists.txt file. Is there anyway to tell cmake to find the package in my folder ?
Thanks!
The gflags documentation describes specifying the location of a non-standard installation when using CMake:
The gflags_DIR variable must be set to the <prefix>/lib/cmake/gflags directory containing the gflags-config.cmake file
In your case, this might look like:
cmake -Dgflags_DIR=/home/blackball/mylibs/gflags/lib/cmake/gflags [...]
Since you are integrating gflags as subproject in a super cmake, you basically want find_package() to be a no-op if you have it in your "meta-project" source dir. This can be done by overloading the find_package() command.
Top CMakeLists.txt:
# Use find_package everywhere, no-op if it's a subproject
macro(find_package)
if(NOT TARGET ${ARGV0} AND NOT TARGET ${ARGV0}::${ARGV0})
_find_package(${ARGV})
else()
if(TARGET ${ARGV0})
get_target_property(TGT_VER ${ARGV0} VERSION)
set(TGT ${ARGV0})
else()
get_target_property(TGT_VER ${ARGV0}::${ARGV0} VERSION)
set(TGT ${ARGV0}::${ARGV0})
endif()
message(STATUS "Found ${ARGV0}: CMake Target ${TGT} (found version \"${TGT_VER}\")")
set(${ARGV0}_FOUND TRUE)
endif()
endmacro()
note: for gflags you may need to "force" gflags_NAMESPACE also since gflags it's a source not a binary (cf glog issue ToDo)
I am using libpng in my project. Right now, I can compile my project with: g++ *.cpp `libpng-config --ldflags`
I want to switch to using CMake for easy recompiling but I don't know what to do for the libpng part. How do I provide `libpng-config --ldflags` with CMake?
I finally solved it using find_package. Thanks to this blog post.
find_package(PNG REQUIRED)
include_directories(${PNG_INCLUDE_DIR})
target_link_libraries(${MY_EXEC} ${PNG_LIBRARY})
I think the recommended and portable way it should be done using pkg-config, something like this:
# search for pkg-config
include (FindPkgConfig)
if (NOT PKG_CONFIG_FOUND)
message (FATAL_ERROR "pkg-config not found")
endif ()
# check for libpng
pkg_check_modules (LIBPNG libpng16 REQUIRED)
if (NOT LIBPNG_FOUND)
message(FATAL_ERROR "You don't seem to have libpng16 development libraries installed")
else ()
include_directories (${LIBPNG_INCLUDE_DIRS})
link_directories (${LIBPNG_LIBRARY_DIRS})
link_libraries (${LIBPNG_LIBRARIES})
endif ()
add_executable (app_png ${_MYSOURCES} ${LIBPNG_LINK_FLAGS})
I am a total noob concerning cmake. My CMakeLists is really basic:
cmake_minimum_required(VERSION 2.4.6)
#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
#For the Curses library to load:
SET(CURSES_USE_NCURSES TRUE)
include_directories(
"src/"
)
add_subdirectory(src)
when I make the linker does not find the ncurses commands and in the verbose mode of make I see that the compiler did not add the -lncurses. What do I have to add to the CMakeLists to make it work?
For the super noob, remember target_link_libraries() needs to be below add_executable():
cmake_minimum_required(VERSION 2.8) project(main)
find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})
add_executable(main main.cpp)
target_link_libraries(main ${CURSES_LIBRARIES})
before use some third party libs, you ought to find it!
in case of ncurses you need to add find_package(Curses REQUIRED) and then use ${CURSES_LIBRARIES} in a call to target_link_libraries() and target_include_directories(... ${CURSES_INCLUDE_DIR}).
I have a problem with CMake. I have wrote a CMakeList.txt file. But when I run it with Cmake I got a strange error "CMake Error at CMakeLists.txt:17 (target_link_libraries):
Cannot specify link libraries for target "debug" which is not built by this
project.".
Is it possible to create a Cmake file that can build a project file for Debug and Release mode at the same time? Or is there a simple way to fix this error?
My CMakeLists.txt looks like this:
cmake_minimum_required (VERSION 2.8)
project (SimuVille)
# Import required CMake files
set (CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules")
file(GLOB_RECURSE files
"*.cpp"
)
add_executable(debug ${files})
# Find the find Modules
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
#Find any version 2.X of SFML
#See the FindSFML.cmake file for additional details and instructions
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()
#Find SfeMovie
find_package(sfeMovie REQUIRED)
if(SFEMOVIE_FOUND)
include_directories(${SFEMOVIE_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFEMOVIE_LIBRARY})
endif()
#Find Assimp
find_package(ASSIMP REQUIRED)
if(ASSIMP_FOUND)
include_directories(${ASSIMP_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${ASSIMP_LIBRARY})
endif()
#Find DevIL
find_package(DevIL REQUIRED)
if(IL_FOUND)
include_directories(${IL_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${IL_LIBRARY})
target_link_libraries(${EXECUTABLE_NAME} ${ILU_LIBRARY})
target_link_libraries(${EXECUTABLE_NAME} ${ILUT_LIBRARY})
endif()
#Find opengl libs
find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIRS})
link_directories(${OpenGL_LIBRARY_DIRS})
add_definitions(${OpenGL_DEFINITIONS})
if(NOT OPENGL_FOUND)
message(ERROR " OPENGL not found!")
endif(NOT OPENGL_FOUND)
#file(GLOB_RECURSE hfiles
# "*.h"
#)
#add_executable(SimuVille ${hfiles})
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/Game)
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/GameEngine)
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/GameEngine/SfmlObject)
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/GameEngine/Camera)
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/GameEngine/OpenglObject)
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/GameEngine/Playable)
Edit: Added new source code.
Looks like your CMakeLists.txt doesn't contain either of two lines (which depends you are creating a library or a executable)
add_library(debug <files Name>)
OR
add_executable(debug <files Name>)
If you have these lines in your file, place it before target_link_libraries ()