3.1C
HDFql can be used in the C programming language through a shared library, which is stored in the directory “lib”. The following short program illustrates how HDFql can be used in this language.
// include HDFql C header file (make sure it can be found by the C compiler)
#include <stdlib.h>
#include <stdio.h>
#include "HDFql.h"
int main(int argc, char *argv[])
{
// display HDFql version in use
printf("HDFql version: %s\n", HDFQL_VERSION);
// create an HDF5 file named "my_file.h5" and use (i.e. open) it
hdfql_execute("CREATE AND USE FILE my_file.h5");
// create an HDF5 dataset named "my_dataset" of data type int
hdfql_execute("CREATE DATASET my_dataset AS INT VALUES(10)");
// select (i.e. read) data from dataset "my_dataset" and populate cursor in use with it
hdfql_execute("SELECT FROM my_dataset");
// move cursor in use to the first position within the result set
hdfql_cursor_first(NULL);
// display content of cursor in use
printf("Dataset value: %d\n", *hdfql_cursor_get_int(NULL));
return EXIT_SUCCESS;
}Assuming that the program is stored in a file named “example.c”, it must first be compiled before it can be launched. To compile the program against the HDFql C shared library, execute the following command in a terminal:
- On Windows using Microsoft Visual Studio:
cl.exe example.c /I<hdfql_include_directory> <hdfql_lib_directory>\HDFql_dll.lib - On Linux and macOS using GCC:
- With an HDFql non MPI-based distribution:
gcc example.c -I<hdfql_include_directory> -L<hdfql_lib_directory> -lHDFql -lm -ldl - With an HDFql MPI-based distribution:
gcc example.c -I<hdfql_include_directory> -L<hdfql_lib_directory> -L<mpi_lib_directory> -lHDFql -lmpi -lm -ldl
- With an HDFql non MPI-based distribution:
In case the program does not compile, check whether a C compiler is installed. If a C compiler is missing, the solution is:
- On Windows, download and install Microsoft Visual Studio from the website https://visualstudio.microsoft.com/downloads.
- On Linux, install the GCC C compiler by executing the following command in a terminal:
- On a Red Hat-based distribution:
sudo dnf install gcc - On a Debian-based distribution:
sudo apt install gcc
- On a Red Hat-based distribution:
- On macOS, install the GCC C compiler by executing the following command in a terminal:
- With Homebrew:
brew install gcc - With MacPorts:
sudo port install gcc
- With Homebrew:
In case the compiled program does not launch, it is most likely that the HDFql C shared library and/or the MPI shared library were not found (these are needed to launch the program). The solution is:
- On Windows, add the directory where the file “HDFql_dll.dll” is located to the environment variable “PATH” by executing the following command in a terminal:
set PATH=<hdfql_lib_directory>;%PATH% - On Linux, add the directories where the files “libHDFql.so” and (optionally) “libmpi.so” are located to the environment variable “LD_LIBRARY_PATH” by executing the following command in a terminal:
- With an HDFql non MPI-based distribution:
export LD_LIBRARY_PATH=<hdfql_lib_directory>:$LD_LIBRARY_PATH - With an HDFql MPI-based distribution:
export LD_LIBRARY_PATH=<hdfql_lib_directory>:<mpi_lib_directory>:$LD_LIBRARY_PATH
- With an HDFql non MPI-based distribution:
- On macOS, add the directories where the files “libHDFql.dylib” and (optionally) “libmpi.dylib” are located to the environment variable “DYLD_LIBRARY_PATH”3 by executing the following command in a terminal:
- With an HDFql non MPI-based distribution:
export DYLD_LIBRARY_PATH=<hdfql_lib_directory>:$DYLD_LIBRARY_PATH - With an HDFql MPI-based distribution:
export DYLD_LIBRARY_PATH=<hdfql_lib_directory>:<mpi_lib_directory>:$DYLD_LIBRARY_PATH
- With an HDFql non MPI-based distribution:
Notes
- Starting with version 10.11 (i.e. El Capitan), Apple introduced a security feature named System Integrity Protection (SIP) that may prevent setting the environment variable “DYLD_LIBRARY_PATH” and, ultimately, launching the program. To overcome this, SIP should be disabled (please refer to https://developer.apple.com/library/archive/documentation/Security/Conceptual/System_Integrity_Protection_Guide/ConfiguringSystemIntegrityProtection/ConfiguringSystemIntegrityProtection.html for additional information). ↩