无法使用MATLAB mex链接外部lib(CLAPACK)

时间:2014-05-17 05:30:47

标签: matlab mex lapack lnk2019

我是mex的新手,这个问题花了我几天但我仍然无法弄清楚该怎么做。

我创建了la_test.cpp文件来测试CLAPCK中的一个子程序:cgemm_(复杂矩阵 - 矩阵乘法)。这是代码:

#include "mex.h"
#include "matrix.h"
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <iostream>
#include <stdio.h>
#include "f2c.h"
#include "clapack.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{

float *Xr,*Xi; // To store the input data
float *zsr,*zsi; // To store the output
long int m,n;
complex *A;

Xr = (float *) mxGetPr(prhs[0]);
Xi = (float *) mxGetPi(prhs[0]);

size_t K = mxGetNumberOfDimensions(prhs[0]);
const int *size = mxGetDimensions(prhs[0]); 

m = mxGetM(prhs[0]); 
n = mxGetN(prhs[0]); 

A = new complex[m*n];
complex one = {1.0f, 0.0f}, zero = {0.0f, 0.0f};

for (int i=0; i<m; i++){
     for (int j=0; j<n; j++){
           complex rc = {Xr[j + n*i], Xi[j + n*i]};
           A[j + n*i] = rc;
    }
}

plhs[0] =(mxArray *) mxCreateDoubleMatrix( n, n, mxCOMPLEX );

zsr = (float *) mxGetPr(plhs[0]);
zsi = (float *) mxGetPi(plhs[0]);

complex *AtA = 0;
AtA = new complex[n*n];
char *chn = "N";
char *chc = "C";
cgemm_(chc, chn, &n, &n, &m, &one, A, &m, A, &m, &zero, AtA, &n);

for (int i=0; i<m; i++){
       for (int j=0; j<n; j++){
           zsr[j + n*i] = AtA[j + n*i].r;
           zsi[j + n*i] = AtA[j + n*i].i;
        }
     }
}

基本上,我将输入矩阵存储到A中并尝试计算A&#39; * A.头文件:f2c.h,clapack.h以及三个64位库:来自http://icl.eecs.utk.edu/lapack-for-windows/clapack/index.html#install的blas.lib,libf2c.lib和lapack.lib都在la_test.cpp的同一个文件中。我正在使用matlab r2013a和Visual Studio 2012在Windows 7 64位系统上工作。

我试过两个:
mex la_test.cpp lapack.lib libf2c.lib blas.lib

mex -llapack -llibf2c -lblas -L"C:\Users\Ziwu\Desktop\la_test" la_test.cpp

以下错误:

Creating library C:\Users\Ziwu\AppData\Local\Temp\mex_epl230\templib.x and object C:\Users\Ziwu\AppData\Local\Temp\mex_epl230\templib.exp 
la_test.obj : error LNK2019: unresolved external symbol cgemm_ referenced in function mexFunction 
la_test.mexw64 : fatal error LNK1120: 1 unresolved externals

我已经在互联网上查了很久,但还没找到解决办法!

如果您有任何建议,请帮助我。

0 个答案:

没有答案