How to connect the QuaZip library in CMake - c++

My project uses the QuaZip library, and I need to build the project through CMake. How to add this library to CMakeLists?
From the library I need JlCompress
My CMakeLists:
cmake_minimum_required(VERSION 3.6)
#set_property(GLOBAL PROPERTY USE_FOLDERS ON)
#set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "cmake")
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
project(Archiver LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui)
find_package(zlib)
find_package(QuaZip5)
include_directories(${QUAZIP_INCLUDE_DIRS})
set(project_ui
mainwindow.ui)
set(project_headers
archive.h
mainwindow.h)
set(project_sources
main.cpp
archive.cpp
mainwindow.cpp)
qt5_wrap_ui(project_headers_wrapped ${project_ui})
qt5_wrap_cpp(project_sources_moc ${project_headers})
add_executable(${PROJECT_NAME} ${project_headers} ${project_sources}
${project_sources_moc} ${project_headers_wrapped})
target_link_libraries(${PROJECT_NAME}
PUBLIC
Qt5::Core
Qt5::Gui
Qt5::Widgets
${QUAZIP_LIBRARIES}
)
Build error:
CMake Warning at CMakeLists.txt:13 (find_package): By not providing
"Findquazip.cmake" in CMAKE_MODULE_PATH this project has asked CMake
to find a package configuration file provided by "quazip", but CMake
did not find one.
Could not find a package configuration file provided by "quazip"
with any of the following names:
quazipConfig.cmake
quazip-config.cmake
Add the installation prefix of "quazip" to CMAKE_PREFIX_PATH or set
"quazip_DIR" to a directory containing one of the above files. If
"quazip" provides a separate development package or SDK, be sure it
has been installed.
CMake Error at CMakeLists.txt:37 (target_link_libraries): The
keyword signature for target_link_libraries has already been used with
the target "Archiver". All uses of target_link_libraries with a
target must be either all-keyword or all-plain.
The uses of the keyword signature are here:
CMakeLists.txt:31 (target_link_libraries)

