CheckIncludeFileCXX does not find header - cmake

I'm trying to use the CheckIncludeFileCXX module to verify <gsl/gsl> is present in the system. GSL exists at /usr/local/include/gsl/gsl but generation fails at "GSL not found"
project(cpp-binaries)
cmake_minimum_required(VERSION 3.2)
include(CheckIncludeFileCXX)
CHECK_INCLUDE_FILE_CXX("gsl/gsl" GSL_LIBRARY)
if(NOT GSL_LIBRARY)
message(FATAL_ERROR "GSL not found")
endif(NOT GSL_LIBRARY)
add_executable(
cpp-binaries
"main.cpp"
)

Ok I looked more into CheckIncludeFileCXX and you basically have to specify something with CMAKE_REQUIRED_INCLUDES or else it doesn't know where to search. To give it something, it quietly searches for the include with find_path but still errors out if nothing is found.
project(gsl_t)
cmake_minimum_required(VERSION 3.2)
find_path(
gsl_location
gsl
HINTS ENV GSLDIR
)
if(gsl_location)
get_filename_component(gsl_include_dir ${gsl_location} DIRECTORY)
list(APPEND CMAKE_INCLUDE_PATH ${gsl_include_dir})
endif(gsl_location)
include(CheckIncludeFileCXX)
set(CMAKE_REQUIRED_INCLUDES ${CMAKE_INCLUDE_PATH})
CHECK_INCLUDE_FILE_CXX("gsl/gsl" gsl_found)
if(NOT gsl_found)
message(FATAL_ERROR "GSL not found. \
Try setting the GSLDIR environment variable")
endif(NOT gsl_found)
add_executable(
gsl_t
"main.cpp"
)

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?

Strange issue with variables in a config-file cmake package

