gpt4 book ai didi

c++ - 编译 MatIO 时出现链接错误

转载 作者:行者123 更新时间:2023-11-28 05:54:55 35 4
gpt4 key购买 nike

我已经构建了 matio使用提供的说明 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 选项。编译器不会扫描所有库来查找符号。母/母:

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

关于c++ - 编译 MatIO 时出现链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34393058/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com