gpt4 book ai didi

c - MATLAB mex - undefined symbol _max

转载 作者:行者123 更新时间:2023-11-30 15:41:26 24 4
gpt4 key购买 nike

我正在尝试使用 mex 在 MATLAB 脚本中使用以下 C 文件。

#include <math.h>
#include "mex.h"
#include "blas.c"



static void lbidiagQR (int n, double* gamma, double* delta, double mu,
double* Q, double* u, double* v)
{
int i, ldQ ;
double tmp ;

ldQ = n*2 ;
u[0] = gamma[0] ;
for (i = 0 ; i < n ; ++i)
{
tmp = mu ;
rotg (&u[i], &tmp, &Q[i*2], &Q[i*2 +ldQ]) ;

tmp = delta[i] ;
rotg (&u[i], &tmp, &Q[i*2+1], &Q[i*2+1+ldQ]) ;

if (i < n-1)
{
v[i] = 0.0 ;
u[i+1] = gamma [i+1] ;

rot (&v[i], &u[i+1], Q[i*2+1], Q[i*2+1+ldQ]) ;
}
}
}

// input arguments
#define gamma prhs[0]
#define delta prhs[1]
#define mu prhs[2]

// output arguments
#define Q plhs[0]
#define u plhs[1]
#define v plhs[2]

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

// check for proper number of arguments
if (nrhs != 3) mexErrMsgTxt ("lbidiagQR requires three input arguments.") ; else
if (nlhs != 3) mexErrMsgTxt ("lbidiagQR requires three output arguments.") ;

// check the dimensions of gamma
n = max (mxGetM (gamma), mxGetN (gamma)) ;
if (min (mxGetM (gamma), mxGetN (gamma)) != 1)
mexErrMsgTxt ("gamma must be an n-by-1 or a 1-by-n matrix.") ;

// check the dimensions of delta
if ((min (mxGetM (delta), mxGetN (delta)) != 1) ||
(max (mxGetM (delta), mxGetN (delta)) != n))
mexErrMsgTxt ("delta must be an n-by-1 or a 1-by-n matrix.") ;

// check the dimensions of mu
if ((mxGetM (mu) != 1) || (mxGetN (mu) != 1))
mexErrMsgTxt ("mu must be a scalar.") ;

// create matrices for the return arguments
Q = mxCreateDoubleMatrix (n*2, 2, mxREAL) ;
u = mxCreateDoubleMatrix (n, 1, mxREAL) ;
v = mxCreateDoubleMatrix (n-1, 1, mxREAL) ;

// do the actual computations in a subroutine
lbidiagQR (n, mxGetPr (gamma), mxGetPr (delta), *mxGetPr (mu),
mxGetPr (Q), mxGetPr (u), mxGetPr (v)) ;
}

但是这会引发错误:

c_routines/lbidiagqr.c:75:9: warning: implicit declaration of function 'max' is invalid in C99
[-Wimplicit-function-declaration]
n = max (mxGetM (gamma), mxGetN (gamma)) ;
^
c_routines/lbidiagqr.c:76:9: warning: implicit declaration of function 'min' is invalid in C99
[-Wimplicit-function-declaration]
if (min (mxGetM (gamma), mxGetN (gamma)) != 1)
^
2 warnings generated.
Undefined symbols for architecture x86_64:
"_max", referenced from:
_mexFunction in lbidiagqr.o
"_min", referenced from:
_mexFunction in lbidiagqr.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

mex: link of ' "lbidiagqr.mexmaci64"' failed.

在我看来,这表明使用 max() 和 min() 的行是问题所在。环顾四周后,我发现这些可以使用宏定义为:

#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })

#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })

尽管这并不能解决错误,但我现在收到有关在代码中重新定义宏的警告。链接错误仍然存​​在。

我正在使用 MATLAB R2012b 运行 OSX mavericks(遗憾的是,整个“clang”崩溃)。我非常感谢对此的任何帮助。

最佳答案

我可能已经解决了这个问题。

将它们定义为宏并没有起作用,但在我的例子中,我可以通过假设这些值是 float (因为它们将在 matlab 代码中)来逃脱。有了这些知识,我使用了 math.h fmin() fmax() 中的函数

这可以让代码编译良好。

再花一点时间,我将能够完全看到这是否有效,因为代码将完全可运行。

编辑:

是的!这解决了问题。明确知道这些值是 float 使我能够在 C 代码中使用 fmin 和 fmax 代替 min 和 max 来成功编译、链接和运行。

很抱歉使用此空间进行橡皮鸭调试,但也许这对将来的某人有用。祝你 future 的问题对象好运。

关于c - MATLAB mex - undefined symbol _max,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20524741/

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