We can use a cmake config file to import targets.
For example given machinary including foobarConfig.cmake.in
set(FOOBAR_VERSION #VERSION#)
#PACKAGE_INIT#
set_and_check(FOOBAR_INCLUDE_DIR "#PACKAGE_INCLUDE_INSTALL_DIR#")
set_and_check(FOOBAR_LIBRARY_DIR "#PACKAGE_LIBRARY_INSTALL_DIR#")
set_and_check(FOOBAR_LIBRARY "#PACKAGE_LIBRARY_INSTALL_DIR#/libfoobar.so")
set_and_check(FOOBAR_STATIC_LIBRARY #PACKAGE_LIBRARY_INSTALL_DIR#/libfoobar.a")
include("${CMAKE_CURRENT_LIST_DIR}/FoobarLibTargets.cmake")
message(STATUS "foobar version: ${FOOBAR_VERSION}")
message(STATUS "foobar include location: ${FOOBAR_INCLUDE_DIR}")
message(STATUS "foobar library location: ${FOOBAR_LIBRARY_DIR}")
for an exported target foobar
We can do:
find_package(foobar)
add_executable(usesfoo
usesfoo.cpp)
target_link_libraries(usesfoo
${FOOBAR_LIBRARY})
target_include_directories(usesfoo PUBLIC
${FOOBAR_INCLUDE_DIR})
and it normally just works.
However, I have a strage case where variables set in the Config.cmake are not available after find_package.
For example given:
find_package(foobar REQUIRED)
if (foobar_FOUND)
message(STATUS "found foobar")
endif()
message(STATUS "foobar include location2: ${FOOBAR_INCLUDE_DIR}")
message(STATUS "foobar library location2: ${FOOBAR_LIBRARY_DIR}")
The output is:
foobar include location: /test-import/opt/foobar/include
foobar library location: /test-import/opt/foobar/lib
found foobar
foobar include location2:
foobar library location2:
What could be going on here?
How can I:
Find this problem?
Avoid similar problems in the future?
Create these files in a safe and canonical way?
I got very confused trying to debug this and started to question how Config packages are supposed to work.
Should I be using properties of imported targets instead of variables?
What scope does find_package run in? I thought it was like an include() rather than an add_subdirectory() - which introduces its own scope.
How can these variables become unset?
What is find_package doing under the hood?
See also correctly set the location of imported cmake targets for an installed package.
That question contains code to reproduce that problem which is similar to the code for this problem.
Complete set of files to reproduce the problem:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.7)
set(VERSION 1.3.3)
project(FoobarLib VERSION "${VERSION}" LANGUAGES CXX)
SET(CMAKE_INSTALL_PREFIX "/opt/foo")
set(INSTALL_LIB_DIR lib)
add_library(foobar SHARED
foobar.cpp
)
# Create the distribution package(s)
set(CPACK_PACKAGE_VERSION ${VERSION})
set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY 0)
set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
set(CPACK_PACKAGE_NAME "foobar")
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
set(LIBRARY_INSTALL_DIR lib)
set(INCLUDE_INSTALL_DIR include)
INSTALL(TARGETS foobar
EXPORT FoobarLibTargets
LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR}
ARCHIVE DESTINATION ${LIBRARY_INSTALL_DIR}
INCLUDES DESTINATION ${INCLUDE_INSTALL_DIR})
include(CMakePackageConfigHelpers)
set(ConfigFileInstallDir lib/cmake/FoobarLib)
set(INCLUDE_INSTALL_DIR include CACHE PATH "install path for include files")
set(LIBRARY_INSTALL_DIR lib CACHE PATH "install path for libraries")
configure_package_config_file(FoobarLibConfig.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/FoobarLibConfig.cmake"
INSTALL_DESTINATION "${ConfigFileInstallDir}"
PATH_VARS INCLUDE_INSTALL_DIR LIBRARY_INSTALL_DIR
)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/FoobarLibConfigVersion.cmake"
VERSION "${VERSION}"
COMPATIBILITY SameMajorVersion)
EXPORT(EXPORT FoobarLibTargets
FILE FoobarLibTargets.cmake)
INSTALL(FILES
"${CMAKE_CURRENT_BINARY_DIR}/FoobarLibConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/FoobarLibConfigVersion.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/FoobarLibTargets.cmake"
DESTINATION "${ConfigFileInstallDir}")
include(CPack)
FoobarLibConfig.cmake.in:
set(FoobarLib_VERSION #VERSION#)
#PACKAGE_INIT#
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/FoobarLibTargets.cmake")
SET_AND_CHECK(FoobarLib_LIB_DIR "#PACKAGE_LIBRARY_INSTALL_DIR#")
message(STATUS "Foobar library version: ${FoobarLib_VERSION}")
message(STATUS "Foobar library location: ${FoobarLib_LIB_DIR}")
# workaround incorrect setting of location for import targets when package is installed
# see https://stackoverflow.com/q/56135785/1569204
#set_target_properties(foobar PROPERTIES
# IMPORTED_LOCATION_NOCONFIG "#PACKAGE_LIBRARY_INSTALL_DIR#/libfoobar.so"
# IMPORTED_LOCATION_RELEASE "#PACKAGE_LIBRARY_INSTALL_DIR#/libfoobar.so"
# IMPORTED_LOCATION_DEBUG "#PACKAGE_LIBRARY_INSTALL_DIR#/libfoobar.so")
check_required_components(FoobarLib)
run.sh:
#!/bin/sh
SRC=`pwd`
mkdir -p ./target/debug && \
cd ./target/debug &&
cmake -DCMAKE_BUILD_TYPE=Debug ../../ &&
make &&
cpack -G TGZ
cd ../..
rm -rf foo
mkdir foo
TGZ=`pwd`/target/debug/foobar-1.3.3.tar.gz
cd foo
tar -xvzf $TGZ
cat - >CMakeLists.txt <<EOF
cmake_minimum_required(VERSION 3.7)
project(useFoo VERSION 1.2.3)
find_package(FoobarLib ${MIN_FOOBARLIB_VERSION}
HINTS "${WSDIR}/opt/foo"
PATHS /opt/foo
REQUIRED)
message(STATUS "Foobar library version: ${FOOBARLIB_VERSION}")
message(STATUS "Foobar library location: ${FOOBARLIB_LIB_DIR}")
message(STATUS "FoobarLib_FOUND=${FoobarLib_FOUND}")
message(STATUS "FoobarLib_PATH=${FOOBARLIB_PATH}")
message(STATUS "FoobarLib_DIR=${FoobarLib_DIR}")
message(STATUS "FOOBARLIB_FOUND=${FoobarLib_FOUND}")
message(STATUS "FOOBARLIB_PATH=${FOOBARLIB_PATH}")
message(STATUS "FOOBARLIB_DIR=${FoobarLib_DIR}")
file(GENERATE OUTPUT foobar-loc CONTENT "<TARGET_FILE:foobar>=$<TARGET_FILE:foobar>\n")
EOF
export CMAKE_PREFIX_PATH=`pwd`/opt/foo/lib/cmake:`pwd`/opt/foo/lib/cmake/
cmake . && make VERBOSE=1
echo pwd=`pwd`
# critical - check the location of the target is relative to the installation
grep $WSDIR/opt/foo/lib/libfoobar.so foobar-loc
if [ $? -ne 0 ]; then
echo "FAIL: location of imported target 'foobar' is incorect" >&2
cat foobar-loc >&2
exit 1
fi
Here is the generated Config.cmake as requested by #havogt I don't think it helps as it is the standard generated code:
# CMake configuration file for the FoobarLib package
# Use with the find_package command in config-mode to find information about
# the FoobarLib package.
#
set(FoobarLib_VERSION 1.3.3)
####### Expanded from #PACKAGE_INIT# by configure_package_config_file() #######
####### Any changes to this file will be overwritten by the next CMake run ####
####### The input file was FoobarLibConfig.cmake.in ########
get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
macro(set_and_check _var _file)
set(${_var} "${_file}")
if(NOT EXISTS "${_file}")
message(FATAL_ERROR "File or directory ${_file} referenced by variable ${_var} does not exist !")
endif()
endmacro()
macro(check_required_components _NAME)
foreach(comp ${${_NAME}_FIND_COMPONENTS})
if(NOT ${_NAME}_${comp}_FOUND)
if(${_NAME}_FIND_REQUIRED_${comp})
set(${_NAME}_FOUND FALSE)
endif()
endif()
endforeach()
endmacro()
####################################################################################
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/FoobarLibTargets.cmake")
SET_AND_CHECK(FoobarLib_LIB_DIR "${PACKAGE_PREFIX_DIR}/lib")
message(STATUS "Foobar library version: ${FoobarLib_VERSION}")
message(STATUS "Foobar library location: ${FoobarLib_LIB_DIR}")
# workaround incorrect setting of location for import targets when package is installed
# see https://stackoverflow.com/q/56135785/1569204
#set_target_properties(foobar PROPERTIES
# IMPORTED_LOCATION_NOCONFIG "${PACKAGE_PREFIX_DIR}/lib/libfoobar.so"
# IMPORTED_LOCATION_RELEASE "${PACKAGE_PREFIX_DIR}/lib/libfoobar.so"
# IMPORTED_LOCATION_DEBUG "${PACKAGE_PREFIX_DIR}/lib/libfoobar.so")
check_required_components(FoobarLib)
'package'_FOUND is set by the implementation of find_package() not by the Config.cmake that it loads. Adding check_required_components() is good practice for other reasons (picking up that someone thinks the package is componentised when it isn't) but is not relevant to this issue.
Oops. This is embarrassing. I'd moved the generation code into a shell script and forgot to escape the variables!
cat - >CMakeLists.txt <<EOF
cmake_minimum_required(VERSION 3.7)
project(useFoo VERSION 1.2.3)
find_package(FoobarLib ${MIN_FOOBARLIB_VERSION}
HINTS "${WSDIR}/opt/foo"
PATHS /opt/foo
REQUIRED)
message(STATUS "Foobar library version: ${FOOBARLIB_VERSION}")
message(STATUS "Foobar library location: ${FOOBARLIB_LIB_DIR}")
message(STATUS "FoobarLib_FOUND=${FoobarLib_FOUND}")
message(STATUS "FoobarLib_PATH=${FOOBARLIB_PATH}")
message(STATUS "FoobarLib_DIR=${FoobarLib_DIR}")
message(STATUS "FOOBARLIB_FOUND=${FoobarLib_FOUND}")
message(STATUS "FOOBARLIB_PATH=${FOOBARLIB_PATH}")
message(STATUS "FOOBARLIB_DIR=${FoobarLib_DIR}")
file(GENERATE OUTPUT foobar-loc CONTENT "<TARGET_FILE:foobar>=$<TARGET_FILE:foobar>\n")
EOF
The question is still useful for providing source for the related question though.
To answer my own questions:
How can I find this problem?
Avoid similar problems in the future?
Create these files in a safe and canonical way?
https://en.wikipedia.org/wiki/Rubber_duck_debugging
Reduce the problem to a minimum reproducible example (preferably before posting on stack overflow)
Avoid (or at least take extra care) generating code from shell scripts
Reduce stress and get more sleep
check_required_components(Foobar) should be called at the end in the case. The docs.
check_required_components() should be called at the end
of the FooConfig.cmake file. This macro checks whether all requested,
non-optional components have been found, and if this is not the case,
sets the Foo_FOUND variable to FALSE, so that the package is
considered to be not found. It does that by testing the
Foo__FOUND variables for all requested required components.
This macro should be called even if the package doesn’t provide any
components to make sure users are not specifying components
erroneously. When using the NO_CHECK_REQUIRED_COMPONENTS_MACRO option,
this macro is not generated into the FooConfig.cmake file.

How to build a MPI program with VTK, Boost and SQLite3 library using CMake?

I would like to build a MPI program with VTK, Boost and SQLite3 library using CMake. First, I built a simple MPI program without other libraries using a CMakeLists.cmake file like this:
cmake_minimum_required(VERSION 2.6)
PROJECT(testMPI)
FIND_PATH(MPI_INTEL_C mpicc $ENV{PATH})
FIND_PATH(MPI_INTEL_CXX mpicxx $ENV{PATH})
IF(MPI_INTEL_C AND MPI_INTEL_CXX)
MESSAGE(STATUS "Intel MPI compiler is used.")
SET(CMAKE_CXX_COMPILER mpicxx)
SET(CMAKE_C_COMPILER mpicc)
ENDIF(MPI_INTEL_C AND MPI_INTEL_CXX)
SET(EXECUTABLE_OUTPUT_PATH ${testMPI_SOURCE_DIR}/bin)
SET(SRC_LIST testMPI.cpp)
ADD_EXECUTABLE(testMPI ${SRC_LIST})
FIND_PATH(MPI_H_INCLUDE mpi.h $ENV{PATH})
IF(NOT MPI_H_INCLUDE)
MESSAGE(FATAL_ERROR "mpi.h not found")
ENDIF(NOT MPI_H_INCLUDE)
INCLUDE_DIRECTORIES(${MPI_H_INCLUDE})
IF(MPI_INTEL_C AND MPI_INTEL_CXX)
SET_TARGET_PROPERTIES(testMPI PROPERTIES LINK_FLAGS -i-dynamic)
ELSE(MPI_INTEL_C AND MPI_INTEL_CXX)
FIND_LIBRARY(MPI_CXX_LIB mpichcxx $ENV{PATH})
IF(NOT MPI_CXX_LIB)
MESSAGE(FATAL_ERROR "mpich2 library not found")
ENDIF(NOT MPI_CXX_LIB)
TARGET_LINK_LIBRARIES(testMPI ${MPI_CXX_LIB})
FIND_LIBRARY(MPI_LIB mpich $ENV{PATH})
IF(NOT MPI_LIB)
MESSAGE(FATAL_ERROR "mpich2 library not found")
ENDIF(NOT MPI_LIB)
TARGET_LINK_LIBRARIES(testMPI ${MPI_LIB})
ENDIF(MPI_INTEL_C AND MPI_INTEL_CXX)
I used mpirun -np 8 -nolocal -machinefile ./machinefile ./testMPI to run the program successfully, and the returned number of processes was correct (=8).
And then, I just put some headers of VTK, Boost and SQLite3 library into this simple program (.cpp), and used the following CMakeLists.cmake to find, include and link these libraries. The program was built successfully, also I used mpirun -np 8 -nolocal -machinefile ./machinefile ./testMPI to run this program, but the returned number of processes was wrong (=1). So how to build a MPI program with other libraries using CMake? Thank you in advance!
cmake_minimum_required(VERSION 2.6)
PROJECT(testMPI)
SET(EXECUTABLE_OUTPUT_PATH ${testMPI_SOURCE_DIR}/bin)
aux_source_directory(. DIR_SRCS)
ADD_EXECUTABLE(testMPI ${DIR_SRCS})
install(TARGETS testMPI DESTINATION bin)
SET(VTK_DIR "/export/home/hh/hh/vtk/")
SET(VTK_LIBRARIES libLSDyna.so libMapReduceMPI.so libmpistubs.so libvtkalglib.so libvtkCharts.so libvtkCommon.so libvtkDICOMParser.so libvtkexoIIc.so libvtkexpat.so libvtkFiltering.so libvtkfreetype.so libvtkftgl.so libvtkGenericFiltering.so libvtkGeovis.so libvtkGraphics.so libvtkhdf5_hl.so libvtkhdf5.so libvtkHybrid.so libvtkImaging.so libvtkInfovis.so libvtkIO.so libvtkjpeg.so libvtklibxml2.so libvtkmetaio.so libvtkNetCDF_cxx.so libvtkNetCDF.so libvtkpng.so libvtkproj4.so libvtkRendering.so libvtksqlite.so libvtksys.so libvtktiff.so libvtkverdict.so libvtkViews.so libvtkVolumeRendering.so libvtkWidgets.so libvtkzlib.so)
SET(VTK_LIBRARIES_DIRS)
foreach(libs ${VTK_LIBRARIES})
set(FOUND "FOUND-NOTFOUND")
find_library(FOUND ${libs} "/export/home/hh/hh/vtk/lib/vtk-5.10")
list(APPEND VTK_LIBRARIES_DIRS ${FOUND})
endforeach(libs)
find_package(VTK REQUIRED)
IF(VTK_FOUND)
MESSAGE(STATUS "VTK IS USED")
INCLUDE(${VTK_USE_FILE})
ELSE()
MESSAGE(FATAL_ERROR "VTK IS NOT FOUND")
ENDIF(VTK_FOUND)
TARGET_LINK_LIBRARIES(testMPI ${VTK_LIBRARIES_DIRS})
LIST (APPEND CMAKE_INCLUDE_PATH "/export/home/hh/hh/boost/include")
LIST (APPEND CMAKE_LIBRARY_PATH "/export/home/hh/hh/boost/lib")
FIND_PACKAGE(Boost REQUIRED)
IF(Boost_FOUND)
MESSAGE(STATUS "Boost IS USED")
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
ELSE(Boost_FOUND)
MESSAGE(FATAL_ERROR "Boost IS NOT FOUND")
ENDIF(Boost_FOUND)
TARGET_LINK_LIBRARIES(testMPI ${Boost_LIBRARIES})
LIST(APPEND CMAKE_INCLUDE_PATH "/export/home/hh/hh/sqlite/include")
LIST(APPEND CMAKE_LIBRARY_PATH "/export/home/hh/hh/sqlite/lib")
LIST(APPEND CMAKE_MODULE_PATH "${testMPI_SOURCE_DIR}/cmake")
FIND_PACKAGE(SQLite3 REQUIRED)
IF(SQLITE3_FOUND)
MESSAGE(STATUS "SQLite3 IS USED")
INCLUDE_DIRECTORIES(${SQLITE3_INCLUDE_DIRS})
ELSE(SQLITE3_FOUND)
MESSAGE(FATAL_ERROR "SQLite3 IS NOT FOUND")
ENDIF(SQLITE3_FOUND)
TARGET_LINK_LIBRARIES(testMPI ${SQLITE3_LIBRARIES})
FIND_PACKAGE(MPI REQUIRED)
IF(MPI_FOUND)
MESSAGE(STATUS "MPI IS USED")
INCLUDE_DIRECTORIES(${MPI_INCLUDE_PATH})
ELSE(SQLITE3_FOUND)
MESSAGE(FATAL_ERROR "MPI IS NOT FOUND")
ENDIF(MPI_FOUND)
TARGET_LINK_LIBRARIES(testMPI ${MPI_LIBRARIES})

Cmake fails missing pthread.h

There was once a bug related to cmake and pthread but it is now fixed.
I use cmake version 3.5.1 on Mac OS. It compiled previously but in the meantime I installed, changed, or updated things. I don't have much knowledge about cmake. The cmake file was written by someone else.
This is the error message I get:
-- Looking for pthread.h
-- Looking for pthread.h - not found
CMake Error at /usr/local/Cellar/cmake/3.5.1/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
Could NOT find Threads (missing: Threads_FOUND)
Call Stack (most recent call first):
/usr/local/Cellar/cmake/3.5.1/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:388 (_FPHSA_FAILURE_MESSAGE)
/usr/local/Cellar/cmake/3.5.1/share/cmake/Modules/FindThreads.cmake:223 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
external/glfw-3.0.3/CMakeLists.txt:74 (find_package)
My cmake file (if relevant).
_minimum_required(VERSION 2.8 FATAL_ERROR)
cmake_policy(VERSION 2.8)
set(FENSTERCHEN fensterchen)
set(FRAMEWORK_NAME framework)
project(${FENSTERCHEN})
if(CMAKE_CURRENT_SOURCE_DIR EQUAL CMAKE_CURRENT_BINARY_DIR)
message(FATAL_ERROR "Source and binary directories must be different")
endif(CMAKE_CURRENT_SOURCE_DIR EQUAL CMAKE_CURRENT_BINARY_DIR)
if(MSVC)
set(BINARY_DIRECTORY build)
endif()
if(("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") OR ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") OR (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel") AND UNIX))
set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++0x")
set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
set(CMAKE_CXX_FLAGS "-std=c++0x -fopenmp")
set(CMAKE_C_FLAGS "-fopenmp")
endif()
set(CMAKE_CXX_FLAGS "-std=c++0x -fopenmp")
set(CMAKE_C_FLAGS "-fopenmp")
option(FENSTERCHEN_IN_SOURCE_BUILD "FENSTERCHEN_IN_SOURCE_BUILD" OFF)
if(FENSTERCHEN_IN_SOURCE_BUILD)
add_definitions(-DFENSTERCHEN_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
add_definitions(-DFENSTERCHEN_BINARY_DIR="../")
else()
add_definitions(-DFENSTERCHEN_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
add_definitions(-DFENSTERCHEN_BINARY_DIR="${CMAKE_CURRENT_BINARY_DIR}")
endif()
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/external)
add_definitions(-DGLEW_STATIC)
set(GLFW_DIRECTORY glfw-3.0.3)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/external/${GLFW_DIRECTORY}/include)
set(GLFW_INSTALL OFF CACHE STRING "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE STRING "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE STRING "" FORCE)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/external/${GLFW_DIRECTORY})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/external/${GLFW_DIRECTORY}/include)
set(GLM_DIRECTORY glm-0.9.5.3)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/external/${GLM_DIRECTORY})
################################
# Add libraries to executables
set(BINARY_FILES glfw ${GLFW_LIBRARIES} ${FREEIMAGE_LIBRARY})
################################
# Add output directory
if(MSVC)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/build/)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(COPY_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/build/Debug/ )
else(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(COPY_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/build/Release/ )
endif(CMAKE_BUILD_TYPE STREQUAL "Debug")
else(MSVC)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/build/Debug )
else(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/build/Release )
endif(CMAKE_BUILD_TYPE STREQUAL "Debug")
endif(MSVC)
option(FENSTERCHEN_AUTOMATED_TESTS "FENSTERCHEN_AUTOMATED_TESTS" OFF)
if(FENSTERCHEN_AUTOMATED_TESTS)
add_definitions(-DAUTOMATED_TESTS)
endif()
if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
if (NOT APPLE)
add_definitions(-fpermissive)
endif()
endif()
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif(MSVC)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/framework)
add_subdirectory(framework)
add_subdirectory(source)
set (FENSTERCHEN_TESTS "false" CACHE BOOL "Set to enable testing.")
if (FENSTERCHEN_TESTS)
set(UNITTEST_PP_DIRECTORY unittest-cpp)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/external/${UNITTEST_PP_DIRECTORY}/UnitTest++)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/external/${UNITTEST_PP_DIRECTORY})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/external/${UNITTEST_PP_DIRECTORY}/UnitTest++)
#set(BINARY_FILES UnitTest++ glfw ${GLFW_LIBRARIES} ${FREEIMAGE_LIBRARY})
add_subdirectory(tests)
enable_testing()
add_test( NAME testFensterchen COMMAND runTests )
endif (FENSTERCHEN_TESTS)
install (DIRECTORY data DESTINATION .)
# See http://www.vtk.org/Wiki/CMake:CPackPackageGenerators
set (CPACK_PACKAGE_VERSION_MAJOR "4")
set (CPACK_PACKAGE_VERSION_MINOR "4")
set (CPACK_PACKAGE_VERSION_PATCH "2")
# Use current date in YYYYMMDD format as patch number
# cpack mistakenly detects Mingw-w64 as win32
if (MINGW)
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
set (CPACK_SYSTEM_NAME win64)
endif ()
endif ()
if (WIN32)
set (CPACK_GENERATOR "ZIP")
else ()
set (CPACK_GENERATOR "TBZ2")
endif ()
include(CPack)
How do I fix this?
Thanks to #Tsyvarev.
The problem was that the "-fopenmp" flag was causing an error because the compiler was set to clang.
From the CMakeFiles/CMakeError.log
clang: error: unsupported option '-fopenmp'
Changing the C++ compiler to g++ and removing the flag from the c compiler solved the problem.

