Hello all, I’m attempting to use CMakeLists to build the HDF5 library in a manner that will allow me to allow multiple threads of my program to write to their own specific .h5 files. Each H5 file is only ever accessed by its specific thread, so I believe this should be thread safe. I’m new to CMakeLists, and I’d appreciate if anyone can give me assistance on how to use it to build HDF5 for multithreading use.
See below my CMakeList code with the debugging and error messages that I get when building from local installation and attempting to build from source.
===
cmake_minimum_required(VERSION 3.12)
set(CMAKE_VERBOSE_MAKEFILE ON)
include(FetchContent)
project(farfield)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(RAYLIB_VERSION 5.0)
set(raylib_VERBOSE 1)
find_package(raylib ${RAYLIB_VERSION} QUIET) # QUIET or REQUIRED
if (NOT raylib_FOUND) # If there’s none, fetch and build raylib
FetchContent_Declare(
raylib
DOWNLOAD_EXTRACT_TIMESTAMP OFF
URL https://github.com/raysan5/raylib/archive/refs/tags/${RAYLIB_VERSION}.tar.gz
)
FetchContent_GetProperties(raylib)
if (NOT raylib_POPULATED) # Have we downloaded raylib yet?
set(FETCHCONTENT_QUIET NO)
FetchContent_Populate(raylib)
set(BUILD_EXAMPLES OFF CACHE BOOL “” FORCE) # don’t build the supplied examples
set(BUILD_TESTING OFF CACHE BOOL “” FORCE)
add_subdirectory(${raylib_SOURCE_DIR} ${raylib_BINARY_DIR})
endif()
endif()
Select HDF5 build strategy
set(HDF5_BUILD_FROM_SOURCE ON CACHE BOOL “” FORCE)
HDF5 configuration flags
set(BUILD_TESTING OFF CACHE BOOL “” FORCE)
set(BUILD_SHARED_LIBS ON CACHE BOOL “Build shared libraries” FORCE)
set(BUILD_STATIC_LIBS OFF CACHE BOOL “Build static libraries” FORCE)
set(HDF5_ENABLE_THREADSAFE ON CACHE BOOL “” FORCE)
set(HDF5_BUILD_HL_LIB OFF CACHE BOOL “” FORCE) # Disable high-level APIs for thread safety
set(HDF5_BUILD_EXAMPLES OFF CACHE BOOL “” FORCE) # Disable examples
set(HDF5_BUILD_TOOLS OFF CACHE BOOL “” FORCE) # Disable tools
HDF5 build
if (HDF5_BUILD_FROM_SOURCE)
FetchContent_Declare(
hdf5
GIT_REPOSITORY GitHub - HDFGroup/hdf5: Official HDF5® Library Repository
GIT_TAG hdf5_1.14.5
)
FetchContent_MakeAvailable(hdf5)
set (LINK_LIBS ${LINK_LIBS} hdf5-shared)
set (HDF5_INCLUDE_DIRS ${hdf5_BINARY_DIR}/src)
else()
# Load pre-installed HDF5 package
set (LIB_TYPE SHARED) # STATIC or SHARED
string(TOLOWER ${LIB_TYPE} SEARCH_TYPE)
find_package(HDF5 NAMES hdf5 COMPONENTS C ${SEARCH_TYPE})
set (LINK_LIBS ${LINK_LIBS} ${HDF5_C_${LIB_TYPE}_LIBRARY})
endif()
HDF5 package information
message(STATUS “hdf5_POPULATED: ${hdf5_POPULATED}”)
message(STATUS “hdf5_BINARY_DIR: ${hdf5_BINARY_DIR}”)
message(STATUS “hdf5_SOURCE_DIR: ${hdf5_SOURCE_DIR}”)
message(STATUS “HDF5 Found: ${HDF5_FOUND}”)
message(STATUS “HDF5_VERSION: ${HDF5_VERSION}”)
message(STATUS “HDF5_INCLUDE_DIRS: ${HDF5_INCLUDE_DIRS}”)
message(STATUS “HDF5_LIBRARIES: ${HDF5_LIBRARIES}”)
message(STATUS “HDF5_DEFINITIONS: ${HDF5_DEFINITIONS}”)
message(STATUS “HDF5 include: ${HDF5_INCLUDE_DIR}”)
message(STATUS “HDF5 library: ${LINK_LIBS}”)
Other build code
file(GLOB_RECURSE SOURCES “src/*.cpp”)
message(STATUS “Cpp file sources:”)
foreach(SOURCE ${SOURCES})
message(STATUS “- ${SOURCE}”)
endforeach()
add_executable(${PROJECT_NAME} ${SOURCES})
add_subdirectory(src)
set_target_properties(${PROJECT_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${PROJECT_NAME})
set_property(TARGET ${PROJECT_NAME} PROPERTY VS_DEBUGGER_WORKING_DIRECTORY $<TARGET_FILE_DIR:${PROJECT_NAME}>)
if (“${PLATFORM}” STREQUAL “Web”)
add_custom_command(
TARGET ${PROJECT_NAME} PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/src/resources $<TARGET_FILE_DIR:${PROJECT_NAME}>/…/resources
)
#DEPENDS ${PROJECT_NAME}
else()
add_custom_command(
TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/src/resources $<TARGET_FILE_DIR:${PROJECT_NAME}>/resources
)
#DEPENDS ${PROJECT_NAME}
endif()
Linking and include directories
target_link_libraries(${PROJECT_NAME} raylib)
target_link_libraries (${PROJECT_NAME} ${LINK_LIBS}) # Link HDF5 libraries
target_include_directories(${PROJECT_NAME} PRIVATE ${HDF5_INCLUDE_DIRS}) # Include HDF5 headers
if (“${PLATFORM}” STREQUAL “Web”)
# Tell Emscripten to build an example.html file.
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX “.html”)
target_link_options(${PROJECT_NAME} PUBLIC -sUSE_GLFW=3 PUBLIC --preload-file resources)
endif()
if (APPLE)
target_link_libraries(${PROJECT_NAME} “-framework IOKit”)
target_link_libraries(${PROJECT_NAME} “-framework Cocoa”)
target_link_libraries(${PROJECT_NAME} “-framework OpenGL”)
endif()
===
BUILDING FROM LOCAL INSTALLATION - BUILDS AND RUNS, BUT CANNOT MULTITHREAD
When I set HDF5_BUILD_FROM_SOURCE to OFF, then it uses the local installation on my Windows machine, built using the .msi installer from HDF5 website. The program launches successfully, and all single threaded H5DF functions work normally, however when I try the same commands in a second thread, I get errors like this:
HDF5-DIAG: Error detected in HDF5 (1.14.5):
#000: D:\a\hdf5\hdf5\hdf5-1.14.5\src\H5D.c line 186 in H5Dcreate2(): unable to synchronously create dataset
major: Dataset
minor: Unable to create file
#001: D:\a\hdf5\hdf5\hdf5-1.14.5\src\H5D.c line 135 in H5D__create_api_common(): unable to create dataset
major: Dataset
minor: Unable to create file
#002: D:\a\hdf5\hdf5\hdf5-1.14.5\src\H5VLcallback.c line 1870 in H5VL_dataset_create(): can’t reset VOL wrapper info
major: Virtual Object Layer
minor: Can’t reset object
#003: D:\a\hdf5\hdf5\hdf5-1.14.5\src\H5VLint.c line 2406 in H5VL_reset_vol_wrapper(): no VOL object wrap context?
major: Virtual Object Layer
minor: Bad value
#004: D:\a\hdf5\hdf5\hdf5-1.14.5\src\H5VLcallback.c line 1865 in H5VL_dataset_create(): dataset create failed
major: Virtual Object Layer
minor: Unable to create file
#005: D:\a\hdf5\hdf5\hdf5-1.14.5\src\H5VLcallback.c line 1830 in H5VL__dataset_create(): dataset create failed
major: Virtual Object Layer
Here’s the output from the CMake build
[cmake] – hdf5_POPULATED:
[cmake] – hdf5_BINARY_DIR:
[cmake] – hdf5_SOURCE_DIR:
[cmake] – HDF5 Found: 1
[cmake] – HDF5_VERSION: 1.14.5
[cmake] – HDF5_INCLUDE_DIRS:
[cmake] – HDF5_LIBRARIES:
[cmake] – HDF5_DEFINITIONS:
[cmake] – HDF5 include: C:/Program Files/HDF_Group/HDF5/1.14.5/include;
[cmake] – HDF5 library: hdf5::hdf5-shared
BUILD FROM SOURCE - BUILDS BUT DOES NOT RUN
When I set HDF5_BUILD_FROM_SOURCE to ON, then it builds without errors, however the program terminates when I run it with a DLL not found error.
Error:
The program ‘[30692] farfield.exe’ has exited with code -1073741515 (0xc0000135).
CMakeBuild
[cmake] – hdf5_POPULATED: True
[cmake] – hdf5_BINARY_DIR: C:/Repos/farfield/build/_deps/hdf5-build
[cmake] – hdf5_SOURCE_DIR: C:/Repos/farfield/build/_deps/hdf5-src
[cmake] – HDF5 Found:
[cmake] – HDF5_VERSION:
[cmake] – HDF5_INCLUDE_DIRS: C:/Repos/farfield/build/_deps/hdf5-build/src
[cmake] – HDF5_LIBRARIES:
[cmake] – HDF5_DEFINITIONS:
[cmake] – HDF5 include:
[cmake] – HDF5 library: hdf5-shared
I’d appreciate any guidance, thank you!