CMake FindPackage in add_subdirectory from parent dirs cmake folder - cmake

I have a CMake structure:
mainproject/
mainproject/cmake/FindMyPackage.cmake
mainproject/CMakeLists.txt
plugin/
plugin/CMakeLists.txt
and in the above CMakeLists.txt I call
add_subdirectory(${LOOP_SUBDIR}/${subdir} examples/${subdir})
which add the plugin/CMakeLists.txt subdirectory outside the directory defined above.
In this second CMakeLists.txt I call FindPackage(MyPackage REQUIRED)
which give error message:
CMake Error at [...]/plugin/CMakeLists.txt:9 (find_package):
By not providing "FindMyPackage.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "MyPackage", but
CMake did not find one.
Is there any way to make it use the cmake files in the mainproject folder?

Just follow given error message, i.e. this must help:
# CMakeLists.txt
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/mainproject/cmake")
add_subdirectory(plugin)

Related

Hot do I link MPFR in CMake?

I'm totally new to cmake, I'm on MacOs and I'm trying to build a c++ library and I need to link my executables to mpfr in order to make it work
This is my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.19)
project(my_project)
set(CMAKE_CXX_STANDARD 14)
add_executable(my_project main.cpp)
find_package(GSL REQUIRED)
target_link_libraries(my_project GSL::gsl GSL::gslcblas)
find_package(Boost REQUIRED)
target_link_libraries(my_project Boost::boost)
find_package(MPFR REQUIRED) # <- It fails here!
target_link_libraries(my_project MPFR::mpfr)
When I try to build my project with CLion I get the following error:
CMake Error at CMakeLists.txt:13 (find_package):
By not providing "FindMPFR.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "MPFR", but
CMake did not find one.
Could not find a package configuration file provided by "MPFR" with any of
the following names:
MPFRConfig.cmake
mpfr-config.cmake
Add the installation prefix of "MPFR" to CMAKE_PREFIX_PATH or set
"MPFR_DIR" to a directory containing one of the above files. If "MPFR"
provides a separate development package or SDK, be sure it has been
installed.
After some research I found out that Cmake was linking correctly both GSL and Boost because there are both a /usr/local/share/cmake/Modules/FindGSL.cmake and a /usr/local/share/cmake/Modules/FindBoost.cmake file, So I looked online for a FindMPFR.cmake file to insert into the /usr/local/share/cmake/Modules/ directory, I tried with this one but the error remains the same. What am I doing wrong?
Edit:
Ok now my CMakeLists.txt file looks like this:
cmake_minimum_required(VERSION 3.19)
project(my_project)
set(CMAKE_CXX_STANDARD 14)
add_executable(my_project main.cpp )
# Append the cmake/ directory to the CMAKE_MODULE_PATH
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
find_package(GSL REQUIRED)
message(STATUS "GSL Found: ${GSL_FOUND}")
target_link_libraries(my_project GSL::gsl GSL::gslcblas)
find_package(Boost REQUIRED)
message(STATUS "Boost Found: ${Boost_FOUND}")
target_link_libraries(my_project Boost::boost)
find_package(MPFR REQUIRED)
message(STATUS "MPFR Found: ${MPFR_FOUND}")
target_link_libraries(my_project ${MPFR_LIBRARIES})
And it works fine :)

How can I set CMAKE_MODULE_PATH for doing regular and out-of-source builds in CMake?

I have a line like this in my CMakeLists.txt
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "cmake")
This is so that some custom libraries can be found with a directory tree that looks like this:
CMakeLists.txt
cmake/
|-- FindSomeLibrary.cmake
|-- FindAnotherLibrary.cmake
Normally I build simply like this:
cmake .
Which works fine. However, I want to provide a debug and release build using a script like this:
mkdir release
cd release
cmake -DCMAKE_BUILD_TYPE=Release ../
However, now it cannot find the cmake modules.
Is there a way to set CMAKE_MODULE_PATH such that it works for both in-source and out-of-source builds?
As proposed in the comments:
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
and always try to set all paths relative to some CMAKE_* directory. That way you will miss many errors. ; )
As CMAKE_MODULE_PATH is a list, so it's better to use:
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

Error with cmake and jsonCPP static library target "jsoncpp_lib_static"

When I try to use cmake on the jsonCPP i get the following error
CMake Error at lib_json/CMakeLists.txt:73 (INSTALL):
install TARGETS given no ARCHIVE DESTINATION for static library target
"jsoncpp_lib_static"
I use the command from readme:
cmake -DCMAKE_BUILD_TYPE=debug -DJSONCPP_LIB_BUILD_STATIC=ON -DJSONCPP_LIB_BUILD_SHARED=OFF -G "Unix Makefiles" ../..
From the error, it looks as though you're pointing CMake to the CMakeLists.txt inside "/jsoncpp/src" rather than the root one at "/jsoncpp".
The root CMakeLists.txt defines the variable ARCHIVE_INSTALL_DIR at this point and it's used in the "/jsoncpp/src/lib_json/CMakeLists.txt" at this point to define the target's ARCHIVE DESTINATION.
Since you're skipping the root CMakeLists.txt, this variable never gets set.
The error message mentions the path lib_json/CMakeLists.txt:73, and this is relative to the "main" CMakeLists.txt - i.e. the one you pointed CMake to when you first executed it. So CMake thinks the root is "/jsoncpp/src" instead of the real root.
Basically, to fix your error, clean out your build folder then rerun CMake to point to the "/jsoncpp" folder.
By the way, although the docs don't specifically mention it, I think the CMAKE_BUILD_TYPE is case-sensitive. You should be doing -DCMAKE_BUILD_TYPE=Debug.

