USING_HDF5_CMake.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. ************************************************************************
  2. * Build and Install HDF5 Applications with CMake *
  3. ************************************************************************
  4. Notes: This short instruction is written for users who want to quickly
  5. build HDF5 applications using the CMake tools. Users can adapt
  6. these instructions for their own applications. For more information,
  7. see the "Minimum C Project Files for CMake" section.
  8. More information about using CMake can be found at the KitWare
  9. site, www.cmake.org.
  10. CMake uses the command line; however, the visual CMake tool is
  11. available for the configuration step. The steps are similar for
  12. all of the operating systems supported by CMake.
  13. NOTES:
  14. 1. Using CMake for building and using HDF5 is under active
  15. development. While we have attempted to provide error-free
  16. files, please understand that development with CMake has not
  17. been extensively tested outside of HDF. The CMake specific
  18. files may change before the next release.
  19. 2. CMake for HDF5 development should be usable on any system
  20. where CMake is supported. Please send us any comments on how
  21. CMake support can be improved on any system.
  22. 3. See the appendix at the bottom of this file for an example
  23. of using a ctest script for building and testing. See
  24. INSTALL_CMake.txt for more information.
  25. 4. See https://cmake.org/cmake/help/latest/command/find_package.html
  26. for more information on the CMake "Config Mode Search Procedure".
  27. ========================================================================
  28. I. Preconditions
  29. ========================================================================
  30. 1. We suggest you obtain the latest CMake for your platform from the Kitware
  31. web site. The HDF5 1.13.x product requires a minimum CMake version
  32. of 3.12. If you are using VS2019, the minimum version is 3.15.
  33. 2. You have installed the HDF5 library built with CMake, by executing
  34. the HDF Install Utility (the *.msi file in the binary package for
  35. Windows or the *.sh on Linux). You can obtain pre-built binaries
  36. from The HDF Group's website at www.hdfgroup.org.
  37. 3. Set the HDF5_ROOT CMake variable, -DHDF5_ROOT=<install_path>
  38. or environment variable, set(ENV{HDF5_ROOT} "<install_path>")
  39. to the installed location of HDF5.
  40. On Windows:
  41. HDF5_ROOT=C:/Program Files/HDF_Group/HDF5/1.13.x/
  42. On unix:
  43. HDF5_ROOT=<install root folder>/HDF_Group/HDF5/1.13.x/
  44. If you are using shared libraries, you may need to add to the path
  45. environment variable. Set the path environment variable to the
  46. installed location of the library files for HDF5.
  47. On Windows (*.dll):
  48. PATH=%PATH%;C:/Program Files/HDF_Group/HDF5/1.13.x/bin
  49. On unix (*.so):
  50. LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<install root folder>/HDF_Group/HDF5/1.13.x/lib
  51. If you are using filter plugin libraries, you will need to set the
  52. HDF5_PLUGIN_PATH environment variable.
  53. On Windows:
  54. HDF5_PLUGIN_PATH=C:/Program Files/HDF_Group/HDF5/1.13.x/lib/plugin
  55. On unix:
  56. HDF5_PLUGIN_PATH=<install root folder>/HDF_Group/HDF5/1.13.x/lib/plugin
  57. (Note there are no quote characters used on Windows and all platforms
  58. use forward slashes)
  59. 4. Created separate source and build directories.
  60. (CMake commands are executed in the build directory)
  61. 5. Created a CMakeLists.txt file(s) for your source. See Section III
  62. below.
  63. ========================================================================
  64. II. Building HDF5 Applications with CMake
  65. ========================================================================
  66. Go through these steps to build HDF5 applications with CMake.
  67. (The application must support building with CMake.)
  68. 1. Run CMake
  69. 2. Configure the cache settings
  70. 3. Build HDF5 Applications
  71. 4. Test HDF5 Applications
  72. These steps are described in more detail below.
  73. 1. Run CMake
  74. The visual CMake executable is named "cmake-gui.exe" on Windows and should be
  75. available in your Start menu. For Linux, UNIX, and Mac users the
  76. executable is named "cmake-gui" and can be found where CMake was
  77. installed.
  78. Specify the source and build directories. Make the build and source
  79. directories different. For example on Windows, if the source is at
  80. c:\MyHDFstuff\hdf5, then use c:\MyHDFstuff\hdf5\build or
  81. c:\MyHDFstuff\build\hdf5 for the build directory.
  82. PREFERRED:
  83. Users can perform the configuration step without using the visual
  84. cmake-gui program. The following is an example command line
  85. configuration step executed within the build directory:
  86. cmake -G "<generator>" [-D<options>] <sourcepath>
  87. Where <generator> is
  88. * MinGW Makefiles
  89. * NMake Makefiles
  90. * Unix Makefiles
  91. * Visual Studio 14 2015
  92. * Visual Studio 14 2015 Win64
  93. * Visual Studio 15 2017
  94. * Visual Studio 15 2017 Win64
  95. * Visual Studio 16 2019
  96. * ... in addition VS2019 will need to set the "-A" option,
  97. * ... [Win32, x64, ARM, ARM64]
  98. <options> is:
  99. * BUILD_TESTING:BOOL=ON
  100. * BUILD_SHARED_LIBS:BOOL=[ON | OFF]
  101. 2. Configure the cache settings
  102. 2.1 Visual CMake users, click the Configure button. If this is the first time you are
  103. running cmake-gui in this directory, you will be prompted for the
  104. generator you wish to use (for example on Windows, Visual Studio 14 2015 Win64).
  105. CMake will read in the CMakeLists.txt files from the source directory and
  106. display options for the HDF5 project. After the first configure you
  107. can adjust the cache settings and/or specify locations of other programs.
  108. Any conflicts or new values will be highlighted by the configure
  109. process in red. Once you are happy with all the settings and there are no
  110. more values in red, click the Generate button to produce the appropriate
  111. build files.
  112. On Windows, if you are using a Visual Studio generator, the solution and
  113. project files will be created in the build folder.
  114. On linux, if you are using the Unix Makefiles generator, the Makefiles will
  115. be created in the build folder.
  116. 2.2 Alternative command line example on Windows in c:\MyHDFstuff\hdf5\build directory:
  117. cmake -G "Visual Studio 14 2015 Win64" -DBUILD_TESTING:BOOL=ON ..
  118. 3. Build HDF5 Applications
  119. On Windows, you can build HDF5 applications using either the Visual Studio Environment
  120. or the command line. The command line is normally used on linux, Unix, and Mac.
  121. To build from the command line, navigate to your build directory and
  122. execute the following:
  123. cmake --build . --config {Debug | Release}
  124. NOTE: "--config {Debug | Release}" may be optional on your platform. We
  125. recommend choosing either Debug or Release on Windows. If you are
  126. using the pre-built binaries from HDF, use Release.
  127. 3.1 If you wish to use the Visual Studio environment, open the solution
  128. file in your build directory. Be sure to select either Debug or
  129. Release and build the solution.
  130. 4. Test HDF5 Applications
  131. To test the build, navigate to your build directory and execute:
  132. ctest . -C {Debug | Release}
  133. NOTE: "-C {Debug | Release}" may be optional on your platform. We
  134. recommend choosing either Debug or Release to match the build
  135. step on Windows.
  136. 5. The files that support building with CMake are all of the files in the
  137. config/cmake folder, the CMakeLists.txt files in each source folder, and
  138. CTestConfig.cmake. CTestConfig.cmake is specific to the internal testing
  139. performed by The HDF Group. It should be altered for the user's
  140. installation and needs. The cacheinit.cmake file settings are used by
  141. The HDF Group for daily testing. It should be altered/ignored for the user's
  142. installation and needs.
  143. ========================================================================
  144. III. Minimum C Project Files for CMake
  145. ========================================================================
  146. Given the preconditions in section I, create a CMakeLists.txt file at the
  147. source root. Include the following text in the file:
  148. ##########################################################
  149. cmake_minimum_required (VERSION 3.12)
  150. project (HDF5MyApp C CXX)
  151. set (LIB_TYPE STATIC) # or SHARED
  152. string(TOLOWER ${LIB_TYPE} SEARCH_TYPE)
  153. find_package (HDF5 NAMES hdf5 COMPONENTS C ${SEARCH_TYPE})
  154. # find_package (HDF5) # Find non-cmake built HDF5
  155. set_directory_properties(PROPERTIES INCLUDE_DIRECTORIES "${HDF5_INCLUDE_DIR}")
  156. set (LINK_LIBS ${LINK_LIBS} ${HDF5_C_${LIB_TYPE}_LIBRARY})
  157. set (example hdf_example)
  158. add_executable (${example} ${PROJECT_SOURCE_DIR}/${example}.c)
  159. TARGET_C_PROPERTIES (${example} PRIVATE ${LIB_TYPE})
  160. target_link_libraries (${example} ${LINK_LIBS})
  161. enable_testing ()
  162. include (CTest)
  163. add_test (NAME test_example COMMAND ${example})
  164. ##########################################################
  165. ========================================================================
  166. IV. APPENDIX
  167. ========================================================================
  168. Below is an example of a ctest script that can be used to build the examples.
  169. Adjust the values as necessary. Note that the defaults can be entered on the
  170. command line and the build folder is created as a sub-folder. Windows should
  171. adjust the forward slash to double backslashes, except for the HDF_DIR
  172. environment variable.
  173. NOTE: this file is available at the HDF web site:
  174. https://portal.hdfgroup.org/display/support/Building+HDF5+with+CMake
  175. HDF5_Examples.cmake
  176. HDF5_Examples_options.cmake
  177. Also available at the HDF web site is a CMake application framework template.
  178. You can quickly add files to the framework and execute the script to compile
  179. your application with an installed HDF5 binary.
  180. ========================================================================
  181. For further assistance, send email to help@hdfgroup.org
  182. ========================================================================