【发布时间】:2013-12-11 16:36:10
【问题描述】:
我正在尝试在使用 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 小牛(遗憾的是整个“clang”崩溃)。我真的很感激这方面的任何帮助。
【问题讨论】:
-
你定义了两次 max,没有给出 min 的定义。
-
哎呀...我现在可以看到了。尽管如此,我还是找到了一个不同的解决方案,我在下面发布了。
-
C 中没有 min/max 函数,C++ 有 std::min 和 std::max
标签: c matlab max mex undefined-symbol