I'm learning C via Sam's Teach Yourself C for Linux Programming in 21 Years...
I mean Days.
I typed out the below code on my laptop and compiled it three times with GCC with the following command $gcc -Wall sort.c -o sort.
This error came back each time (sort of):
/home/casper/tmp/ccox4j2H.o(.text+0x62): In function `main':
: undefined reference to `print_strings'
collect2: ld returned 1 exit status
My working directory was /home/casper/CStuff/lessons
I looked in /home/casper/tmp using $ls -a and could find no file called ccox4jH.o(.text+0x62)
I went back over the code three times and could find nothing different between what I had typed out and what was in the book.
Each time I recompiled the sort.c document the value at the end of the error
string /home/casper/tmp showed a different string- though the format and the remainder of the string appeared the same.
I recreated the sort.c file on my desktop and compiled it. It worked and did
what the book said it was supposed to do.
I printed the sort.c document from my desktop and compared it to the sort.c
document from my laptop line by line character by character. No difference!
What on earth is going on???
here is the code from my desktop (the one that compiled correctly):
/*inputs a list of strings from the keyboard, sorts them,*/
/*and then displays them on the screen*/
#define MAXLINES 25
int get_lines(char *lines);
void sort(char *p, int n);
void print_strings(char *p, int n);
/*read in the lines from the keyboard*/
number_of_lines = get_lines(lines);
if ( number_of_lines < 0)
puts("Memory allocation error");
int get_lines(char *lines)
int n = 0, slen;
char buffer; /*temporary storage for each line*/
puts("Enter one line at a time; enter a blank line when done.");
while ((n < MAXLINES) && (fgets(buffer, 80,stdin) != 0))
slen = strlen (buffer);
if (slen < 2)
buffer [slen-1] = 0;
if ((lines[n] = (char *)malloc(strlen(buffer)+1)) == NULL)
strcpy( lines[n++], buffer );
} /*end of get_lines()*/
void sort(char *p, int n)
int a, b;
for (a=1; a<n; a++)
for (b = 0; b < n-1; b++)
if (strcmp(p[b], p[b+1]) > 0)
x = p[b];
p[b] = p[b+1];
p[b+1] = x;
void print_strings(char *p, int n)
for (count = 0; count < n; count++)