Using HDF5 on Windows 10: Could NOT find HDF5 (missing: HDF5_DIR) - cmake

I was successfully able to use HDF5 in my C++ project on MacOS and Linux.
Currently I am trying to get it to work on Windows 10, but I get the following CMake output:
-- Could NOT find HDF5 (missing: HDF5_DIR)
-- Configuring done
-- Generating done
-- Build files have been written to: /cygdrive/z/hdf5parser/cmake-build-debug
I installed the Pre-built Binary Distribution of HDF5 on Windows and set the environment variable HDF5_DIR to C:/Program Files/HDF_Group/HDF5/1.10.5/cmake.
According to the official documentation I use the following CMake configuration for my project:
cmake_minimum_required (VERSION 3.10)
project (HDF5MyApp C CXX)
set (LIB_TYPE STATIC) # or SHARED
string(TOLOWER ${LIB_TYPE} SEARCH_TYPE)
find_package (HDF5 NAMES hdf5 COMPONENTS C ${SEARCH_TYPE})
set_directory_properties(PROPERTIES INCLUDE_DIRECTORIES "${HDF5_INCLUDE_DIR}")
set (LINK_LIBS ${LINK_LIBS} ${HDF5_C_${LIB_TYPE}_LIBRARY})
set (example HdfHandler)
add_executable(...) # I skipped this part
target_link_libraries(hdf5parser PRIVATE ${LINK_LIBS})
Had somebody similar problems or can give me a hint to use HDF5 on Windows?

Related

Not able to link HDF5 path in CMake configuration

I'm trying to create make files of C++ code on a server doing
cmake ../
and I get the error
-- Could NOT find HDF5 (missing: HDF5_INCLUDE_DIRS) (found version "1.10.7")
CMake Error at src/CMakeLists.txt:180 (message):
HDF5 support was requested, but no HDF5 library was found on this system
I have installed HDF5 in /home/directory/ so I have HDF5 include and library files in
/home/directory/include
/home/directory/lib
I've tried to add in CMakeLists.txt the following lines:
set(HDF5_LIBRARIES "/home/directory/")
set(HDF5_CXX_LIBRARIES "/home/directory/")
set(HDF5_INCLUDE_DIRS "/home/directory/")
set(HDF5_CXX_INCLUDE_DIRS "/home/directory/")
But still getting the error.
I've also tried to export HDF5_ROOT=/home/directory/ without success.
The error come from the following lines in the code:
if (HDF5_FOUND)
target_include_directories(soft PUBLIC ${HDF5_INCLUDE_DIRS})
target_link_libraries(soft PUBLIC ${HDF5_LIBRARIES})
else (HDF5_FOUND)
message(FATAL_ERROR "HDF5 support was requested, but no HDF5 library was found on this system")
endif (HDF5_FOUND)
Any help?
Using CMake 3.19.3 and hdf5-1.10.7

Why my <package>-config.cmake have <package>_include_dir and <package>_librairies empty

