LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-21-2010, 10:32 AM   #1
Astrodude
LQ Newbie
 
Registered: Jun 2010
Posts: 15

Rep: Reputation: 0
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);
 
Old 07-21-2010, 10:36 AM   #2
harry edwards
Member
 
Registered: Nov 2007
Location: Lincolnshire, UK
Distribution: CentOS, Fedora, and Suse
Posts: 365

Rep: Reputation: 48
Undefined reference means the compiler cannot find the two functions in question. Do you recognise the two functions?
 
Old 07-21-2010, 10:55 AM   #3
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
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];

Last edited by JohnGraham; 07-21-2010 at 10:57 AM.
 
Old 07-23-2010, 02:20 PM   #4
Astrodude
LQ Newbie
 
Registered: Jun 2010
Posts: 15

Original Poster
Rep: Reputation: 0
Thank you both!

Compiles a-ok!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Undefined reference error jacques83 Programming 20 04-11-2013 08:23 AM
undefined reference to error in C++ knobby67 Programming 3 01-19-2008 11:34 PM
Gcj undefined reference main (hello world program) SW-ANIKY Programming 1 12-16-2006 11:08 AM
Undefined reference in C++ program Komakino Programming 3 12-03-2006 05:42 PM
undefined reference error bahadur Programming 5 07-25-2006 10:18 PM


All times are GMT -5. The time now is 12:54 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration