I am not a C++ programmer, only have made a course a while ago. Using homebrew I installed libbitcoin and was hoping that I can reference the library like I was able to reference the boost libraries. I also realized that there are no links in /usr/local/bin to the Cellar.
I think I could get it working by using the absolute paths but I am looking for the proper way of handling this constellation that I just mentioned.
Current CMake:
cmake_minimum_required(VERSION 2.8.4)
project(cplusplus)
message(STATUS "start running cmake...")
find_package(boost 1.65.1 COMPONENTS system filesystem REQUIRED)
find_package(libbitcoin 3.3.0 COMPONENTS system filesystem REQUIRED)
message("system: ${CMAKE_SYSTEM_PREFIX_PATH}")
find_library(LIB_BITCOIN libbitcoin)
message("bitcoin: ${LIB_BITCOIN}")
if(Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(cplusplus main.cpp)
if(Boost_FOUND)
target_link_libraries(cplusplus ${Boost_LIBRARIES})
endif()
Currently I get these errors:
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" /Users/johndow/Documents/Workspace/bitcoin-code/cplusplus
-- start running cmake...
-- Boost version: 1.65.1
CMake Error at CMakeLists.txt:8 (find_package):
By not providing "Findlibbitcoin.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"libbitcoin", but CMake did not find one.
Could not find a package configuration file provided by "libbitcoin"
(requested version 3.3.0) with any of the following names:
libbitcoinConfig.cmake
libbitcoin-config.cmake
Add the installation prefix of "libbitcoin" to CMAKE_PREFIX_PATH or set
"libbitcoin_DIR" to a directory containing one of the above files. If
"libbitcoin" provides a separate development package or SDK, be sure it has
been installed.
-- Configuring incomplete, errors occurred!
See also "/Users/johndoe/Documents/Workspace/bitcoin-code/cplusplus/cmake-build-debug/CMakeFiles/CMakeOutput.log".
[Finished]
You seem to have double lookup for libbitcoin library in your CMakeLists file. You are first looking for it by:
find_package(libbitcoin ...)
and then by
find_library(LIB_BITCOIN libbitcoin)
Cmake is not happy (as your error message says) with the find_package() clause as libbitcoin does not provide cmake configuration by itself. You have many ways how to fix it, just two of them:
remove find_package() and use only find_library(), I think this is the simpler way and your project should work this way
provide cmake configuration for libbitcoin by yourself. Good introduction how to do this is here (and good to read anyway):
https://cmake.org/Wiki/CMake:How_To_Find_Libraries
As far as I know, currently libbitcoin does not export any <libbitcoin>Config.cmake package.
But it does export a libbitcoin.pc file for generic use with pkg-config.
ie: /usr/local/lib/pkgconfig/libbitcoin.pc
If you get results from invoking pkg-config --cflags libbitcoin then it's there.
And then you can put something like this in your CMakeLists.txt:
#use this if libbitcoin is installed to some custom location
set(ENV{PKG_CONFIG_PATH} "/path/to/libbitcoin/pkgconfig/:$ENV{PKG_CONFIG_PATH}")
#then later..
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIB_BITCOIN REQUIRED libbitcoin)
#then later..
target_link_libraries(${PROJECT_NAME} PRIVATE ${LIB_BITCOIN_LIBRARIES})
target_include_directories(${PROJECT_NAME} PRIVATE ${LIB_BITCOIN_INCLUDE_DIRS})
That should pull in boost, make the libbitcoin includes visible and solve all manner of compiler and linker woes.
(Or if you are feeling mad, you could always make use of this gist).
Related
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})
I am trying to make a self contained library whose dependencies are self contained.All installs are installed to <repo_dir>/libs//build.
To find dependencies in cmake build system I use find_package. Because all installs are in a custom location I point I use HINTS to help out like so -
# CMake 3.16.3
# ../lib/Catch2/build is where Catch2Config.cmake is located
find_package(Catch2 REQUIRED HINTS "../lib/Catch2/build" NO_MODULE)
include_directories(tests)
add_executable(tests tests.cpp)
target_link_libraries(tests PRIVATE project_warnings project_options Catch2::Catch2)
I recognize that Catch2 likely doesn't need this as it is header only, however I am trying to learn what cmake is doing.
The CMakeLists.txt above prints out
include could not find load file:
<repo_dir>/lib/Catch2/build/Catch2Targets.cmake │
Call Stack (most recent call first):
tests/CMakeLists.txt:1 (find_package)
It looks like a target file is needed as well. I cannot find one in the Catch2 repo.
I am also trying the same process for folly, however it does not work for me either.
Using the following in module mode for Boost worked. Why does it work?
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${PROJECT_SOURCE_DIR}/lib/boost_1_75_0/")
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "${PROJECT_SOURCE_DIR}/lib/boost_1_75_0/libs")
find_package(Boost 1.60.0 REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(${CMAKE_PROJECT_NAME} ${Boost_LIBRARIES})
Could someone help to uncover my problems and how I should go about using
Header only libraries
Static lib binaries
Self-Compiled Libraries
Whose resources are in unusual places?
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.
This question is one that has occurred before on SO. For instance this question:
Cmake doesn't find Boost
But the answers there and elsewhere don't seem to work.
On Ubuntu 16.04 with the stock boost 1.58.0 installed, I have also built, in a custom location, boost 1.68.0.
Now I am trying to compile a simple c++ program using boost, with cmake. It does not find boost. Either version (although 1.68.0 is the one I really want to use).
It gives:
-- Could NOT find Boost (missing: Boost_DIR)
The CMakeLists.txt file is below. CMake 3.12.1 is being used.
cmake_minimum_required(VERSION 3.0.0)
set(CMAKE_CXX_STANDARD 17)
project(mytest CXX)
set(Boost_DEBUG ON)
set(Boost_DETAILED_FAILURE_MSG ON)
set(BOOST_ROOT /home/hal/projects/boost/boost)
# set(Boost_DIR /home/hal/projects/boost/boost)
#set(Boost_USE_DEBUG_LIBS ON)
#set(Boost_USE_STATIC_LIBS ON)
#set(Boost_USE_MULTITHREADED ON)
# set(Boost_USE_STATIC_RUNTIME OFF)
#set(Boost_INCLUDE_DIR /home/hal/projects/boost/boost )
set(Boost_ADDITIONAL_VERSIONS "1.58" "1.58.0")
#set(BOOST_INCLUDEDIR /home/hal/projects/boost/boost/include )
#set(BOOST_LIBRARYDIR /home/hal/projects/boost/boost/lib )
#SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "/home/hal/projects/boost/boost")
#SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "/home/hal/projects/boost/boost/lib")
find_package(Boost 1.68.0 COMPONENTS system date_time PATHS /home/hal/projects/boost/boost )
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(mytest main.cpp)
target_link_libraries(mytest ${Boost_LIBRARIES} stdc++)
endif()
Unless you work with implementing of searching packaging, option PATHS for find_package is not such useful, just remove it:
find_package(Boost 1.68.0 COMPONENTS system date_time)
Explanations
There are two ways for search packages in CMake:
With XXXConfig.cmake script, shipped with a specific package installation. In this script all paths (libraries, include directories, etc.) are hardcoded.
With FindXXX.cmake script, shipped with CMake itself. This script searches the libraries and headers under the system directories (like /usr/local/lib) but also takes hints from the user.
By default, the second way is tried; only if FindXXX.cmake script is absent, the first way is used.
But some options for find_package are applied only for the first way, and PATHS is exactly an option of this sort: it specifies paths where XXXConfig.cmake file can be found. With such options, find_package uses the second way - it tries to find XXXConfig.cmake script and execute it. But it seems that your Boost installation lacks of this config script, so CMake fails to find Boost.
I am having trouble telling cmake to find openssl on my mac. Let me say, I am an amateur at using cmake, as my acquaintance with it, is very recent and occasional. That said, I can do simple things in cmake such as create small projects build them and run them. Some commands (find_library, find_package, ...) always perplex me.
I downloaded and installed openssl (./configure, make and make install) and it has put the files in /opt/openssl
I was googling around to see help for a simple inclusion openssl in my program.
One search result points to a command FindOpenSSL here (https://cmake.org/cmake/help/v3.6/module/FindOpenSSL.html). When i used FindOpenSSL, cmake emitted error saying FindOpenSSL is not a command.
I also tried find_library and find_package commands.
But if I check the variable OPENSSL_FOUND, it is still undefined.
It looks like I have to hardcode (or pass through -D options to cmake) the variables for openssl. What is the best practice here? The reference manual of cmake seems to be like piece of puzzle, as I don't understand the true difference between find_library and find_package, or when to use in preference of the other.
Am I the only one facing this, or more souls around here that are confused and struggling?
Thanks to anyone pointing me the right direction and helping me out of this confusion.
My current CMakeLists.txt looks like below:
ravindranaths-MacBook-Pro:crypt_handlers ravindranath$ cat CMakeLists.txt
cmake_minimum_required (VERSION 3.5.1)
# search for CPP_HOME. If found use this as the install_root dir.
# else, use /usr/local
message (STATUS "Searching for environment var CPP_HOME ...")
if (DEFINED ENV{CPP_HOME})
message (STATUS "Found CPP_HOME: " $ENV{CPP_HOME})
set (CPP_HOME $ENV{CPP_HOME})
else()
message (STATUS "Could not find. Treating /usr/local as CPP_HOME...")
set (CPP_HOME /usr/local)
endif()
set(Boost_USE_STATIC_LIBS ON)
###########################################################
set(CMAKE_INCLUDE_PATH /usr/local/ssl/include/openssl)
set(OPENSSL_USE_STATIC_LIBS ON)
############################################################
find_package(Boost 1.45.0 COMPONENTS system )
find_package (Threads)
set (CMAKE_CXX_STANDARD 11)
include_directories(../../include)
include_directories(../../include/ext)
include_directories(../../include/ext/spdlog)
include_directories(${CPP_HOME}/externals/plog/include)
add_executable(crypt app.cpp)
link_directories(../../build/src)
target_link_libraries (crypt ${CMAKE_THREAD_LIBS_INIT})
find_library(OpenSSL_LIB libcrypto.a libssl.a)
#find_package(OpenSSL)
if (OPENSSL_FOUND)
message (STATUS "OPENSSL found")
message (STATUS "OpenSSL INclude directories:" OPENSSL_INCLUDE_DIR)
else()
message (FATAL_ERROR "OpenSSL Not found.")
endif()
include_directories(${OPENSSL_INCLUDE_DIR})
target_link_libraries(crypt ${CMAKE_DL_LIBS})
TARGET_LINK_LIBRARIES(crypt ${OPENSSL_LIBRARIES} ${LIBYAML_LIBRARIES} pthread -ldl)
#target_link_libraries(crypt /usr/local/ssl/lib/libcrypto.a /usr/local/lib/libssl.a)
target_link_libraries(crypt /usr/local/lib/libcrypto.a /usr/local/lib/libssl.a)
if(Boost_FOUND)
include_directories(${include_directories} ${Boost_INCLUDE_DIRS})
target_link_libraries(crypt nettu ${Boost_LIBRARIES})
endif()
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/dist/bin)
UPDATE
now, I just use find_package(OpenSSL), instead of find_libraries(...). And, I am running cmake as below:
cmake -DCMAKE_BUILD_TYPE=Debug -DOPENSSL_ROOT_DIR=/opt/openssl/ -DOPENSSL_LIBRARIES=/opt/openssl/lib/ ..
cmake succeeds by writing the makefiles. However, I get a linker error when running make. The error points to missing libssl.a like below:
make[2]: *** No rule to make target `/usr/local/lib/libssl.a', needed by `examples/crypt_handlers/crypt'. Stop.
What I don't understand is that the make is still looking for libssl.a in "/usr/local/lib/.." instead of /opt/openssl/lib.
How to fix this?
You can change as follows
#find_library(OpenSSL_LIB libcrypto.a libssl.a)
find_package(OpenSSL)
And run
cmake -DOPENSSL_ROOT_DIR=/opt/openssl
Where OPENSSL_ROOT_DIR is hint for FindOpenSSL.cmake standard module where to find root for OpenSSL installation.