cmake ignore exact option in findPackage for protobuf

I have a problem with cmake
I'm writting in CMakeLists
set(PROTOBUF_VERSION "2.4.1")
find_package(Protobuf ${PROTOBUF_VERSION} EXACT REQUIRED)
But when I run cmake on my machine with protobuf 2.5.0, it successfully generates makefile.
In stdout I have only:
-- Found PROTOBUF: /usr/local/lib/libprotobuf.so
But for ZLIB I have
-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.7")
Probably, protobuf doesn't contain a version of itself in the library.
Is there a way to specify protobufer's version?
I'm not sure whether the fault is in protobuf, or in the CMake module, but you have a couple of choices here.
If the find_package call succeeds, you should have access to both the protobuf include path, and to the protoc compiler. You can either read in the contents of ${PROTOBUF_INCLUDE_DIRS}/google/protobuf/stubs/common.h and do a regex search for #define GOOGLE_PROTOBUF_VERSION, or you can invoke protoc --version and search the output for the correct version.
So, for option 1, you can do:
find_package(Protobuf ${PROTOBUF_VERSION} REQUIRED)
if(NOT EXISTS "${PROTOBUF_INCLUDE_DIRS}/google/protobuf/stubs/common.h")
message(FATAL_ERROR "Failed to find protobuf headers")
endif()
file(STRINGS "${PROTOBUF_INCLUDE_DIRS}/google/protobuf/stubs/common.h" Found
REGEX "#define GOOGLE_PROTOBUF_VERSION 2004001")
if(NOT Found)
message(FATAL_ERROR "Didn't find v2.4.1 of protobuf")
endif()
or for option 2:
find_package(Protobuf ${PROTOBUF_VERSION} REQUIRED)
if(NOT PROTOBUF_PROTOC_EXECUTABLE)
message(FATAL_ERROR "Failed to find protoc")
endif()
execute_process(COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --version
OUTPUT_VARIABLE VersionInfo)
string(FIND "${VersionInfo}" "2.4.1" Found)
if(Found LESS 0)
message(FATAL_ERROR "Didn't find v2.4.1 of protobuf")
endif()