Errors when compiling with the Cuba library

↘锁芯ラ 提交于 2021-01-28 01:40:39

问题


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.

The includes are:

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

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

The output I got was:

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

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.

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

Lines 74, 76 and 83 correspond to

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

in

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

(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.

System

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.

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

Full code 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

回答1:


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

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



来源:https://stackoverflow.com/questions/16808115/errors-when-compiling-with-the-cuba-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!