tbeu / matio

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

undefined reference to `Mat_Open' #69

Closed htt210 closed 7 years ago

htt210 commented 7 years ago

I have installed matio 1.5.2 and found libmatio.a libmatio.la libmatio.so libmatio.so.2 libmatio.so.2.0.2 under /usr/local/lib/ directory. However, I cannot compile any program as the following errors appear:

/tmp/ccI8aVfw.o: In function `main':
testMatio.cpp:(.text+0x39): undefined reference to `Mat_Open'
testMatio.cpp:(.text+0xad): undefined reference to `Mat_VarRead'
testMatio.cpp:(.text+0x204): undefined reference to `Mat_VarRead'
testMatio.cpp:(.text+0x360): undefined reference to `Mat_VarRead'
testMatio.cpp:(.text+0x43f): undefined reference to `Mat_VarRead'
testMatio.cpp:(.text+0x519): undefined reference to `Mat_Close'
collect2: error: ld returned 1 exit status

My program is from the internet and it's as follow:

#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<<"Otwarto plik do odczytu\n\tmat == "<<mat<<"\n" ;

        matvar_t *matVar=0 ;
        std::cout<<"Wypisujemy dane\n\n" ;
        std::cout<<"x:\n" ;
        matVar = Mat_VarRead(mat, (char*)"x") ;
        if(matVar)
        {
            unsigned 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") ;
        unsigned 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" ;
        }

        std::cout<<"minX, step:\n";
        matVar = Mat_VarRead(mat, (char*)"minX") ;
        const double *minXData = static_cast<const double*>(matVar->data) ;
        std::cout<<"\tminX = "<<minXData[0]<<"\n" ;
        std::cout<<"\n" ;
        for(int i=0; i<matVar->rank; ++i)
        {
            std::cout<<"\tdim["<<i<<"] == "<<matVar->dims[i]<<"\n" ;
        }

        matVar = Mat_VarRead(mat, (char*)"step") ;
        const double *stepData = static_cast<const double*>(matVar->data) ;
        std::cout<<"\tstep = "<<stepData[0]<<"\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<<"Nie można otworzyć pliku\n" ;
        return 1;
    }
    return 0;
}

I compiled it with g++ -I/usr/include -L/usr/local/lib -lmatio -lz testMatio.c -o testMatio and with this from the source directory: g++ -I./src/ -L./src/.libs/ -lmatio -lz testMatio.cpp -o testMatio all give the same error.

Can you please help me to fix this? Thank you.

tbeu commented 7 years ago

Pls update to v1.5.10.

htt210 commented 7 years ago

The is no difference. I tried the github version as well as sudo apt install libmatio-dev Do you think this is a problem with the compiler? I am using Ubuntu 16.04 and gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609 g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609

tbeu commented 7 years ago

Hm, cannot reproduce. Linking runs for me.

htt210 commented 7 years ago

Can you type in the coomand you used to comple the above code

htt210 commented 7 years ago

I can compile it now by copying the command used in the Makefile. I missed some directories.

gcc -DHAVE_CONFIG_H -I. -I../src -I../src -I../getopt -g -O2 -c -o testMatio testMatio.cpp