tbeu / matio

MATLAB MAT File I/O Library
https://matio.sourceforge.io
BSD 2-Clause "Simplified" License
338 stars 97 forks source link

Microsoft Windows minimal CMake configuration #217

Closed colombojrj closed 7 months ago

colombojrj commented 1 year ago

Hi,

I opened this as an issue because other people may want to use this cmake script at Windows. I believe many users (just as myself) do not care about organizing cmake or compiling dependencies or correct placing files just to the compiler to be happy. We only want to use this beautiful piece of code (matio). To do that, I wrote a small cmake script to help.

Before starting:

  1. install Microsoft Visual Studio (community edition is fine) with support to C/C++
  2. install hdf5 library (next, next, next, install and do not mess with default location)
  3. install cmake

Now, create some folder where you want to keep your project. Inside that folder, create a file named CMakeLists.txt and copy and paste the following into it:

# This version is critical. Accordingly to CMake documentation, FetchContent is only available after this version
cmake_minimum_required(VERSION 3.11) 

include(ExternalProject) # For more information: # https://crascit.com/2015/07/25/cmake-gtest
include(FetchContent)

####################
# Project settings #
####################
project(matio_example C CXX)

###############################
# All binary will be put here #
###############################
set(LIBS_DIR ${CMAKE_CURRENT_BINARY_DIR}/libs)

################
# HDF5 library #
################
set(HDF5_USE_STATIC_LIBRARIES ON)
find_package(HDF5 REQUIRED COMPONENTS C HL)

################
# zlib library #
################
ExternalProject_Add (zlibProject
    PREFIX            zlib
    URL               https://github.com/madler/zlib/releases/download/v1.2.13/zlib1213.zip
    CMAKE_ARGS        -DCMAKE_INSTALL_PREFIX=${LIBS_DIR}
)
set(ZLIB_INCLUDE_DIR ${LIBS_DIR}/include)
set(ZLIB_LIBRARY     ${LIBS_DIR}/lib)

########################
# eigen matrix library #
########################
ExternalProject_Add(eigen
    PREFIX            eigen
    URL               https://gitlab.com/libeigen/eigen/-/archive/3.4.0/eigen-3.4.0.zip
    CMAKE_ARGS        -DCMAKE_INSTALL_PREFIX=${LIBS_DIR}
)
include_directories(${LIBS_DIR}/include/eigen3)

#################
# matio library #
#################
set(MATIO_SHARED OFF CACHE INTERNAL "")  # Forces to use static library
FetchContent_Declare(
    matioProject
    URL               https://github.com/tbeu/matio/archive/refs/tags/v1.5.23.zip
)
FetchContent_MakeAvailable(matioProject)
target_include_directories(matio PUBLIC ${LIBS_DIR}/include)
add_dependencies(matio zlibProject)
link_libraries(matio)

#######################
# eigen-matio library #
#######################
set(EIGEN_MATIO_URL  https://raw.githubusercontent.com/tesch1/eigen-matio/master/MATio)
set(EIGEN_MATIO_DIR ${LIBS_DIR}/include)
set(EIGEN_MATIO_DEST ${EIGEN_MATIO_DIR}/MATio)
if(NOT EXISTS EIGEN_MATIO_DEST)
    file(DOWNLOAD ${EIGEN_MATIO_URL} ${EIGEN_MATIO_DEST})
endif()
include_directories(${EIGEN_MATIO_DIR})

############################
# We add our projects here #
############################
add_executable(my_executable main.cpp)
add_dependencies(my_executable matio eigen)

You may create a main.cpp file with the following content:

#include <iostream>

// Matrix library. you may find documentation in the link below:
// https://eigen.tuxfamily.org/dox/GettingStarted.html
#include <Eigen/Dense>
using namespace Eigen;

// The MATio library is used to load matlab files. You may find documentation in the link below:
// https://github.com/tesch1/eigen-matio
#include <MATio>

int main()
{
    double alpha = 123;  // dummy value to alpha
    MatrixXd aux;
    matio::read_mat("../data.mat", "alpha", aux);
    alpha = aux(0);
    std::cout << "alpha = " << alpha << std::endl;

    system("pause");

    return 0;
}

It will try to load a scalar variable named "alpha" from a file "data.mat".

I have spent many hours of my life trying to make this complete script that just works. I hope it helps someone.

tbeu commented 1 year ago

Hm, matio has a thoroughly tested CMake configuration. Binaries are alo is available from ConanCenter together with its dependencies zlib and hdf5. Also eigen is available there.

tbeu commented 7 months ago

Thanks. It's available from https://github.com/tbeu/matio-eigen-example now.