0
#include < stdio.h > 
#include < stdlib.h > 
#include "nrutil.h"
#include "ran1.h"

#define NVAR 2
#define NPAR 6
#define H 0.001
#define FILEIN "par.ini"
#define FILECI "ci.ini"
#define NMAX 1000000
void rk4(double * p, double * x, double * dx, double t,
  void( * deriv)(double * p, double * x, double * dx, double t));
void deriv(double * p, double * x, double * dx, double t);
 int main() {
  double * p, * x, * dx, t, dt, TTOT, DTSAV, seed = 0.0, xx = 0.0, y = 0.0;
  long count;
  int i, j;
  FILE * fp1, * fp2, * fp3, * fp4;
  p = dvector(1, NPAR);
  x = dvector(1, NVAR);
  dx = dvector(1, NVAR);

  if (!(fp1 = fopen(FILEIN, "r"))) nrerror("No se encuentra el archivo par.ini");
  for (i = 1; i <= NPAR; i++) fscanf(fp1, "%lg", & p[i]);
  fscanf(fp1, "%lg %lg ", & TTOT, & DTSAV);
  fclose(fp1);
  printf("Introduce un número: ");
  scanf("%lg", & seed);
  if (seed > 0) {
    seed = -seed;
  }

  for (i = 1; i <= NVAR; i++) {
    xx = ran1( & seed);
    y = xx * 15.0 - 65.0;
    x[1] = y;
  }
  for (j = 1; j <= NVAR; j++) {
    x[2] = 0.2 * x[1];
  }

  t = 0.0;
  count = 0;
  fp1 = fopen("vu.dat", "w");

  while (t <= TTOT && count <= NMAX) {

    fprintf(fp1, "%12.8g ", t);
    for (i = 1; i <= NVAR; i++) {
      fprintf(fp1, "%12.8g ", x[i]);
    }
    fprintf(fp1, "\n");

    dt = 0.0;
    while (dt <= DTSAV) {
      rk4(p, x, dx, t + dt, deriv);
      dt += H;
    }
    t += dt;
    count++;
  }
  fclose(fp1);

  free_dvector(p, 1, NPAR);
  free_dvector(x, 1, NVAR);
  free_dvector(dx, 1, NVAR);
  return 1;
}
void deriv(double * p, double * x, double * dx, double t) {

  dx[1] = 0.04 * x[1] * x[1] + 5 * x[1] + 140 - x[2] + p[1];
  dx[2] = p[2] * (p[3] * x[1] - x[2]);
  if (x[1] >= p[4]) {
    x[1] = p[5];
    x[2] = x[2] + p[6];
  }
  return;
}

void rk4(double * p, double * x, double * dx, double t,
  void( * deriv)(double * p, double * x, double * dx, double t)) {
  int i;
  double * dxm, * dxt, * xt;
  float hh, h6, mu;

  dxm = dvector(1, NVAR);
  dxt = dvector(1, NVAR);
  xt = dvector(1, NVAR);
  hh = H * 0.5;
  h6 = H / 6.0;

  ( * deriv)(p, x, dx, t); /*dx=f(x0)*/
  for (i = 1; i <= NVAR; i++) xt[i] = x[i] + hh * dx[i];
  ( * deriv)(p, xt, dxt, t + hh); /*xt es x1, dxt=f(x1)*/

  for (i = 1; i <= NVAR; i++) xt[i] = x[i] + hh * dxt[i];
  ( * deriv)(p, xt, dxm, t + hh); /*xt es x2, dxm=f(x2)*/

  for (i = 1; i <= NVAR; i++) {
    xt[i] = x[i] + H * dxm[i]; /*xt es x3, dxm=f(x1)+f(x2)*/
    dxm[i] += dxt[i];
  }
  ( * deriv)(p, xt, dxt, t + H); /*dxt=f(x3)*/

  for (i = 1; i <= NVAR; i++)
    x[i] += h6 * (dx[i] + dxt[i] + 2.0 * dxm[i]);

  free_dvector(xt, 1, NVAR);
  free_dvector(dxt, 1, NVAR);
  free_dvector(dxm, 1, NVAR);
  return;
}       

I use to compile with gcc -O3 -o rk4 rk4.c -lm to execute ./rk4 > temp.dat &
The message that I receive is:

Undefined symbols for architecture x86_64:
  "_dvector", referenced from:
      _main in rk4-173408.o
      _rk4 in rk4-173408.o
  "_free_dvector", referenced from:
      _main in rk4-173408.o
      _rk4 in rk4-173408.o
  "_nrerror", referenced from:
      _main in rk4-173408.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
Marila
  • 1
  • 3
  • Welcome to Stack Overflow. Please read the [About] and [Ask] pages soon. You've not given us enough information to be help you. You have a source file `rk4-63c5e9.c` which you've compiled and which calls some functions or variables (`dvector`, `free_dvector` and `nrerror`) that you've not defined. Perhaps you have a second source file and you've not linked that with the first. Perhaps you simply haven't written the functions yet. You need to provide more information before anyone here can help you much more. – Jonathan Leffler Dec 08 '18 at 17:19
  • Possible duplicate of [undefined symbol for architecture x86\_64 in compiling C program](https://stackoverflow.com/questions/29073978/undefined-symbol-for-architecture-x86-64-in-compiling-c-program) – too honest for this site Dec 08 '18 at 17:24

1 Answers1

0

Your code seems to be using libraries from "Numerical Recipes", while -lm option may not link to that library (and I guess -lm should be put before -o option). You may find the binary file or compile from source code yourself the "Numerical Recipes" library, after which the option -L/path/to/the/library should be added to enable the linker find those functions.

Ze Chen
  • 21
  • 5