编译 MatIO 时出现链接错误

Linking error while compiling MatIO

我已经构建了 matio using the intructions provided here。但是当我尝试 运行 下面给出的代码时,出现以下错误:

/tmp/ccIrtdDv.o: In function `main':
test.cpp:(.text+0x39): undefined reference to `Mat_Open'
test.cpp:(.text+0xaa): undefined reference to `Mat_VarRead'
test.cpp:(.text+0x20b): undefined reference to `Mat_VarRead'
test.cpp:(.text+0x359): undefined reference to `Mat_VarRead'
test.cpp:(.text+0x43c): undefined reference to `Mat_VarRead'
test.cpp:(.text+0x51a): undefined reference to `Mat_Close'
collect2: ld returned 1 exit status

这是我写的代码。

#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;
}

我编译代码使用:

g++ -I/usr/include -L/usr/local/lib test.cpp -o test

是不是安装有问题,因为我只是按照他们README里的步骤来的。如果没有,那我该如何解决?提前致谢。

您应该指定 -l 选项。编译器不会扫描所有库来查找符号。 F/e:

g++ -I/usr/include -L/usr/local/lib -lmatio test.cpp -o test