与古巴图书馆汇编时出错

时间:2013-05-29 07:40:20

标签: c compiler-errors static-libraries

我正在尝试使用定义多维集成例程的"Cuba" "library",并在C程序中使用它们(我通常使用Mathematica,但对于我现在需要计算的积分,Mathematica有很大的收敛问题)。我安装了包古巴-3.1.tar.gz(“新”)的元素,并试图编译演示文件demo-cc,但经过多个小时的试验,我仍然没有成功(我只学到了一点点在学校里有点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

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

  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
  10. 和其他一些小的变化,但基本上得到相同的错误消息(也为fortran代码)。请注意,文档说明代码应该使用第6行编译:

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

    通过对互联网的广泛研究,我了解“主要”功能一定存在问题,即:

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

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

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

    但这不足为奇地产生了(其他)错误。

    系统

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

    完整代码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
    

1 个答案:

答案 0 :(得分:0)

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

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

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

相关问题