Getting ‘undefined reference to’ when compiling simple HDF5 example in Eclipse

StackOverflow https://stackoverflow.com/questions/22483473

  •  16-06-2023
  •  | 
  •  

문제

I just started with C and also with HDF5 so my question might be very easy to answer.

I tried an example-code from http://beige.ucs.indiana.edu/I590/node122.html to create an HDF5 file.

#include "hdf5.h"
#define FILE "file.h5"

main() {

   hid_t       file_id;   /* file identifier */
   herr_t      status;

   /* Create a new file using default properties. */
   file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

   /* Terminate access to the file. */
   status = H5Fclose(file_id);
}

I tried to compile this with Eclipse and got 5 error messages:

make: *** [Read_HDF5] Error 1
skipping incompatible /usr/lib/libc.so when searching for -lc
undefined reference to `H5check_version'
undefined reference to `H5Fcreate'
undefined reference to `H5Fclose'

What went wrong?

Thanks for you help!!!

도움이 되었습니까?

해결책

You need to link the HDF5 library when compiling. The errors you get mean the code for these functions is missing, because you didn't link the library file that contains them. The easiest way is to compile using h5cc as described in the tutorial you mentioned. Otherwise, if you prefer compiling in Eclipse, you need to search for the linker flags setting and add the correct flags which you can find by typing h5cc -show in a terminal.

For instance, for me, h5cc -show gives

clang -I/usr/local/include -L/usr/local/Cellar/hdf5/1.8.12/lib /usr/local/Cellar/hdf5/1.8.12/lib/libhdf5_hl.a /usr/local/Cellar/hdf5/1.8.12/lib/libhdf5.a -L/usr/local/lib -lsz -lz -ldl -lm

Piece by piece:

  • clang is the compiler

  • -I/usr/local/include is a flag telling the compiler to search for header files inside the /usr/local/include directory

  • -L/usr/local/Cellar/hdf5/1.8.12/lib and -L/usr/local/lib are flags telling the linker to search for libraries inside these directories

  • /usr/local/Cellar/hdf5/1.8.12/lib/libhdf5_hl.a and /usr/local/Cellar/hdf5/1.8.12/lib/libhdf5.a are the full path to two HDF5 static libraries (telling the linker to link them)

  • -lsz, -lz, -ldl and -lm are flags telling the linker to link the libraries sz, z, dl and m

This is probably overkill. If your HDF5 is installed in a standard location, it might be enough to just add -lhdf5 to the linker flags.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top