The find script for quazip is named FindQuaZip5.cmake (it is renamed during installation). So for find quazip you need to use
find_package(QuaZip5)
Meaning of the find script is described in its head:
# QUAZIP_FOUND - QuaZip library was found
# QUAZIP_INCLUDE_DIR - Path to QuaZip include dir
# QUAZIP_INCLUDE_DIRS - Path to QuaZip and zlib include dir (combined from QUAZIP_INCLUDE_DIR + ZLIB_INCLUDE_DIR)
# QUAZIP_LIBRARIES - List of QuaZip libraries
# QUAZIP_ZLIB_INCLUDE_DIR - The include dir of zlib headers
That is, for use quazip with zlib in your code, add these lines:
include_directories(${QUAZIP_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${QUAZIP_LIBRARIES})

Related

Cannot link sndfile library to cmake project (MacOS)

Currently I'm trying to make some spectogram generation for my uni project. I'm trying to build a static library where all the magic will work and just call it from the main() function.
This is my cmake file:
set(CMAKE_CXX_STANDARD 17)
project(demo)
find_package(SndFile REQUIRED)
add_subdirectory(spectogram)
add_executable(demo main.cpp)
target_link_libraries (demo LINK_PUBLIC Spectrogram)
target_link_libraries(demo PRIVATE SndFile::sndfile)
I have installed libsndfile via homebrew, but find_package() refuses to locate the lib and throws this error:
CMake Error at CMakeLists.txt:6 (find_package):
By not providing "FindSndFile.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "SndFile", but
CMake did not find one.
Could not find a package configuration file provided by "SndFile" with any
of the following names:
SndFileConfig.cmake
sndfile-config.cmake
Add the installation prefix of "SndFile" to CMAKE_PREFIX_PATH or set
"SndFile_DIR" to a directory containing one of the above files. If
"SndFile" provides a separate development package or SDK, be sure it has
been installed.
-- Configuring incomplete, errors occurred!
I`ve made some research and found out that libsndfile does not have any .cmake configs inside like other libs, that I can link easily (like OpenCV or spdlog). I would really appreciate any help to solve this horror.
With help of Tsyvarev, I figured out the solution. I used the pkg-config module and a custom cmake file, I found on the web. I will include my final cmake in case someone else will need it:
cmake_minimum_required(VERSION 3.17)
set(CMAKE_CXX_STANDARD 17)
project(demo)
# - Try to find libsndfile
# Once done, this will define
#
# LIBSNDFILE_FOUND - system has libsndfile
# LIBSNDFILE_INCLUDE_DIRS - the libsndfile include directories
# LIBSNDFILE_LIBRARIES - link these to use libsndfile
# Use pkg-config to get hints about paths
find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
pkg_check_modules(LIBSNDFILE_PKGCONF sndfile)
endif(PKG_CONFIG_FOUND)
# Include dir
find_path(LIBSNDFILE_INCLUDE_DIR
NAMES sndfile.h
PATHS ${LIBSNDFILE_PKGCONF_INCLUDE_DIRS}
)
# Library
find_library(LIBSNDFILE_LIBRARY
NAMES sndfile libsndfile-1
PATHS ${LIBSNDFILE_PKGCONF_LIBRARY_DIRS}
)
find_package(PackageHandleStandardArgs)
find_package_handle_standard_args(LibSndFile DEFAULT_MSG LIBSNDFILE_LIBRARY LIBSNDFILE_INCLUDE_DIR)
if(LIBSNDFILE_FOUND)
set(LIBSNDFILE_LIBRARIES ${LIBSNDFILE_LIBRARY})
set(LIBSNDFILE_INCLUDE_DIRS ${LIBSNDFILE_INCLUDE_DIR})
endif(LIBSNDFILE_FOUND)
mark_as_advanced(LIBSNDFILE_LIBRARY LIBSNDFILE_LIBRARIES LIBSNDFILE_INCLUDE_DIR LIBSNDFILE_INCLUDE_DIRS)
include(FindPkgConfig)
pkg_search_module(SndFile REQUIRED sndfile)
include_directories(${LIBSNDFILE_INCLUDE_DIRS})
add_subdirectory(spectogram)
add_executable(demo main.cpp)
message(STATUS "sndfile include dirs path: ${LIBSNDFILE_INCLUDE_DIRS}")
message(STATUS "sndfile libs path: ${LIBSNDFILE_LIBRARIES}")
target_link_libraries (demo LINK_PUBLIC Spectrogram)
target_link_libraries(demo PRIVATE ${LIBSNDFILE_LIBRARIES})

How to use Caffe library in C++ project with CMakeLists.txt

I'm trying to use Caffe in my C++ project which I compile with CMakeLists.txt, but it doesn't want to work. My only line in the code is
#include <caffe/caffe.hpp>
I compiled Caffe myself, it is installed in the directory "/home/tamas/caffe". My CMakeLists.txt looks like this so far:
cmake_minimum_required (VERSION 3.5)
include(FindPkgConfig)
project (main)
set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD_REQUIRED TRUE)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -pthread")
set (source_dir "${PROJECT_SOURCE_DIR}/src/")
set (OpenCV_DIR "/home/tamas/opencv/include/opencv2")
set (Caffe_DIR "/home/tamas/caffe")
file (GLOB source_files "${source_dir}/ssd_video.cpp")
find_package(OpenCV 4.4.0 REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
find_package(Caffe REQUIRED)
include_directories(${Caffe_INCLUDE_DIRS})
add_executable (main ${source_files})
target_link_libraries(main ${OpenCV_LIBS})
target_link_libraries(main ${Caffe_LIBRARIES})
The error is the following:
CMake Error at CMakeLists.txt:24 (find_package):
By not providing "FindCaffe.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Caffe", but
CMake did not find one.
Could not find a package configuration file provided by "Caffe" with any of
the following names:
CaffeConfig.cmake
caffe-config.cmake
Add the installation prefix of "Caffe" to CMAKE_PREFIX_PATH or set
"Caffe_DIR" to a directory containing one of the above files. If "Caffe"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
The problem is that I have searched and I don't have a FindCaffe.cmake file on my computer. I found an example for CaffeConfig.cmake, but I tried it and it doesn't work either.
Is there a way I can link Caffe with my C++ project? Thanks!
To fix this issue you may do the following:
Download this FindCAFFE.cmake file
Create cmake dir in your repo root directory and put the downloaded file there.
Modify your CMake file:
add set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")
change set (Caffe_DIR "/home/tamas/caffe") to set (CAFFE_ROOT_DIR "/home/tamas/caffe")
change find_package(Caffe REQUIRED) to find_package(CAFFE REQUIRED)
use CAFFE_INCLUDE_DIRS and CAFFE_LIBRARIES for include directories and link libraries respectively
Clean up your build dir and run cmake command again
<library>_DIR should not be set manually in CMake code usually. There are better alternatives that should be used as setting these variable won't necessarily do what you want. It won't change where find_package finds its libraries.
The CaffeConfig.cmake file is generated when building Caffe. You should never download another one, these files are compatible only with a specific build configuration.
The Caffe library supports to be used with CMake, so FindCaffe.cmake is unnecessary.
For find_package to work, either set the <package>_ROOT variable (require CMake 3.12 minimum) or you must append the install path in CMAKE_PREFIX_PATH. Here's a CMake example that uses the prefix path:
# If you only built the library
list (APPEND CMAKE_PREFIX_PATH "/home/tamas/caffe/build-dir")
# If you installed the library there
list (APPEND CMAKE_PREFIX_PATH "/home/tamas/caffe/")
find_package(Caffe REQUIRED)
Note that the Caffe_LIBRARIES and Caffe_INCLUDE_DIRS won't be set. This is old CMake style and the Caffe library uses the new style. This is what you should do:
target_link_libraries(main PUBLIC caffe caffeproto)
This line add both include directory and adds linking to the libraries too.

CMake only accepting `find_package(Qt5Widgets REQUIRED)` in add_subdirectory, not in root project

Context
Using the below CMakeLists.txt, it build the Qt test project without issues ONLY when included to a parent project like:
RootProject
+--CMakeLists.txt // Parent CMake
+--TestQt
+--testwidget.cpp
+--testwidget.hpp // Empty class, just extends QWidget
+--CMakeLists.txt // My Test Project CMake
Parent Project just contain:
add_subdirectory( "TestQt" )
As soon as I try to build the "TestQt" project standalone, it just return an error like:
CMake Error at CMakeLists.txt:16 (find_package): By not providing
"FindQt5Widgets.cmake" in CMAKE_MODULE_PATH this project has asked
CMake to find a package configuration file provided by "Qt5Widgets",
but CMake did not find one.
Could not find a package configuration file provided by "Qt5Widgets"
with any of the following names:
Qt5WidgetsConfig.cmake /
qt5widgets-config.cmake
Add the installation prefix of "Qt5Widgets" to CMAKE_PREFIX_PATH or
set "Qt5Widgets_DIR" to a directory containing one of the above
files. If "Qt5Widgets" provides a separate development package or
SDK, be sure it has been installed.
CMAKE_PREFIX_PATH is empty in both cases.
Currently using Debian with a slightly old CMAKE 3.0.2
Question
What is wrong/ missing?
CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Widgets REQUIRED)
include_directories(${Qt5Widgets_INCLUDE_DIRS})
add_definitions(${Qt5Widgets_DEFINITIONS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
# Project name
project ( "TestQt" )
add_executable( UnitTest_TestQt UnitTest.cpp testwidget.cpp)
target_link_libraries(UnitTest_TestQt Qt5::Widgets)
find_package needs project to work properly. Move the line project("TestQt") to the top of the file, right after cmake_minimum_required.

CMake user built libraries; cannot specify link libraries for target

I'm building a project in Cpp that will communicate with my Java apps via rabbitmq and post updates to twitter. I'm using a few libraries from github
rabbitmq-c
Rabbit installed to /usr/local/lib64
jansson - json library
I installed this a while back for another project, went to /usr/local/lib
twitcurl - C lib for Twitter API
Got installed to /usr/local/lib
If it matters, I'm using CLion as my IDE, which displays jansson and rabbit under auto-complete when defining includes - so that's picking the libs off my system somehow
e.g.
#include <jansson.h>
#include <amqp.h>
I link them using the target_link_libraries(name libs...) and I see output saying
build$ cmake ..
CMake Error at CMakeLists.txt:30 (target_link_libraries):
Cannot specify link libraries for target "twitcurl" which is not built by
this project.
I set LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib64
I try to set the CMAKE_LIBRARY_PATH to include usr/local/lib and lib64 but doesn't seem to have any effect. Here's my CMakeLists.txt file
#
# This is a CMake makefile. You can find the cmake utility and
# information about it at http://www.cmake.org
#
cmake_minimum_required(VERSION 2.6)
set(PROJECT_NAME twitterUpdater)
set(SOURCE_FILES main.cpp)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "/usr/local/lib"
"/usr/local/lib64")
project(${PROJECT_NAME})
find_package(X11 REQUIRED)
find_package(OpenCV REQUIRED)
IF (X11_FOUND)
INCLUDE_DIRECTORIES(${X11_INCLUDE_DIR})
LINK_LIBRARIES(${X11_LIBRARIES})
ENDIF ( X11_FOUND )
IF (OpenCV_FOUND)
include_directories(${OpenCV_INCLUDE_DIRS})
link_libraries(${OpenCV_LIBS})
ENDIF(OpenCV_FOUND)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${project_name} twitcurl jansson rabbitmq)
What's confusing me is another project I have uses jansson by simply adding it here TARGET_LINK_LIBRARIES(${project_name} dlib jansson)
What did I miss?? Thanks
CMake variables are case sensitive, thus the variable ${project_name} results in an empty string. Use ${PROJECT_NAME} instead, i.e.:
target_link_libraries(${PROJECT_NAME} twitcurl jansson rabbitmq)
Running CMake with the flag --warn-uninitialized helps you detect mistakes like this.

In cmake, How to specific qt5 pacakge path

When I use following code:
find_package(Qt5Widgets REQUIRED)
add_executable(ex ex.cc)
target_link_libraries(ex Qt5::Widgets Qt5::DBus)
I get:
Error:By not providing "FindQt5Widgets.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "Qt5Widgets", but CMake did not find one.
Could not find a package configuration file provided by "Qt5Widgets" with any of the following names:
My qtbase path is in "/media/roroco/disk750/Downloads/qt5/qtbase", and I have compile it, How to specific this path in find_package
IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
SET(CMAKE_PREFIX_PATH $ENV{QT64_LOCATION})
message(STATUS "QT find path: $ENV{QT64_LOCATION}")
ELSE()
SET(CMAKE_PREFIX_PATH $ENV{QT_LOCATION})
message(STATUS "QT find path: $ENV{QT_LOCATION}")
ENDIF()
and add Paths to environment variables if it's not defined or hardcode it - CMAKE_PREFIX_PATH "C:\Qt\..\msvc2010\" or path to qmake in Linux
ie "/media/roroco/disk750/Downloads/qt5/bin/qmake"
Also you can set default qt path
/usr/lib/x86_64-linux-gnu/qt-default/qtchooser/default.conf
set it to /media/roroco/disk750/Downloads/qt5/bin/
Set your CMAKE_PREFIX_PATH to your Qt folder. For example:
SET(CMAKE_PREFIX_PATH "C:/Qt/5.5/msvc2013")
The path could be different depending on your OS.