使用Cuba库进行编译时出错 [英] Errors when compiling with the Cuba library

查看:53
本文介绍了使用Cuba库进行编译时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用古巴"库" ,它定义了多维集成例程,并在C程序中使用它们(我通常使用Mathematica,但是对于我现在需要计算的积分,Mathematica存在巨大的收敛性问题).我安装了Cuba-3.1.tar.gz软件包的元素(新"),并尝试编译演示文件demo-cc,但是经过许多小时的尝试,我仍然没有成功(我只学了一点有点C,但是从那以后就再也没有使用过,或者真的没有编译过其他东西,所以我是一个新手),我不确定哪里出了问题.

I am trying to use the "Cuba" "library", which defines multidimensional integration routines, and use those in a program in C (I usually use Mathematica, but for the integrals I need to calculate now, Mathematica has huge convergence problems). I installed the elements of the package Cuba-3.1.tar.gz ("new"), and tried to compile the demo file demo-c.c, but after many many hours of trials, I still have not succeeded (I only learnt a tiny bit of C in school, but have never used it since then, or really compiled anything else, so I am quite a novice), and I am not sure where things are going wrong.

总结:我不确定我的计算机/编译器配置是否正确,我是否使用的命令行不正确,是否存在其他问题,但是古巴几乎没有任何文档,也没有用户论坛

In summary: I am not sure whether my computer/compiler configuration is right, whether I am not using the right command line, or whether there is some other problem, but there is barely any documentation for Cuba, and no user forums.

为清楚起见,让我先分解代码的重要部分,然后再在问题末尾复制整个内容.

For clarity, let me break up the important parts of the code, before copying the whole thing at the end of the question.

其中包括:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "cuba.h"

在cd到同时包含demo-c.c和cuba.h的文件夹中(我将cuba.h复制到了demo文件夹中),然后运行:

In cd'd to the folder containing both demo-c.c and cuba.h (I copied cuba.h into the demo folder), and I then ran:

gcc -o demo-c demo-c.c

我得到的输出是:

Undefined symbols:
"_Vegas", referenced from:
  _main in cc7i6pMP.o
ld: symbol(s) not found

我尝试了其他各种编译命令,这是一个列表:

I tried various other compiling commands, here is a list:

  1. gcc -o demo-c demo-c.c -lcuba -lm
  2. gcc -o demo-c demo-c.c -L/users/username/Applications/Cuba-3.1/demo
  3. gcc -iquote〜/Applications/Cuba-3.1/demo/cuba.h -o demo-c demo-c.c
  4. gcc -I/../../../../Applications/Cuba-3.1/demo -o demo-c demo-c.c
  5. g95 -o -L/../../../../Applications/Cuba-3.1/demo demo-fortran demo-fortran.F
  6. g95 -o demo-fortran demo-fortran.F -L/../../../../Applications/Cuba-3.1/demo
  7. cc -o demo-c demo-c.c -lcuba -lm
  8. cc -o -iquote ../demo-c demo-c.c
  9. gcc -o demo-c demo-c.c -I/./lcuba
  1. gcc -o demo-c demo-c.c -lcuba -lm
  2. gcc -o demo-c demo-c.c -L/users/username/Applications/Cuba-3.1/demo
  3. gcc -iquote ~/Applications/Cuba-3.1/demo/cuba.h -o demo-c demo-c.c
  4. gcc -I/../../../../Applications/Cuba-3.1/demo -o demo-c demo-c.c
  5. g95 -o -L/../../../../Applications/Cuba-3.1/demo demo-fortran demo-fortran.F
  6. g95 -o demo-fortran demo-fortran.F -L/../../../../Applications/Cuba-3.1/demo
  7. cc -o demo-c demo-c.c -lcuba -lm
  8. cc -o -iquote ../ demo-c demo-c.c
  9. gcc -o demo-c demo-c.c -I/./lcuba

和其他一些小的变化,但是得到的错误消息基本相同(对于fortran代码也是如此).请注意,文档说该代码应与第6行完全一样编译:

and some other small variations but got essentially the same error messages (also for the fortran code). Note that the documentation says the code should be compiled with exactly line 6:

cc -o demo-c demo-c.c -lcuba -lm

从互联网上的广泛研究中,我了解到主要"功能肯定存在问题,即:

From extensive research over the internet, I understand that there must be a problem with the "main" function, which is:

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;
}

但是我真的看不到那是什么.确实,拉斯维加斯的所有论点似乎都定义清楚.

but I really cannot see what that is. Indeed, all the arguments in Vegas seem to be well defined.

由于cuba.h文件实际上很小,我尝试将其复制并粘贴到demo-c.c中,然后在main之前出现以下错误:

Since the file cuba.h is actually quite small, I tried copying it and pasting it in demo-c.c, before main, I then got the following error:

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行对应于

Lines 74, 76 and 83 correspond to

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[]);

(这是cuba.h的摘录,我粘贴在demo-c.c中).我尝试过

(this is an excerpt from cuba.h, which I pasted in demo-c.c). I tried with

const double x[10],    double f[10],    and     double x[10]);

但这毫不奇怪地会产生(其他)错误.

but this unsurprisingly produced (other) errors.

我正在使用运行Snow Leopard(10.6.8)和XCode 3.2.6的MacBook(2009年5月-2.4GHz Intel Core Duo 8Gb 1067MHz DDR3).

I am using a MacBook (May 2009 - 2.4GHz Intel Core Duo 8Gb 1067MHz DDR3) running Snow Leopard (10.6.8), with XCode 3.2.6.

#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

推荐答案

尝试 gcc -o demo-c.exe demo-c.c ../libcuba.a 然后您需要执行demo-c.exe

try gcc -o demo-c.exe demo-c.c ../libcuba.a you need then to execute demo-c.exe

./demo-c.exe > demo-c.out

输出应该在演示目录中的文件demo-c.out中

The output should be in the file demo-c.out in the demo directory

这篇关于使用Cuba库进行编译时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