Home > OS >  unresolved external symbol while loading HDF5 library via vcpkg to C vscode project
unresolved external symbol while loading HDF5 library via vcpkg to C vscode project

Time:02-26

I am using visual studio project 2019- and vcpkg in order to load data to CUDA 11.6 C visual studio project.
at the begining of the file i have :

// #define H5_BUILT_AS_DYNAMIC_LIB 
#include <H5Cpp.h>

and it do not give any errors - so I assume it was correctly loaded and integrated by by vcpkg. Also as visible at the end of the post H5 files are visible in external dependencies list of a solution explorer.

simple code that leads to error is shown below

    H5::H5File file(FILE_NAME, H5F_ACC_RDONLY);
    H5::DataSet dset = file.openDataSet(DATASET_NAME);

error (set of unresolved external symbol errors) - it appears in compile time. enter image description here

If I will uncomment #define H5_BUILT_AS_DYNAMIC_LIB 1 the error discussed above disappears but new shows (in runtime) -

hdf5_cpp_D.dll not found ...

Exactly the same code worked perfectly well on my cmake CUDA C project, but for various reasons I need to switch to visual studio project.

Minimal example giving error

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
//#define H5_BUILT_AS_DYNAMIC_LIB 1
#include <H5Cpp.h>
#include <stdio.h>





void loadHDFIntoBoolArr(H5std_string FILE_NAME, H5std_string DATASET_NAME, bool*& data) {


    H5::H5File file(FILE_NAME, H5F_ACC_RDONLY);
    H5::DataSet dset = file.openDataSet(DATASET_NAME);
    /*
     * Get the class of the datatype that is used by the dataset.
     */
    H5T_class_t type_class = dset.getTypeClass();
    H5::DataSpace dspace = dset.getSpace();
    int rank = dspace.getSimpleExtentNdims();


    hsize_t dims[2];
    rank = dspace.getSimpleExtentDims(dims, NULL); // rank = 1
    printf("Datasize: %d \n ", dims[0]); // this is the correct number of values

     // Define the memory dataspace
    hsize_t dimsm[1];
    dimsm[0] = dims[0];
    H5::DataSpace memspace(1, dimsm);

    data = (bool*)calloc(dims[0], sizeof(bool));

    dset.read(data, H5::PredType::NATIVE_HBOOL, memspace, dspace);

    file.close();

}

void loadHDF() {
    const int WIDTH = 512;
    const int HEIGHT = 512;
    const int DEPTH = 826;


    const H5std_string FILE_NAMEonlyLungsBoolFlat("C:\\Users\\1\\PycharmProjects\\pythonProject3\\mytestfile.hdf5");
    const H5std_string DATASET_NAMEonlyLungsBoolFlat("onlyLungsBoolFlat");
    // create a vector the same size as the dataset
    bool* onlyLungsBoolFlat;
    loadHDFIntoBoolArr(FILE_NAMEonlyLungsBoolFlat, DATASET_NAMEonlyLungsBoolFlat, onlyLungsBoolFlat);

  

}


int main()
{
    loadHDF();
    return 0;
}

vcpk commands used


.\vcpkg install hdf5
.\vcpkg install hdf5[cpp]
.\vcpkg integrate install

enter image description here

CodePudding user response:

If you build this with H5_BUILT_AS_DYNAMIC_LIB 1 defined, there should be the hdf5_cpp_D.dll lying around under the vcpkg_installed directory somewhere. Look at $(vcpkg_root)\installed\ and search for a hdf5 directory. There should be a .\bin and a .\debug\bin directory there with hdf5_cpp.dll and hdf5_cpp_D.dll. (I am guessing that _D is for debug).

Normally vcpkg integrate install will set up Visual Studio so that the dlls from the vcpkg directory will be copied to your output directory automatically. But this may fail if the hdf5 DLL is loaded dynamically at runtime. You will have to copy it manually (or add the path to the PATH environment variable).


If you build this without H5_BUILT_AS_DYNAMIC_LIB 1 defined, the symbols really should be defined in the static lib (hdf5.lib). Search for them in the $(vcpkg)\installed directory. There should be two (or four) different ones, one for release and one for debug (and each one for static vs. dll).

Oh, I forgot: Which vcpkg triplet are you using? That may also play a role into which hdf5.lib will be linked.

If you still get the linker error (undefined symbols), then perhaps the vcpkg port of hdf5 is not meant to be used with static linking. You should open a ticket then with hdf5.


CodePudding user response:

The solution in my situation was simple just reinstall windows - on fresh installation I did steps as mentioned at the begining and all works now

  • Related