In cmake, How to specific qt5 pacakge path - c++

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.

Related

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.

How to connect the QuaZip library in CMake

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})

Problems linking GLFW with CMake

I'm trying to add GLFW to my C++ project in CLion. I've downloaded the pre-built Windows binaries, and I'm attempting to add them to my CMake file, but I'm getting errors. Here's my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.12.3)
project(Streamlined)
set(CMAKE_CXX_STANDARD 17)
list(APPEND CMAKE_MODULE_PATH "$(CMAKE_CURRENT_LIST_DIR}/cmake")
add_executable(Streamlined src/main.cpp)
find_package(glfw3 REQUIRED)
include_directories(${GLFW_INCLUDE_DIRS})
target_link_libraries(${CMAKE_PROJECT_NAME} ${GLFW_LIBRARY})
And my cmake/Findglfw3.cmake file:
set(FIND_GLFW_PATHS ${CMAKE_SOURCE_DIR}/../Library/glfw-3.2.1.bin.WIN64/lib)
find_path(GLFW_INCLUDE_DIR glfw3.h
PATH_SUFFIXES include
PATHS ${FIND_GLFW_PATHS})
find_library(GLFW_LIBRARY NAMES libglfw3
PATH_SUFFIXES lib
PATHS ${FIND_GLFW_PATHS})
However, I'm getting the following error:
CMake Error at CMakeLists.txt:10 (find_package):
By not providing "Findglfw3.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "glfw3", but
CMake did not find one.
Could not find a package configuration file provided by "glfw3" with any of
the following names:
glfw3Config.cmake
glfw3-config.cmake
Add the installation prefix of "glfw3" to CMAKE_PREFIX_PATH or set
"glfw3_DIR" to a directory containing one of the above files. If "glfw3"
provides a separate development package or SDK, be sure it has been
installed.
I created the Findglfw3.cmake file, and appended it to CMAKE_MODULE_PATH, so I don't understand why I'm getting this error. I attempted this solution as well, but I'm getting a "PkgConfig not found" error.

Can't use protobuf in cmakelists.txt

