Thursday, November 11, 2021

[SOLVED] How to add the path of data file in CMakeLists?

Issue

I have a cmake project with a data file. In order to execute the output file I need to copy paste the data file in the build directory. The tree structure of project looks like this: enter image description here After running cmake the output file is stored in /build/src/main.o and data file is copied in /build/data/xaa.h5 . I do not want to copy the data file every time. Is there a way that main.o file access the data file which is stored in data/xaa.h5? I tried the file_configure, but something is not quite correct! Here is top level cmakelists

cmake_minimum_required(VERSION 3.1)

set(CMAKE_USER_MAKE_RULES_OVERRIDE "cmake/platform.cmake")

# C language only needed due to regression in FindHDF5.
project(H5XX LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

find_package(Boost 1.40.0 QUIET REQUIRED COMPONENTS unit_test_framework)
find_package(HDF5 COMPONENTS C REQUIRED)

include_directories(SYSTEM ${HDF5_INCLUDE_DIRS})
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
include_directories(${CMAKE_SOURCE_DIR})

set(H5XX_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/h5xx")
add_subdirectory(data)
add_subdirectory(src)

and this is from the directory data:

cmake_minimum_required(VERSION 2.8)

configure_file( xaa.h5 ${CMAKE_CURRENT_BINARY_DIR}/xaa.h5 COPYONLY)

I want to read the data from the file and then print it

#include <boost/multi_array.hpp>
#include <h5xx/h5xx.hpp>
#include <iostream>

using array_2d_t = boost::multi_array<float, 2>;

template <typename T> void print_array(T const& array)
{
    for (auto const& row : array) 
        { for (auto v : row)
            printf("%5f ", v);
        printf("\n");
    }
}

array_2d_t read_frame(int frame_no) {
    h5xx::file xaa("xaa.h5", h5xx::file::mode::in);

    h5xx::group   g(xaa, "particles/lipids/box/positions");
    h5xx::dataset ds(g, "value");

    array_2d_t arr(boost::extents[11214][3]);

    std::vector<float> offsets{frame_no, 0, 0}, counts{1, 11214, 3};
    h5xx::slice slice(offsets, counts);

    h5xx::read_dataset(ds, arr, slice);
    return arr;
}

int main()
{
    print_array(read_frame(9));
}

Solution

May be you can try the file https://cmake.org/cmake/help/latest/command/file.html, instead of file_configure.



Answered By - Friction