I am trying to make a cross-platform CMake for my project (Windows and Linux).
I need to use external libraries (yaml-cpp). On Linux, I just had to do an apt get and use find_package. But on Windows, I need to append the CMAKE_MODULE_PATH in order for my program to find the yaml-cpp-config.cmake.
So I start by installing yaml-cpp (https://github.com/jbeder/yaml-cpp) with CMake GUI 3.16 and mingw32 (mingw32-make install).
I have tried the library on a hello world project, and it works fine.
cmake_minimum_required(VERSION 3.1)
project (yaml_test)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
message (STATUS "Yaml-cpp include = $ENV{YAML_CPP_INCLUDE_DIR}")
message (STATUS "Yaml-cpp library = $ENV{YAML_CPP_LIBRARIES}")
include_directories ($ENV{YAML_CPP_INCLUDE_DIR})
add_executable(yaml_test main.cpp)
target_link_libraries(yaml_test $ENV{YAML_CPP_LIBRARIES})
But now, I want to include the library in my project and use find_package. But the yaml-cpp-config.cmake looks like this:
# - Config file for the yaml-cpp package
# It defines the following variables
# YAML_CPP_INCLUDE_DIR - include directory
# YAML_CPP_LIBRARIES - libraries to link against
# Compute paths
get_filename_component(YAML_CPP_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(YAML_CPP_INCLUDE_DIR "")
# Our library dependencies (contains definitions for IMPORTED targets)
include("${YAML_CPP_CMAKE_DIR}/yaml-cpp-targets.cmake")
# These are IMPORTED targets created by yaml-cpp-targets.cmake
set(YAML_CPP_LIBRARIES "")
The YAML_CPP_INCLUDE_DIR and YAML_CPP_LIBRARIES variables are empty, and even if CMake found yaml-cpp-config.cmake, It doesn't work. So what do I have missing in the installation of yaml-cpp? Should I have set the paths by hand?
The absence of definition of YAML_CPP_INCLUDE_DIR and YAML_CPP_LIBRARIES variables is the issue with the yaml-cpp project which is already reported here.
Instead of variables described in this config file, use target yaml-cpp:
add_executable(yaml_test main.cpp)
# This provides both include directories and libraries.
target_link_libraries(yaml_test yaml-cpp)
Linking with an IMPORTED target (yaml-cpp in this case) is known as CMake "modern way".

CMake find protobuf compiled from source

I am trying to build a project that depends on Google Protocol Buffers compiled from source. My project should be platform independent and also should support cross-compilation, which is the reason that i prefer to use a locally built protobuf. However I would prefer not to include the whole library as a subproject as it would take too much to build.
My simplified CMakeLists.txt is:
cmake_minimum_required(VERSION 3.5)
project(sample-protobuf)
# find a boost install with the libraries filesystem and system
find_package(Protobuf REQUIRED)
set(SOURCES
main.cpp
)
add_executable(sample
${SOURCES}
)
target_link_libraries(sample
PRIVATE
protobuf::libprotobuf
)
I invoke CMake on Linux as:
cmake -DCMAKE_PREFIX_PATH=/path/to/built/protobuf/ ..
but it does not find the library and I get the following message:
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.
On Windows this procedure works.
I built the library on Linux using the suggested approach, which is not with CMake but with autotools.
What should I do differently?
cd protobuf/cmake
mkdir build
cd build
cmake....
make...
sudo make install

Errors while making tdlib example

l am trying to build a java example for the td lib following the README(https://github.com/tdlib/td/tree/master/example/java)
I have got following mistackes. Please tell how can I fix it?
C:\Users\irina\td\jnibuild>cmake -DCMAKE_BUILD_TYPE=Debug -DTD_ENABLE_JNI=ON -DCMAKE_INSTALL_PREFIX:PATH=../example/java/td ..
-- Could NOT find ccache
-- Found OpenSSL: C:/OpenSSL-Win32/include optimized;C:/OpenSSL-Win32/lib/VC/ssleay32MD.lib;debug;C:/OpenSSL-Win32/lib/VC/ssleay32MDd.lib;optimized;C:/OpenSSL-Win32/lib/VC/libeay32MD.lib;debug;C:/OpenSSL-Win32/lib/VC/libeay32MDd.lib
-- Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
-- Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
CMake Warning at CMakeLists.txt:256 (message):
Not found zlib: skip TDLib, tdactor, tdnet, tddb
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
ZLIB_LIBRARY
linked by target "tdutils" in directory C:/Users/irina/td/tdutils
-- Configuring incomplete, errors occurred!
See also "C:/Users/irina/td/jnibuild/CMakeFiles/CMakeOutput.log".
See also "C:/Users/irina/td/jnibuild/CMakeFiles/CMakeError.log".
ZLIB for Windows is a part of the GnuWin32 project (I'm not sure whether it is allowed to give links on SO). As I see, the CMakeLists.txt uses find_package to lookup the ZLIB library:
if (NOT ZLIB_FOUND)
find_package(ZLIB)
endif()
if (NOT ZLIB_FOUND)
message(WARNING "Not found zlib: skip TDLib, tdactor, tdnet, tddb")
return()
endif()
How the find_package command works is well described in the official documentation:
The command has two modes by which it searches for packages: “Module” mode and
“Config” mode. Module mode is available when the command is invoked with the
above reduced signature. CMake searches for a file called Find<package>.cmake
in the CMAKE_MODULE_PATH followed by the CMake installation. If the file is
found, it is read and processed by CMake. It is responsible for finding the
package, checking the version, and producing any needed messages. Many find-
modules provide limited or no support for versioning; check the module
documentation. If no module is found and the MODULE option is not given the
command proceeds to Config mode.
I've had a look into FindZLIB.cmake on my Windows machine. The module uses the following path: ZLIB_ROOT and the following registry keys:
"[HKEY_LOCAL_MACHINE\SOFTWARE\GnuWin32\Zlib;InstallPath]"
"$ENV{PROGRAMFILES}/zlib
So, as I understand, if you install GnuWin32 using the installer, the HKLM key will be written down into the registry and CMake will be able to find the path to ZLIB. If you wish to use just the zip-archive, the ZLIB_ROOT parameter must be correctly specified when you run CMake:
cmake -DZLIB_ROOT=<PATH-to-your-unpacked-zlib> -D.....

Using HDF5 Libraries with CMake on Windows (Error: "Could NOT find HDF5")

I want to use the HDF5 libraries in my C++ program. I am using the VS 2010 x64 compiler and CMake 3.8.0rc2 on Windows 7. The HDF5 version I installed is 1.8.10 (installed by running the official "Installer").
In my CMakeLists file, I added the following lines:
FIND_PACKAGE ( HDF5 REQUIRED )
INCLUDE_DIRECTORIES (${HDF5_INCLUDE_DIRS})
SET (HDF5_LIBS ${HDF5_LIBS} ${HDF5_LIBRARIES})
...
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES} ${HDF5_LIBS})
CMake shows the following error message upon Configuring:
Could NOT find HDF5 (missing: HDF5_LIBRARIES)
I also added the environment variables HDF5_DIR and HDF5_ROOT which both point to my HDF5 installation folder C:\Develop\HDF5\1.8.10.
What am I missing to have CMake recognize the HDF5 installation?
hdf5 can now be installed on Windows via vcpkg (https://github.com/Microsoft/vcpkg)
Jean
I was using an outdated HDF5 version; the current version is HDF5-1.8.18.
Unfortunately, the VS 2010 x64 generator is missing in the CMake-hdf5-1.8.18 archive. There are only .bat files for VS 2012, 2013 and 2015. It is possible to add other generators though:
Download CMake archive from the HDF5 download website, i.e. from this website. Make sure to take the version you want to install.
Open file HDF5config.cmake
Search for CTEST_CMAKE_GENERATOR
Add another elseif for your desired generator, i.e. for Visual Studio 2010 x64:
elseif(${BUILD_GENERATOR} STREQUAL "VS201064")
set(CTEST_CMAKE_GENERATOR "Visual Studio 10 2010 Win64")
Create another .bat file, i.e. build-VS2010-64.bat
Replace the BUILD_GENERATOR value with the one that you chose in the HDF5config.cmake file, i.e. ctest -S HDF5config.cmake,BUILD_GENERATOR=VS201064 -C Release -V -O hdf5.log
Run the .bat file
The built HDF5 installation will be available in the folder .\build\_CPack_Packages\win64
After that I changed the CMakeLists lines shown in the original question, as shown in the USING_HDF5_CMake.txt thats created while compilation. Note that I changed C to CXX in the component list because I have C++ project.
set (LIB_TYPE STATIC) # or SHARED
string(TOLOWER ${LIB_TYPE} SEARCH_TYPE)
find_package (HDF5 NAMES hdf5 COMPONENTS CXX ${SEARCH_TYPE})
# find_package (HDF5) # Find non-cmake built HDF5
INCLUDE_DIRECTORIES (${HDF5_INCLUDE_DIR})
set (LINK_LIBS ${LINK_LIBS} ${HDF5_CXX_${LIB_TYPE}_LIBRARY})