-
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]);
}
}
-
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.