CMake error: For MinGW make to work correctly sh.exe must NOT be in your path

I am trying to built a "Hello World" project with Qt 5 and cmake under MinGW.
This is the CMakeLists.txt file (taken from the on-line doc):
project(Qt5_cmake_test)
cmake_minimum_required(VERSION 2.8.11)
set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.1.1/5.1.1/mingw48_32")
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Find the QtWidgets library
find_package(Qt5Widgets)
# Add the source files from the current directory
aux_source_directory(. SRC_LIST)
# Tell CMake to create the executable
add_executable(${PROJECT_NAME} WIN32 ${SRC_LIST})
# Use the Widgets module from Qt5
target_link_libraries(${PROJECT_NAME} Qt5::Widgets)
The source code is the one generated automatically when creating a new project (which produces an empty window).
Configuring from the Windows command prompt with: cmake -G "MinGW Makefiles" ..\Qt5_cmake_test
I get these errors:
CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/CMakeMinGWFindMake.cmake:20 (message):
sh.exe was found in your PATH, here:
C:/Program Files (x86)/Git/bin/sh.exe
For MinGW make to work correctly sh.exe must NOT be in your path.
Run cmake from a shell that does not have sh.exe in your PATH.
If you want to use a UNIX shell, then use MSYS Makefiles.
Call Stack (most recent call first):
CMakeLists.txt:8 (project)
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_C_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_C_COMPILER
CMake Error: Could not find cmake module file:C:/Users/pietro.mele/projects/tests/buildSystem_test/Qt5_cmake_test-build/CMakeFiles/2.8.11.2/CMakeCCompiler.cmake
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_CXX_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_CXX_COMPILER
CMake Error: Could not find cmake module file:C:/Users/pietro.mele/projects/tests/buildSystem_test/Qt5_cmake_test-build/CMakeFiles/2.8.11.2/CMakeCXXCompiler.cma
ke
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
So it seems it is not able to find the compiler. Is there a way to let cmake find it on its own, or just giving it the CMAKE_PREFIX_PATH directory?
Do I have to manually specify all those variables in the makefile or as environment variables in Windows?
I tried both from the standard Windows command prompt and from the one provided by Qt, with the same result. Is it OK to build from the Windows command prompt, or should I do it from the MinGW's shell?
Platform:
Qt 5.1
CMake 2.8.11.2
MinGW/GCC 4.8
Windows 7
Get the git path out of your PATH before running cmake.
Here is the magic to do that:
set PATH=%PATH:C:/Program Files (x86)/Git/bin;=%
This CMakeLists.txt file works properly:
project(Qt5_cmake_test)
cmake_minimum_required(VERSION 2.8.11)
set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.1.1/5.1.1/mingw48_32")
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Find the Qt libraries
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
# Add the source files from the current directory
aux_source_directory(. SRC_LIST)
# Tell CMake to create the executable
add_executable(${PROJECT_NAME} WIN32 ${SRC_LIST})
# Use Qt5 modules
target_link_libraries(${PROJECT_NAME}
Qt5::Widgets
Qt5::WinMain)
The changes are:
Added find_package(Qt5Core REQUIRED).
Added Qt5::WinMain to target_link_libraries.
In some of my answer here on SO, I have described. CMake does not like sh.exe.
sh.exe was found in your PATH, here:
C:/Program Files (x86)/Git/bin/sh.exe
Solution : Rename C:/Program Files (x86)/Git/bin/sh.exe shortly.
For example:
C:/Program Files (x86)/Git/bin/shxx.exe
But do not forget when everything is built. rename properly again.

CMake Can't find Eigen3

I have copied FindEigen3.cmake into my source directory.
I then added:
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR})
to my CMakeLists.txt to tell CMake to use this .cmake file.
Then in my CMakeLists.txt I do:
FIND_PACKAGE(Eigen3)
I have the environent variable EIGEN3_INCLUDE_DIR set to /home/doriad/src/eigen
When I run CMake, I get:
-- Could NOT find Eigen3 (missing: EIGEN3_INCLUDE_DIR EIGEN3_VERSION_OK) (Required is at least version "2.91.0")
This is a fresh clone from Mercurial, so the version should be at least 3.
Any suggestions?
If I set the module path INCORRECTLY, I get some clues:
Adjust CMAKE_MODULE_PATH to find FindEigen3.cmake or set Eigen3_DIR to the
directory containing a CMake configuration file for Eigen3. The file will
have one of the following names:
Eigen3Config.cmake
eigen3-config.cmake
However, I didn't find either of those files in either the source dir or build dir of Eigen3. I tried to set Eigen3_DIR=/home/doriad/src/eigen, but that didn't work either.
Thoughts?
Thanks,
David
It worked for me, when I installed the eigen3 package (e.g. using -DCMAKE_INSTALL_PREFIX=/home/doriad/install), and also set the CMAKE_INSTALL_PREFIX variable of the cmake package that is using eigen3 to the same directory.
Because of the problems with findscripts, I've actually started using pkg-config instead. For that you need to enable pkg-config support in the eigen3 cmake, and use the pkg-config macro in your own cmake script. Advantage is that you don't need to set any environment variables anymore.
find_package( PkgConfig )
pkg_check_modules( EIGEN3 REQUIRED eigen3 )
include_directories( ${EIGEN3_INCLUDE_DIRS} )