#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Problems with compiling


    I am trying to compile this but I get:
    gcc -Wall -O4 -funroll-all-loops -fschedule-insns -malign-double -c shotrec2tt.c -o shotrec2tt.o
    shotrec2tt.c: In function ‘main’:
    shotrec2tt.c:39: warning: ‘rfname’ may be used uninitialized in this function
    shotrec2tt.c:39: warning: ‘sfname’ may be used uninitialized in this function
    shotrec2tt.c:43: warning: ‘mag_l’ may be used uninitialized in this function
    shotrec2tt.c:43: warning: ‘mag_md’ may be used uninitialized in this function
    gcc -Wall -O4 -funroll-all-loops -fschedule-insns -malign-double -o shotrec2tt shotrec2tt.o gi_line.o -lm
    shotrec2tt.o: In function `main':
    shotrec2tt.c:(.text+0xb33): undefined reference to `distaz'
    collect2: ld returned 1 exit status
    make: *** [shotrec2tt] Error 1

    this is the code:

    char *progname;

    int
    main(argc, argv)
    int argc;
    char **argv;
    {
    char *rfname, *sfname, string[MAXL];

    int i, j, k, nr, ns, n_ml, verbose, sflg, lflg;

    float *mag_l, *mag_md;
    float vel, s_vel, dist, az, baz, xdeg;

    STN *src, *rec;

    FILE *rf_in, *sf_in;

    int get_line(), distaz();

    /* defaults */
    sflg = 0;
    lflg = 0;
    s_vel = 0.;
    vel = 2500.;
    verbose = 0;
    n_ml = 0;

    /* command line arguments */
    progname = *argv;
    while (--argc > 0)
    {
    if (**(++argv) == '-')
    {
    switch (*(*argv + 1))
    {
    case 'm':
    for (n_ml=1; argc-n_ml > 0 && **(argv+n_ml) != '-'; n_ml++);
    n_ml--;
    n_ml /= 2;
    mag_l = (float *) malloc(n_ml*sizeof(float));
    mag_md = (float *) malloc(n_ml*sizeof(float));
    for (i=0; i<n_ml; i++)
    {
    mag_l[i] = atof(*(argv+i*2+1));
    mag_md[i] = atof(*(argv+i*2+2));
    }
    break;
    case 'r':
    rfname = *(argv + 1);
    break;
    case 's':
    switch (*(*argv + 2))
    {
    case 'l':
    lflg++;
    default:
    sfname = *(argv + 1);
    break;
    }
    break;
    case 'u':
    switch (*(*argv + 2))
    {
    case 's':
    s_vel = atof(*(argv + 1));
    break;
    case 'r':
    s_vel = -atof(*(argv+1));
    break;
    default:
    vel = atof(*(argv + 1));
    break;
    }
    break;
    case 'v':
    verbose++;
    break;
    default:
    fprintf(stderr, "%s: Error in command line.\n", progname);
    case 'H':
    case 'h':
    fprintf(stderr, "%s (V %.1f.%1d) Usage:\n", progname, VER, FIX);
    fprintf(stderr, "%s [-m mag1 lim1 mag2 lim2 ... magn limn] -r receiver.xyz -s[l] shot.xyz (l: in lat lon) -u velocity [-us Vs -ur Vp/Vs -v (verbose)] > 'stdout'\n", progname);
    return (-1);

    } /* close switch. */
    } /* close if */
    } /* close while */

    fprintf(stderr,"%s V %.1f.%1d by %s %s\n", progname, VER, FIX, AUTHOR, DATE);
    sflg = (s_vel ? 1 : 0);
    if (sflg && s_vel < 0)
    s_vel = -vel/s_vel;
    if (verbose)
    {
    fprintf(stderr, "Velocity used: %f", vel);
    if (sflg) fprintf(stderr, " and %f", s_vel);
    fprintf(stderr, "\n");
    fprintf(stderr, "Shot file: %s\nReceiver file: %s\n", sfname, rfname);
    if (n_ml)
    {
    fprintf(stderr, "Magnitude/offset limits: ");
    for (i=0; i<n_ml; i++)
    fprintf(stderr, "%.1f %.1f ", mag_l[i], mag_md[i]);
    fprintf(stderr, "\n");
    }
    }
    /* open input files */
    if ((rf_in = fopen(rfname, "r")) == (FILE *) NULL)
    {
    fprintf(stderr, "%s: Can't open file %s.\n", progname, rfname);
    return (-1);
    }
    if ((sf_in = fopen(sfname, "r")) == (FILE *) NULL)
    {
    fprintf(stderr, "%s: Can't open file %s.\n", progname, sfname);
    return (-1);
    }

    /* count lines in the input files */
    ns = nr = 0;
    while (get_line(rf_in, string, MAXL) != -1)
    {
    if (string[0] == '#' || string[0] == '>')
    continue;
    nr++;
    }
    rewind(rf_in);
    while (get_line(sf_in, string, MAXL) != -1)
    {
    if (string[0] == '#' || string[0] == '>')
    continue;
    ns++;
    }
    rewind(sf_in);
    if (verbose)
    fprintf(stderr,"No of receivers: %d\nNo of shots: %d\n", nr, ns);

    /* allocate memory */
    if ((src = (STN *) malloc(ns*sizeof(STN))) == (STN *) NULL)
    {
    fprintf(stderr,"%s: Couldn't allocate memory for shot.\n", progname);
    return (-1);
    }
    if ((rec = (STN *) malloc(nr*sizeof(STN))) == (STN *) NULL)
    {
    fprintf(stderr,"%s: Couldn't allocate memory for rec.\n", progname);
    return (-1);
    }


    /* read in shots and receiver coordinates */
    for (i=0;get_line(sf_in,string, MAXL) != -1;)
    {
    if (string[0] == '#' || string[0] == '>')
    continue;
    if (n_ml || lflg)
    sscanf(string, "%d %*d %*f %f %f %f %*f %f", &src[i].no, &src[i].y, &src[i].x, &src[i].z, &src[i].mag);
    else
    sscanf(string, "%d %f %f %f", &src[i].no, &src[i].x, &src[i].y, &src[i].z);
    i++;
    }

    for (j=0;get_line(rf_in,string, MAXL) != -1;)
    {
    if (string[0] == '#' || string[0] == '>')
    continue;
    if (lflg)
    sscanf(string, "%d %f %f %f", &rec[j].no, &rec[j].y, &rec[j].x, &rec[j].z);
    else
    sscanf(string, "%d %f %f %f", &rec[j].no, &rec[j].x, &rec[j].y, &rec[j].z);
    j++;
    }
    if (verbose)
    fprintf(stderr,"No of receivers read: %d\nNo of shots read: %d\n", j, i);

    /* output */
    for (i=0; i<ns; i++)
    for (j=0; j<nr; j++)
    {
    if (n_ml)
    {
    distaz(&rec[j].x, &rec[j].y, &dist, &az, &baz, &xdeg, src[i].x, src[i].y, 1);
    for (k=0; k<n_ml; k++)
    if (src[i].mag < mag_l[k])
    break;
    if (k<n_ml && dist > mag_md[k])
    continue;
    fprintf(stdout, "%d %d %.6f", src[i].no, rec[j].no, sqrt(SQR(dist) + SQR(rec[j].z - src[i].z))/vel);
    if (sflg)
    fprintf(stdout, " %.6f", sqrt(SQR(dist) + SQR(rec[j].z - src[i].z))/s_vel);
    fprintf(stdout, "\n");
    }
    else
    {
    fprintf(stdout, "%d %d %.6f", src[i].no, rec[j].no, HDIST(src[i].x, src[i].y, src[i].z, rec[j].x, rec[j].y, rec[j].z)/vel);
    if (sflg)
    fprintf(stdout, " %.6f", HDIST(src[i].x, src[i].y, src[i].z, rec[j].x, rec[j].y, rec[j].z)/s_vel);
    fprintf(stdout, "\n");
    }
    }
    return(1);
    } /* the end */
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Could you please enclose your code with code tags and clearly mark the lines that your compiler is having problems with.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    mag_l
    mag_md
    mag_l mag_md
    Originally Posted by G4143
    Could you please enclose your code with code tags and clearly mark the lines that your compiler is having problems with.
    Of course.
    rfname distaz
    char *progname;

    int
    main(argc, argv)
    int argc;
    char **argv;
    {
    char *rfname, *sfname, string[MAXL];

    int i, j, k, nr, ns, n_ml, verbose, sflg, lflg;

    float *mag_l, *mag_md;
    float vel, s_vel, dist, az, baz, xdeg;

    STN *src, *rec;

    FILE *rf_in, *sf_in;

    int get_line(), distaz();

    /* defaults */
    sflg = 0;
    lflg = 0;
    s_vel = 0.;
    vel = 2500.;
    verbose = 0;
    n_ml = 0;

    /* command line arguments */
    progname = *argv;
    while (--argc > 0)
    {
    if (**(++argv) == '-')
    {
    switch (*(*argv + 1))
    {
    case 'm':
    for (n_ml=1; argc-n_ml > 0 && **(argv+n_ml) != '-'; n_ml++);
    n_ml--;
    n_ml /= 2;
    mag_l = (float *) malloc(n_ml*sizeof(float));
    mag_md = (float *) malloc(n_ml*sizeof(float));
    for (i=0; i<n_ml; i++)
    {
    mag_l[i] = atof(*(argv+i*2+1));
    mag_md[i] = atof(*(argv+i*2+2));
    }
    break;
    case 'r':
    rfname = *(argv + 1);
    break;
    case 's':
    switch (*(*argv + 2))
    {
    case 'l':
    lflg++;
    default:
    sfname = *(argv + 1);
    break;
    }
    break;
    case 'u':
    switch (*(*argv + 2))
    {
    case 's':
    s_vel = atof(*(argv + 1));
    break;
    case 'r':
    s_vel = -atof(*(argv+1));
    break;
    default:
    vel = atof(*(argv + 1));
    break;
    }
    break;
    case 'v':
    verbose++;
    break;
    default:
    fprintf(stderr, "%s: Error in command line.\n", progname);
    case 'H':
    case 'h':
    fprintf(stderr, "%s (V %.1f.%1d) Usage:\n", progname, VER, FIX);
    fprintf(stderr, "%s [-m mag1 lim1 mag2 lim2 ... magn limn] -r receiver.xyz -s[l] shot.xyz (l: in lat lon) -u velocity [-us Vs -ur Vp/Vs -v (verbose)] > 'stdout'\n", progname);
    return (-1);

    } /* close switch. */
    } /* close if */
    } /* close while */

    fprintf(stderr,"%s V %.1f.%1d by %s %s\n", progname, VER, FIX, AUTHOR, DATE);
    sflg = (s_vel ? 1 : 0);
    if (sflg && s_vel < 0)
    s_vel = -vel/s_vel;
    if (verbose)
    {
    fprintf(stderr, "Velocity used: %f", vel);
    if (sflg) fprintf(stderr, " and %f", s_vel);
    fprintf(stderr, "\n");
    fprintf(stderr, "Shot file: %s\nReceiver file: %s\n", sfname, rfname);
    if (n_ml)
    {
    fprintf(stderr, "Magnitude/offset limits: ");
    for (i=0; i<n_ml; i++)
    fprintf(stderr, "%.1f %.1f ", mag_l[i], mag_md[i]);
    fprintf(stderr, "\n");
    }
    }
    /* open input files */
    if ((rf_in = fopen(rfname, "r")) == (FILE *) NULL)
    {
    fprintf(stderr, "%s: Can't open file %s.\n", progname, rfname);
    return (-1);
    }
    if ((sf_in = fopen(sfname, "r")) == (FILE *) NULL)
    {
    fprintf(stderr, "%s: Can't open file %s.\n", progname, sfname);
    return (-1);
    }

    /* count lines in the input files */
    ns = nr = 0;
    while (get_line(rf_in, string, MAXL) != -1)
    {
    if (string[0] == '#' || string[0] == '>')
    continue;
    nr++;
    }
    rewind(rf_in);
    while (get_line(sf_in, string, MAXL) != -1)
    {
    if (string[0] == '#' || string[0] == '>')
    continue;
    ns++;
    }
    rewind(sf_in);
    if (verbose)
    fprintf(stderr,"No of receivers: %d\nNo of shots: %d\n", nr, ns);

    /* allocate memory */
    if ((src = (STN *) malloc(ns*sizeof(STN))) == (STN *) NULL)
    {
    fprintf(stderr,"%s: Couldn't allocate memory for shot.\n", progname);
    return (-1);
    }
    if ((rec = (STN *) malloc(nr*sizeof(STN))) == (STN *) NULL)
    {
    fprintf(stderr,"%s: Couldn't allocate memory for rec.\n", progname);
    return (-1);
    }


    /* read in shots and receiver coordinates */
    for (i=0;get_line(sf_in,string, MAXL) != -1;)
    {
    if (string[0] == '#' || string[0] == '>')
    continue;
    if (n_ml || lflg)
    sscanf(string, "%d %*d %*f %f %f %f %*f %f", &src[i].no, &src[i].y, &src[i].x, &src[i].z, &src[i].mag);
    else
    sscanf(string, "%d %f %f %f", &src[i].no, &src[i].x, &src[i].y, &src[i].z);
    i++;
    }

    for (j=0;get_line(rf_in,string, MAXL) != -1;)
    {
    if (string[0] == '#' || string[0] == '>')
    continue;
    if (lflg)
    sscanf(string, "%d %f %f %f", &rec[j].no, &rec[j].y, &rec[j].x, &rec[j].z);
    else
    sscanf(string, "%d %f %f %f", &rec[j].no, &rec[j].x, &rec[j].y, &rec[j].z);
    j++;
    }
    if (verbose)
    fprintf(stderr,"No of receivers read: %d\nNo of shots read: %d\n", j, i);

    /* output */
    for (i=0; i<ns; i++)
    for (j=0; j<nr; j++)
    {
    if (n_ml)
    {
    distaz(&rec[j].x, &rec[j].y, &dist, &az, &baz, &xdeg, src[i].x, src[i].y, 1);
    for (k=0; k<n_ml; k++)
    if (src[i].mag < mag_l[k])
    break;
    if (k<n_ml && dist > mag_md[k])
    continue;
    fprintf(stdout, "%d %d %.6f", src[i].no, rec[j].no, sqrt(SQR(dist) + SQR(rec[j].z - src[i].z))/vel);
    if (sflg)
    fprintf(stdout, " %.6f", sqrt(SQR(dist) + SQR(rec[j].z - src[i].z))/s_vel);
    fprintf(stdout, "\n");
    }
    else
    {
    fprintf(stdout, "%d %d %.6f", src[i].no, rec[j].no, HDIST(src[i].x, src[i].y, src[i].z, rec[j].x, rec[j].y, rec[j].z)/vel);
    if (sflg)
    fprintf(stdout, " %.6f", HDIST(src[i].x, src[i].y, src[i].z, rec[j].x, rec[j].y, rec[j].z)/s_vel);
    fprintf(stdout, "\n");
    }
    }
    return(1);
    } /* the end */
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,158
    Rep Power
    2222
    No, you did not enclose your formatted code in code tags! Saying that you are and then not doing it is very poor form.

    These are code tags: [code] [/code]

    Write that in your reply and then copy-and-paste your formatted code between them.

    Also, go to the advanced editor and click on the checkbox, "Disable smilies". As you can see, some of your code is being interpreted as smilies, thus rendering that code impossible to read.
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,385
    Rep Power
    1871
    Mmm, is this code you "found" somewhere?

    main(argc, argv)
    int argc;
    char **argv;

    Because this style of function definition has been obsolete for over 20 years.

    > int get_line(), distaz();
    ...
    > gcc -Wall -O4 -funroll-all-loops -fschedule-insns -malign-double -o shotrec2tt shotrec2tt.o gi_line.o -lm
    Since get_line() isn't defined in this source code, I guess it is in gi_line.c

    Is there by any chance any other .c file lying around in the directory?
    Does it by any chance have the definition for distaz() ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  10. #6
  11. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    Originally Posted by salem
    Mmm, is this code you "found" somewhere
    Of course it is. With the attribution

    Code:
    /*	shotrec2tt.c:
     *		Creates a travel time file based on a shot and receiver file 
     *		in xy.....
     *
     *	ari 05-02
     *
     *	V 1.1
     *		Now also includes S-wave travel times.
     *
     *	ari Oct 2009
     *
     *	V 2.0
     *		Allows selection on magnitude distance.
     *
     *	ari Mar 2010
     *
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include "ref_xyz.h"
    
    #define VER 2.0
    #define FIX 0
    #define AUTHOR "Ari Tryggvason"
    #define DATE "100323"
    
    #define MAXL 256
    
    char           *progname;
    removed, naturally.

    Comments on this post

    • salem agrees : Nice detective work
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  12. #7
  13. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    All the "uninitialised" warnings mean what they say - when optimisation is used the compiler can perform a level of abstract execution analysis to prove potential execution paths that result in use of an initialised value. These paths are theoretical and may not actually occur in real execution due to the nature of the input data and semantic mutual exclusivity of apparently independent conditions which is runtime information the compiler does not have.

    Either way initialising the data is the simple solution, if only to suppress the warning, however you should manually analyse the code to determine whether the error path is valid and can occur, and if so whether the default initialisation is adequate.

    With respect to the undefined symbol "distaz", here you have an unusual and frankly lazy function declaration:

    Code:
    int get_line(), distaz();
    but there is no definition of distaz() and where it is called, it takes a number of arguments, that are not explicitly declared.

    Ultimately your attempt to perform such extreme optimisation on this code is probably pointless and ill-advised. The code execution performance will be entirely constrained by the file I/O performance, and no amount of compiler optimisation is likely to make a significant difference. If you were to profile this code you would probably observe that it spends most of its time in library code (particularly the I/O functions) over which you have little control.

    Moreover since you have obviously never had this code compile since it is incomplete, you should not start with optimisation on since that precludes useful and intelligible use of a debugger.

    You'd be better off "modernising" this code, and concentrating on getting it to compile (for which you'll need the missing function definition). I strongly suggest that you switch off the optimisation options and switch on the extended warning levels - I'd recommend -Wall -Werror -Wformat. The code would benefit from fixing any warnings generated.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Thanks Cliford.
    Well code is not mine,I want to use it for some calculations which I need for my Phd thesis. I will follow your advices regarding the optimization.

IMN logo majestic logo threadwatch logo seochat tools logo