Thread: Undefined Reference

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Undefined Reference

    When I try to makefile my C programs, I get the message "Undefined reference: 'read_line' in main()". it also does this for out_lines. Can someone tell me why it's doing this?

    makefile:

    sort: sort.o lines.o
    gcc -std=c99 -o lines.o sort.o

    sort.o: sort.c lines.h constants.h
    gcc -c -std=c99 sort.c

    lines.o: lines.c constants.h lines.h
    gcc -c -std=c99 lines.c

    sort.c:
    Code:
    #include "constants.h"
    #include "lines.h"
    #include <stdio.h>
    #include <string.h>
    int main(){
      char lines[MAX_ROWS][MAX_COLUMNS];
      char temp[MAX_COLUMNS];
      char end;
      int i=0,j=0;
      for(i;i<MAX_ROWS;i++){
        read_line(lines);
        if(i>0){
          if(strcmp(lines[i],lines[i-1])<0){
     strcpy(temp,lines[i]);
            strcpy(lines[i],lines[i-1]);
            strcpy(lines[i-1],temp);
          }
        }
        i++;
      }
      out_lines(lines);
      return(0);
    }
    lines.h:
    Code:
    #ifndef MAX_COLUMNS 80
    #define MAX_COLUMNS 80
    void read_line(char lines[][MAX_COLUMNS]);
    void out_lines(char lines[][MAX_COLUMNS]);
    #endif
    constants.h:
    Code:
    #ifndef MAX_COLUMNS
    #ifndef MAX_ROWS
    #define MAX_COLUMNS 80
    #define MAX_ROWS 30
    #endif
    lines.c:
    Code:
    #include "lines.h"
    #include "constants.h"
    #include <stdio.h>
    void read_line(char lines[][MAX_COLUMNS]){
      int ch, i=0;
      while((ch=getchar()) != '\n')
        if(i<MAX_COLUMNS)
          lines[0][i++] = ch;
      lines[0][i] = '\0';
    }
      
    void out_lines(char lines[][MAX_COLUMNS]){
      for(int i=0;i<MAX_ROWS;i++){
        for(int j=0;j<MAX_COLUMNS;j++)
          printf("%c",&lines[i][j]);
      }
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Code:
    sort: sort.o lines.o
    gcc -std=c99 -o lines.o sort.o
    take a look at what the -o argument does

    -o file
    Place output in file file. This applies regardless to whatever
    sort of output is being produced, whether it be an executable file,
    an object file, an assembler file or preprocessed C code.

    If -o is not specified, the default is to put an executable file in
    a.out, the object file for source.suffix in source.o, its assembler
    file in source.s, a precompiled header file in source.suffix.gch,
    and all preprocessed C source on standard output.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. : undefined reference to `pow'
    By mohitsaxena019 in forum C Programming
    Replies: 13
    Last Post: 07-25-2010, 08:35 AM
  2. Undefined Reference
    By Russell in forum C++ Programming
    Replies: 7
    Last Post: 06-02-2004, 02:08 AM
  3. undefined reference
    By siubo in forum C Programming
    Replies: 1
    Last Post: 04-11-2003, 05:34 AM
  4. Undefined Reference
    By mmondok in forum C Programming
    Replies: 0
    Last Post: 02-18-2003, 12:15 PM
  5. undefined reference
    By laasunde in forum C++ Programming
    Replies: 6
    Last Post: 10-23-2002, 11:44 AM