0

so after building and installing the MATIO library using CMake, I am now having problems compiling it. Here's my code:


#include <iostream>
#include <matio.h>
#define tS(x) std::cout<<"\t"<<(#x)<<" == "<<(x)<<"\n" 
int main(int argc, char **argv)
{
    const char *fileName = argc==1?"./S.mat":argv[1] ;
    mat_t *mat = Mat_Open(fileName,MAT_ACC_RDONLY);
    if(mat)
    {
        std::cout<<"A file was opened for reading\n\tmat == "<<mat<<"\n" ;

        matvar_t *matVar=0 ;
        std::cout<<"Writing out the data\n\n" ;
        std::cout<<"x:\n" ;
        matVar = Mat_VarRead(mat, (char*)"x") ;
        if(matVar)
        {
            int xSize = matVar->nbytes/matVar->data_size ;
            const double *xData = static_cast<const double*>(matVar->data) ;
            for(int i=0; i<xSize; ++i)
            {
                std::cout<<"\tx["<<i<<"] = "<<xData[i]<<"\n" ;
            }
            std::cout<<"\n" ;
            for(int i=0; i<matVar->rank; ++i)
            {
                std::cout<<"\tdim["<<i<<"] == "<<matVar->dims[i]<<"\n" ;
            }
        }

        std::cout<<"y:\n" ;
        matVar = Mat_VarRead(mat, (char*)"y") ;
        int ySize = matVar->nbytes/matVar->data_size ;
        const double *yData = static_cast<const double*>(matVar->data) ;
        for(int i=0; i<ySize; ++i)
        {
            double d = yData[i] ;
            std::cout<<"\ty["<<i<<"] = "<<d<<"\n" ;
        }
        std::cout<<"\n" ;
        for(int i=0; i<matVar->rank; ++i)
        {
            std::cout<<"\tdim["<<i<<"] == "<<matVar->dims[i]<<"\n" ;
        }

        Mat_Close(mat);
    }
    else
    {
        std::cout<<"File cannot be opened\n" ;
        return 1;
    }
    return 0;
}

However I get the following error:

C:\WINDOWS\system32\cmd.exe /C ""C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/mingw32-make.exe" -j4 SHELL=cmd.exe -e -f  Makefile"
"----------Building project:[ Test - Debug ]----------"
mingw32-make.exe[1]: Entering directory 'C:/Users/RS3/Desktop/SCC HiWi/Code/C++/myGmm/UQ/Test'
"C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe"  -c  "C:/Users/RS3/Desktop/SCC HiWi/Code/C++/myGmm/UQ/Test/main.cpp" -std=c++14 -Wall -g -O0 -Wall -I/Users/RS3/matio/getopt -I/matio/include -L/matio/bin -lmatio   -o ./Debug/main.cpp.o -I. -I. -I"C:\Users\RS3\Desktop\SCC HiWi\Code\C++\myGmm\UQ\eigen-3.3.9" -I"C:\Users\RS3\Desktop\SCC HiWi\Code\C++\myGmm\UQ\Halton" -I"C:\Users\RS3\Desktop\SCC HiWi\Code\C++\myGmm\UQ\quasimvnrnd"
"C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe" -o ./Debug/Test @"Test.txt" -L. -L"C:\Users\RS3\Desktop\SCC HiWi\Code\C++\myGmm\UQ\eigen-3.3.9"   -static-libgcc -static-libstdc++
./Debug/main.cpp.o: In function `main':
C:/Users/RS3/Desktop/SCC HiWi/Code/C++/myGmm/UQ/Test/main.cpp:38: undefined reference to `Mat_Open'
C:/Users/RS3/Desktop/SCC HiWi/Code/C++/myGmm/UQ/Test/main.cpp:46: undefined reference to `Mat_VarRead'
C:/Users/RS3/Desktop/SCC HiWi/Code/C++/myGmm/UQ/Test/main.cpp:63: undefined reference to `Mat_VarRead'
C:/Users/RS3/Desktop/SCC HiWi/Code/C++/myGmm/UQ/Test/main.cpp:77: undefined reference to `Mat_Close'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[1]: *** [Test.mk:81: Debug/Test] Error 1
mingw32-make.exe[1]: Leaving directory 'C:/Users/RS3/Desktop/SCC HiWi/Code/C++/myGmm/UQ/Test'
mingw32-make.exe: *** [Makefile:5: All] Error 2
====4 errors, 0 warnings====

I looked for this error right and left and I know the linker can't find the library for some reason despite me having the following compiler options: -g;-O0;-Wall;-I/Users/RS3/matio/getopt;-I/matio/include;-L/matio/bin;-lmatio

I'm using Codelite IDE.

I am not sure what I am missing and it's driving me crazy. Any help would be appreciated!

Bializm
  • 1
  • 4
  • A library is unlikely to reside in `bin`, it's more likely to be in `lib` or `lib64`. Look for `libmatio.so` (or `libmatio.a`) and use the path where it's in for the `-L` flag. – dabo42 May 01 '21 at 20:13
  • `@"Test.txt"` contains all the relevant information. Without knowing what's inside it's impossible to tell what's going on. – n. m. could be an AI May 01 '21 at 20:50
  • There's literally one line in Test.txt: "./Debug/main.cpp.o " That's it. – Bializm May 01 '21 at 21:05
  • After changing the path from ```bin``` to ```lib```, I still get the same error. However when I look inside ```lib```, I only find ```libmatio.dll.a```, no ```libmatio.a```. Does that make a difference? – Bializm May 01 '21 at 21:10
  • I know I probably should do that, but as I am out of options I just renamed the ```libmatio.dll.a``` in ```lib``` to ```libmatio.a```, but of course this hasn't solved the problem. – Bializm May 01 '21 at 21:32
  • Seems you also added your `-L -l` switches to the compile command (`g++ -c ...`). That's not where they belong, you need to add them to the link command instead (`g++ -o ./Debug/Test ...`). As for the library name - not sure how mingw expects libraries to be named, maybe the `.dll.a` works. If not, I'd change the link flag to `-lmatio.dll` instead of renaming the file. – dabo42 May 01 '21 at 22:55
  • `-c` means "compile, don't link". Library options are irrelevant and ignored when compiling. They are needed when linking, bit you are not passing them to the command that does the linking. – n. m. could be an AI May 02 '21 at 03:57

0 Answers0