0
我想在使用mex的MATLAB脚本中使用以下C文件。MATLAB mex - 未定义的符号_max
#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)) ;
}
然而,这trows错误:
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.
这seesm给我建议使用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; })
虽然这并没有解决错误,虽然我现在获取有关代码重新定义宏的警告。链接错误依然存在。
我正在运行(令人遗憾的是整个'clang'崩溃)OSX小牛与MATLAB R2012b。我真的很感谢这方面的帮助。
您正在定义最大值两次,没有给出min的定义。 – Daniel
woops ...现在我可以看到。尽管如此,我发现了我在下面发布的一个不同的解决方案。 – JamesFurness
C,C++中没有最小/最大函数,另一个函数有std :: min和std :: max – Amro