cmake cannot include custom module - cmake

I'm trying to include my custom cmake module.
My folder hierarchy is like so:
- project
- cmake
- add_FetchContent_MakeAvailable.cmake
- CMakeLists.txt
CMakeLists.txt:
cmake_minimum_required(VERSION 3.12.0)
project(proj
VERSION 1.0.0
LANGUAGES CXX)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
include(FetchContent)
if(${CMAKE_VERSION} VERSION_LESS 3.14)
include(add_FetchContent_MakeAvailable)
endif()
But I get the following error:
include could not find load file:
add_FetchContent_MakeAvailable
Any ideas?

Related

CMake - get cmake variable value from the subdirectory [duplicate]

This question already has an answer here:
cmake variable scope, add_subdirectory
(1 answer)
Closed 7 months ago.
I have two projects - the first one contains the second one as a subdirectory. in the second project I add the path to the my cmake modules in CMAKE_MODULE_PATH, and in the first project I add the second one using the add_subdirectory command. Then I try to include the module in the first project, but cmake saying me:
CMake Error at tests/CMakeLists.txt:7 (include):
include could not find requested file
and the CMAKE_MODULE_PATH is empty.
How I can save value of CMAKE_MODULE_PATH to then use in the first project?
that's my CMakeLists:
2nd project:
cmake_minimum_required(VERSION 3.23 FATAL_ERROR)
set(UTILS_PROJECT_NAME "utils")
set(UTILS_LIB_NAME "utils")
set(UTILS_VERSION_MAJOR 0)
set(UTILS_VERSION_MINOR 0)
set(UTILS_VERSION_PATCH 1)
set(UTILS_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
if(${CMAKE_SOURCE_DIR} STREQUAL ${UTILS_SOURCE_DIR})
set(UTILS_STANDALONE ON)
message(STATUS "utils standalone")
endif()
option(UTILS_BUILD_DOCS "Build the utils documentation" OFF)
option(UTILS_BUILD_TESTS "Guild the utils tests and examples" ${UTILS_STANDALONE})
project(${UTILS_PROJECT_NAME}
VERSION ${UTILS_VERSION_MAJOR}.${UTILS_VERSION_MINOR}.${UTILS_VERSION_PATCH}
LANGUAGES CXX
)
...
setup library target
...
if(NOT "${UTILS_SOURCE_DIR}/cmake" IN_LIST CMAKE_MODULE_PATH)
list(APPEND CMAKE_MODULE_PATH "${UTILS_SOURCE_DIR}/cmake")
endif()
if (${UTILS_BUILD_DOCS})
add_subdirectory(docs)
endif()
if(${UTILS_BUILD_TESTS})
add_subdirectory(tests)
endif()
1st project:
cmake_minimum_required(VERSION 3.23 FATAL_ERROR)
set(ENGINE_PROJECT_NAME "engine")
set(ENGINE_LIB_NAME "engine")
set(ENGINE_VERSION_MAJOR 0)
set(ENGINE_VERSION_MINOR 0)
set(ENGINE_VERSION_PATCH 1)
set(ENGINE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
if(${CMAKE_SOURCE_DIR} STREQUAL ${ENGINE_SOURCE_DIR})
set(ENGINE_STANDALONE ON)
message(STATUS "engine standalone")
endif()
option(ENGINE_BUILD_TESTS "Build the tests and examples" ${ENGINE_STANDALONE})
project(${ENGINE_PROJECT_NAME}
VERSION ${ENGINE_VERSION_MAJOR}.${ENGINE_VERSION_MINOR}.${ENGINE_VERSION_PATCH}
LANGUAGES CXX
)
# bad way
list(APPEND CMAKE_MODULE_PATH "${ENGINE_SOURCE_DIR}/src/ext/utils/cmake")
add_subdirectory(src) # add_subdirectory(path/to/utils)
if(${ENGINE_BUILD_TESTS})
add_subdirectory(tests) # use utils cmake modules (utils_add_tests)
endif()
In first project, you can use set(CMAKE_MODULE_PATH "/path/to/the/second/project") to set CMAKE_MODULE_PATH, or else CMAKE_MODULE_PATH is empty by default.
Could you show more cmakefile's details for positioning problem?

how to split 'header', 'src', 'test' dir in cmake?(and donot add cmakelist.txt in each dir)

when building a project stucture like following:
LinearAlgebra
|---HEADER
|-----|---Linear.h
|---SRC
|-----|---Linear.cpp
|---TEST
|-----|---hello_test.cc
here is my CMakeList.txt:
cmake_minimum_required(VERSION 3.10)
project(LinearAlgebra )
set(CMAKE_CXX_STANDARD 11)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
add_library(LA_HEADER INTERFACE header/Linear.h)
add_library(LA_SRC INTERFACE src/Linear.cpp)
target_include_directories(LA_HEADER INTERFACE "${PROJECT_SOURCE_DIR}/header" "${PROJECT_SOURCE_DIR}/src")
enable_testing()
add_executable(
hello_test
test/hello_test.cc
)
target_link_libraries(
hello_test
gtest_main
LA_HEADER
LA_SRC
)
include(GoogleTest)
gtest_discover_tests(hello_test)
it works fine with cmake -S . -B build, but when it comes to cmake --build build following error occured:
Undefined symbols for architecture x86_64:
"Linear::Linear()", referenced from:
HelloTest_BasicAssertions_Test::TestBody() in hello_test.cc.o
it seems something wrong with my cmake file that didn't tell Linear.h where Linear.cpp located. So how to fix it ?? thanks for your help...
seems i misunderstand add_library, change three lines and it works out:
cmake_minimum_required(VERSION 3.10)
project(LinearAlgebra )
include_directories(header) # include_directories instead of add_library for .h files
add_library(LA_LIB src/Linear.cpp) # use add_library for .cpp files only!!!
set(CMAKE_CXX_STANDARD 11)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()
add_executable(
hello_test
test/hello_test.cc
)
target_link_libraries(
hello_test
gtest_main
LA_LIB # link LA_LIB only..
)
include(GoogleTest)
gtest_discover_tests(hello_test)
leave it here for anyone have the same requirements.

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 to add wxWidgets to project via cmake?

So I am using clion, trying to build a project with wxwidgets. But I get this error:
CMake Error at C:/Program Files/JetBrains/CLion 2020.1/bin/cmake/win/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:146 (message):
Could NOT find wxWidgets (missing: wxWidgets_LIBRARIES
wxWidgets_INCLUDE_DIRS core)
Here is my cmake file:
cmake_minimum_required(VERSION 3.16)
project(BedrockFinderCpp)
set(wxWidgets_ROOT_DIR <c:/Program Files/wxWidgets-3.1.4>)
set(wxWidgets_CONFIGURATION mswu)
find_package(wxWidgets REQUIRED COMPONENTS core)
include(${wxWidgets_USE_FILE})
find_package(OpenCL REQUIRED)
add_executable(${PROJECT_NAME}
BFFApp.cpp
BFFGui.cpp
PrecomputedRandAdvance.cpp
)
target_include_directories(${PROJECT_NAME} PRIVATE ${OpenCL_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES} ${OpenCL_LIBRARIES})
I found a "fix" by myself. It can be done with this code:
NOTE: wxWidgets is a subdirectory that contains files from the wxWidgets-3.1.4 directory
cmake_minimum_required(VERSION 3.16)
project(BedrockFinderCpp)
add_subdirectory(wxWidgets)
find_package(OpenCL REQUIRED)
add_executable(${PROJECT_NAME}
BFFApp.cpp
BFFGui.cpp
PrecomputedRandAdvance.cpp
)
target_include_directories(${PROJECT_NAME} PRIVATE ${OpenCL_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} wx::core ${OpenCL_LIBRARIES})

Magick++.h not found when trying from Cmake

Hello I am trying to use ImageMagick++ in one of my applications. I am trying to build my program from CMakeList. When I try to include Magick++.h in my main program it is showing an error saying that it cannot be found.
I tried to install like it is mentioned in the link.
Link for installation.
CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(...)
set(CMAKE_CXX_STANDARD 11)
#set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "/usr/local/lib/pkgconfig")
#set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "/usr/local/lib")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
find_package(libhpdf REQUIRED)
find_package(ImageMagick REQUIRED)
message(STATUS "${CMAKE_MODULE_PATH}")
include_directories(${LIBHPDF_INCLUDE_DIRS} src/include)
include_directories(${ImageMagick_INCLUDE_DIRS})
message(Verification "${ImageMagick_LIBRARIES} ${ImageMagick_INCLUDE_DIRS}")
set(SOURCE_FILES src/main.cpp src/base64.cpp src/base64.h)
add_executable(pdfgen ${SOURCE_FILES})
target_link_libraries(pdfgen ${LIBHPDF_LIBRARIES} ${ImageMagick_LIBRARIES})
Can someone help me to resolve this issue
This is the CMake output
Cmake output
Message for ImageMagickLibraries is null