I am trying to run the example given in protobuf repo here, the c++ version. I have successfully installed the library and am able to run the Makefile. But on running the CMakeLists.txt, I get this error:
CMake Error at CMakeLists.txt:9 (find_package):
Could not find a package configuration file provided by "protobuf" with any
of the following names:
protobufConfig.cmake
protobuf-config.cmake
Add the installation prefix of "protobuf" to CMAKE_PREFIX_PATH or set
"protobuf_DIR" to a directory containing one of the above files. If
"protobuf" provides a separate development package or SDK, be sure it has
been installed.
-- Configuring incomplete, errors occurred!
See also "/home/cortana/Projects/CppProjects/proto/build/CMakeFiles/CMakeOutput.log".
See also "/home/cortana/Projects/CppProjects/proto/build/CMakeFiles/CMakeError.log".
I have updated my LD_LIBRARY_PATH but this error is still there. How do I remove this error?
EDIT:
CMakeLists.txt:
# Minimum CMake required
cmake_minimum_required(VERSION 2.8.12)
# Project
project(protobuf-examples)
include(FindProtobuf)
# Find required protobuf package
find_package(protobuf CONFIG REQUIRED)
if(protobuf_VERBOSE)
message(STATUS "Using Protocol Buffers ${Protobuf_VERSION}")
endif()
set(CMAKE_INCLUDE_CURRENT_DIR TRUE)
set(CMAKE_PREFIX_PATH
${CMAKE_PREFIX_PATH}
${THIRDPARTY_DIR}/protobuf-3.1.0
)
include_directories(${ProtobufIncludePath})
# http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
if(MSVC AND protobuf_MSVC_STATIC_RUNTIME)
foreach(flag_var
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
if(${flag_var} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endif(${flag_var} MATCHES "/MD")
endforeach()
endif()
foreach(example add_person list_people)
set(${example}_SRCS ${example}.cc)
set(${example}_PROTOS addressbook.proto)
#Code Generation
if(protobuf_MODULE_COMPATIBLE) #Legacy Support
protobuf_generate_cpp(${example}_PROTO_SRCS ${example}_PROTO_HDRS ${${example}_PROTOS})
list(APPEND ${example}_SRCS ${${example}_PROTO_SRCS} ${${example}_PROTO_HDRS})
else()
foreach(proto_file ${${example}_PROTOS})
get_filename_component(proto_file_abs ${proto_file} ABSOLUTE)
get_filename_component(basename ${proto_file} NAME_WE)
set(generated_files ${basename}.pb.cc ${basename}.pb.h)
list(APPEND ${example}_SRCS ${generated_files})
add_custom_command(
OUTPUT ${generated_files}
COMMAND protobuf::protoc
ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} -I ${CMAKE_CURRENT_SOURCE_DIR} ${proto_file_abs}
COMMENT "Generating ${generated_files} from ${proto_file}"
VERBATIM
)
endforeach()
endif()
#Executable setup
set(executable_name ${example}_cpp)
add_executable(${executable_name} ${${example}_SRCS} ${${example}_PROTOS})
if(protobuf_MODULE_COMPATIBLE) #Legacy mode
target_include_directories(${executable_name} PUBLIC ${PROTOBUF_INCLUDE_DIRS})
target_link_libraries(${executable_name} ${PROTOBUF_LIBRARIES})
else()
target_link_libraries(${executable_name} protobuf::libprotobuf)
endif()
endforeach()
EDIT 2:
After trying for 2 hours, I couldn't fix the CMakeLists.txt provided by google examples. I wrote this basic one and it works for me:
PROJECT(protopuff)
CMAKE_MINIMUM_REQUIRED (VERSION 3.5)
SET(CMAKE_CXX_FLAGS "-g -Wall -Werror -std=c++11")
INCLUDE(FindProtobuf)
FIND_PACKAGE(Protobuf REQUIRED)
INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
PROTOBUF_GENERATE_CPP(PROTO_SRC PROTO_HEADER addressbook.proto)
ADD_LIBRARY(proto ${PROTO_HEADER} ${PROTO_SRC})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
ADD_EXECUTABLE(${CMAKE_PROJECT_NAME}_add add_person.cc)
ADD_EXECUTABLE(${CMAKE_PROJECT_NAME}_list list_people.cc)
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME}_add proto ${PROTOBUF_LIBRARY})
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME}_list proto ${PROTOBUF_LIBRARY})
Your problem is here:
find_package(protobuf CONFIG REQUIRED)
The name should start with uppercase: Protobuf. And that is the reason why your version is working; because in there, you have used correct case (last code snippet line 6):
find_package(Protobuf REQUIRED)
Here cmake documentation for find_package
The command searches for a file called <name>Config.cmake or <lower-case-name>-config.cmake for each name specified.
in this thread fraser solved the problem but if you need to develop according to protobuf CMake config and find_package command in CMake for finding protobuf libraries. your protobuf library must be compiled with CMake and do not use configure routine .
after compile protobuf with CMake , a config file named protobuf-config.cmake will be generated into the prefix/lib/CMake/protobuf directory.
The CmakeList.txt that is provided by the OP works on Linux but it does NOT work on Windows.
There is a way to make the actual CMakeList.txt work without any changes. The problem is that it requires the CONFIG parameter and that part is not documented anywhere. We need to provide the path to that config using -Dprotobuf_DIR parameter while generating the project.
On Windows, wherever you have installed protobuf, it will have bin, cmake, include, lib folders. We need to give the path of this cmake folder as an argument, like following:
cmake -G "Visual Studio 16 2019" -A x64 -B _build2 -Dprotobuf_DIR=C:/protobuf/install/cmake
This will build a solution file in the current directory.

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.