LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C program - Undefined reference error (https://www.linuxquestions.org/questions/programming-9/c-program-undefined-reference-error-821271/)

Astrodude 07-21-2010 09:32 AM

C program - Undefined reference error
 
Hi All,

I'm building my main program on SUSE using C, but the compiler returns the error below. Any suggestions? Thanks in advance.
Code:

undefined reference to `init_aacgm'
undefined reference to `convert_geo_coord'

Here's my code to which the errors are pointing to:
Code:

#include <stdio.h>
#include <string.h>

#define MAX_FNAME_SZ  200
#define LATEST_IGRF_YEAR  2000   
#define EARLIEST_IGRF_YEAR 1975   

extern int init_aacgm();
extern int convert_geo_coord();

static short int prev_year = 0;


long int cnvcoord_cover(year,in_lat,in_lon,height,out_lat,out_lon,m
short int year;
double in_lat;
double in_lon;
double height;
double *out_lat;
double *out_lon;
short int mgflag;
{
  short int cur_year;
  char filename[MAX_FNAME_SZ];
  long int status;
  int err;
  char year_str[5];
  int order;
  short int mod_year;
  double out_lat_copy;
  double out_lon_copy;
  int int_mgflag;


  /* define constants */
  order = 10; 

  int_mgflag = mgflag;

  status = 0L;
 
  cur_year = year;
  if(cur_year < 50)
    cur_year = cur_year + 2000;

  if(cur_year < 1900)
      cur_year = cur_year + 1900;

  if(cur_year != prev_year) {

   
      if(filename == NULL) {
        fprintf(stderr,
        "aacgm_init_cover: missing environment variable: AACGM_DAT_
        status = -1L;
        return(status);
      }

 
      prev_year = cur_year;

      mod_year = cur_year % 5;

      if(mod_year <= 2)
        cur_year = cur_year - mod_year;
      else
        cur_year = cur_year + 5 - mod_year; 

     
      if(cur_year > LATEST_IGRF_YEAR)
        cur_year = LATEST_IGRF_YEAR;

      if(cur_year < EARLIEST_IGRF_YEAR)

      err = init_aacgm(cur_year);
      if(err != 0) {
        fprintf(stderr,
        "cnvcoord_cover: init_aacgm error\n");
        status = -1L;
        return(status);
      }


  }


    err = convert_geo_coord(in_lat,in_lon,height,&out_lat_copy,
                                    &out_lon_copy,int_mgflag,order)
                                   
    if(err != 0) {
      status = -1L;
      *out_lat = 0.;
      *out_lon = 0.;
  }
  else {
      *out_lat = out_lat_copy;
      *out_lon = out_lon_copy;
  }


  return(status);


harry edwards 07-21-2010 09:36 AM

Undefined reference means the compiler cannot find the two functions in question. Do you recognise the two functions?

JohnGraham 07-21-2010 09:55 AM

This is a problem with the linker - the compiler's come along and successfully turned your C source (.c) file into an object (.o) file, and now the linker's trying to turn your object file(s) into an executable file. I assume the two functions in question are defined by yourself in another file?

You have two options:
  • Compile, but do not link, both files by passing the -c option to gcc, and then link them afterwards:
    Code:

    $ gcc -c file1.c -o file1.o  # compile first file
    $ gcc -c file2.c -o file2.o  # compile second file
    $ gcc file1.o file2.o -o program  # link

  • Get gcc to do the above work for you by just giving them both to gcc to organise compilation and linking:
    Code:

    $ gcc file1.c file2.c -o program  # compile and link in one step

Either way, gcc has to be able to find all your symbols (i.e. see all the relevant object files) at link-time.

[shameless_plug] For more in-depth information, see the first few posts of my rather technical blog. [/shameless_plug];

Astrodude 07-23-2010 01:20 PM

Thank you both!

Compiles a-ok!


All times are GMT -5. The time now is 07:03 PM.