【发布时间】:2013-05-29 07:40:19
【问题描述】:
我正在尝试使用 "Cuba" "library",它定义了多维积分例程,并在 C 程序中使用这些例程(我通常使用 Mathematica,但对于我现在需要计算的积分,Mathematica 存在巨大的收敛问题)。我安装了包Cuba-3.1.tar.gz(“new”)的元素,并尝试编译demo文件demo-c.c,但经过很多小时的尝试,我仍然没有成功(我只学到了一点点在学校学过一点 C,但从那以后就再也没有用过,或者真的编译过其他任何东西,所以我是一个新手),我不确定哪里出了问题。
总结:我不确定我的计算机/编译器配置是否正确,是否使用了正确的命令行,或者是否有其他问题,但几乎没有任何关于古巴的文档,也没有用户论坛.
为了清楚起见,让我分解代码的重要部分,然后在问题末尾复制整个内容。
包括:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "cuba.h"
在 cd 到包含 demo-c.c 和 cuba.h 的文件夹中(我将 cuba.h 复制到 demo 文件夹中),然后我运行:
gcc -o demo-c demo-c.c
我得到的输出是:
Undefined symbols:
"_Vegas", referenced from:
_main in cc7i6pMP.o
ld: symbol(s) not found
我尝试了其他各种编译命令,这里是一个列表:
gcc -o demo-c demo-c.c -lcuba -lmgcc -o demo-c demo-c.c -L/users/username/Applications/Cuba-3.1/demogcc -iquote ~/Applications/Cuba-3.1/demo/cuba.h -o demo-c demo-c.cgcc -I/../../../../Applications/Cuba-3.1/demo -o demo-c demo-c.cg95 -o -L/../../../../Applications/Cuba-3.1/demo demo-fortran demo-fortran.Fg95 -o demo-fortran demo-fortran.F -L/../../../../Applications/Cuba-3.1/democc -o demo-c demo-c.c -lcuba -lmcc -o -iquote ../ demo-c demo-c.cgcc -o demo-c demo-c.c -I/./lcuba
和其他一些小的变化,但得到了基本相同的错误消息(也适用于 fortran 代码)。请注意,文档说代码应该完全使用第 6 行编译:
cc -o demo-c demo-c.c -lcuba -lm
从网上的大量研究中,我了解到“main”函数一定有问题,即:
int main() {
int verbose, comp, nregions, neval, fail;
double integral[NCOMP], error[NCOMP], prob[NCOMP];
const char *env = getenv("CUBAVERBOSE");
verbose = 2;
if( env ) verbose = atoi(env);
#if 1
printf("-------------------- Vegas test --------------------\n");
Vegas(NDIM, NCOMP, Integrand, USERDATA,
EPSREL, EPSABS, verbose, SEED,
MINEVAL, MAXEVAL, NSTART, NINCREASE, NBATCH,
GRIDNO, STATEFILE,
&neval, &fail, integral, error, prob);
printf("VEGAS RESULT:\tneval %d\tfail %d\n",
neval, fail);
for( comp = 0; comp < NCOMP; ++comp )
printf("VEGAS RESULT:\t%.8f +- %.8f\tp = %.3f\n",
integral[comp], error[comp], prob[comp]);
#endif
return 0;
}
但我真的看不出那是什么。的确,维加斯的所有论点似乎都得到了很好的定义。
由于文件 cuba.h 实际上很小,我尝试将其复制并粘贴到 demo-c.c 中,在 main 之前,我得到以下错误:
demo-c.c:74: error: array type has incomplete element type
demo-c.c:76: error: array type has incomplete element type
demo-c.c:83: error: array type has incomplete element type
第 74、76 和 83 行对应
const double x[], double f[], and double x[]);
在
typedef int (*integrand_t)(
const int *ndim,
const double x[],
const int *ncomp,
double f[],
void *userdata);
typedef void (*peakfinder_t)(
const int *ndim,
const double b[],
int *n,
double x[]);
(这是我粘贴在 demo-c.c 中的 cuba.h 的摘录)。我试过了
const double x[10], double f[10], and double x[10]);
但不出所料,这会产生(其他)错误。
系统
我正在使用运行 Snow Leopard (10.6.8) 和 XCode 3.2.6 的 MacBook(2009 年 5 月 - 2.4GHz Intel Core Duo 8Gb 1067MHz DDR3)。
完整代码demo-c.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "cuba.h"
static inline double Sq(double x) {
return x*x;
}
static int Integrand(const int *ndim, const double xx[],
const int *ncomp, double ff[], void *userdata) {
#define x xx[0]
#define y xx[1]
#define z xx[2]
#define f ff[0]
#ifndef FUN
#define FUN 1
#endif
#define rsq (Sq(x) + Sq(y) + Sq(z))
#if FUN == 1
f = sin(x)*cos(y)*exp(z);
#elif FUN == 2
f = 1/(Sq(x + y) + .003)*cos(y)*exp(z);
#elif FUN == 3
f = 1/(3.75 - cos(M_PI*x) - cos(M_PI*y) - cos(M_PI*z));
#elif FUN == 4
f = fabs(rsq - .125);
#elif FUN == 5
f = exp(-rsq);
#elif FUN == 6
f = 1/(1 - x*y*z + 1e-10);
#elif FUN == 7
f = sqrt(fabs(x - y - z));
#elif FUN == 8
f = exp(-x*y*z);
#elif FUN == 9
f = Sq(x)/(cos(x + y + z + 1) + 5);
#elif FUN == 10
f = (x > .5) ? 1/sqrt(x*y*z + 1e-5) : sqrt(x*y*z);
#else
f = (rsq < 1) ? 1 : 0;
#endif
return 0;
}
/*********************************************************************/
#define NDIM 3
#define NCOMP 1
#define USERDATA NULL
#define EPSREL 1e-3
#define EPSABS 1e-12
#define LAST 4
#define SEED 0
#define MINEVAL 0
#define MAXEVAL 50000
#define NSTART 1000
#define NINCREASE 500
#define NBATCH 1000
#define GRIDNO 0
#define STATEFILE NULL
#define NNEW 1000
#define FLATNESS 25.
#define KEY1 47
#define KEY2 1
#define KEY3 1
#define MAXPASS 5
#define BORDER 0.
#define MAXCHISQ 10.
#define MINDEVIATION .25
#define NGIVEN 0
#define LDXGIVEN NDIM
#define NEXTRA 0
#define KEY 0
int main() {
int verbose, comp, nregions, neval, fail;
double integral[NCOMP], error[NCOMP], prob[NCOMP];
const char *env = getenv("CUBAVERBOSE");
verbose = 2;
if( env ) verbose = atoi(env);
#if 1
printf("-------------------- Vegas test --------------------\n");
Vegas(NDIM, NCOMP, Integrand, USERDATA,
EPSREL, EPSABS, verbose, SEED,
MINEVAL, MAXEVAL, NSTART, NINCREASE, NBATCH,
GRIDNO, STATEFILE,
&neval, &fail, integral, error, prob);
printf("VEGAS RESULT:\tneval %d\tfail %d\n",
neval, fail);
for( comp = 0; comp < NCOMP; ++comp )
printf("VEGAS RESULT:\t%.8f +- %.8f\tp = %.3f\n",
integral[comp], error[comp], prob[comp]);
#endif
return 0;
}
完整代码 cuba.h
#ifdef __cplusplus
extern "C" {
#endif
typedef int (*integrand_t)(const int *ndim, const double x[],
const int *ncomp, double f[], void *userdata);
typedef void (*peakfinder_t)(const int *ndim, const double b[],
int *n, double x[]);
void Vegas(const int ndim, const int ncomp,
integrand_t integrand, void *userdata,
const double epsrel, const double epsabs,
const int flags, const int seed,
const int mineval, const int maxeval,
const int nstart, const int nincrease, const int nbatch,
const int gridno, const char *statefile,
int *neval, int *fail,
double integral[], double error[], double prob[]);
void llVegas(const int ndim, const int ncomp,
integrand_t integrand, void *userdata,
const double epsrel, const double epsabs,
const int flags, const int seed,
const long long int mineval, const long long int maxeval,
const long long int nstart, const long long int nincrease,
const long long int nbatch,
const int gridno, const char *statefile,
long long int *neval, int *fail,
double integral[], double error[], double prob[]);
void cubasetinit(void (*)(), void *);
void cubasetexit(void (*)(), void *);
void cubaruninit(void);
void cubaruninit(void);
#ifdef __cplusplus
}
#endif
【问题讨论】:
-
你在哪里安装了包的元素?你确认
libcuba.a存在了吗?你可以用nm libcuba.a检查它的内容(看看它是否包含_Vegas)。 -
我将解压后的文件夹 Cuba-3.1 放在 Applications 中,并将 libcuba.a 复制到 demo 文件夹中(它本身包含在 Cuba-3.1 中),其中我还复制了 cuba.h。我跑了
nm libcuba.a,我确实在那里找到了_Vegas。 -
只是一个想法:尝试命令
cc -o demo-c demo-c.c -L. -lcuba -lm(以防在当前目录中没有搜索到库)。 -
将
libcuba.a复制到演示文件夹不一定有效。尝试将其复制到/usr/local/lib并使用-lcuba -lm选项进行编译。
标签: c compiler-errors static-libraries