Jim McNeill
2003-09-15 13:16:08 UTC
Good afternoon,
First of all let me apologise for such an utterly green question. I
have tried to find the answer myself on the web, but let's just say
that I lack the perceptual machinery necessary to understand what I've
turned up.
I've opened up a copy of O'Reilly's Unix System Programming, and I'm
trying to run the first example in the book. I have available to me a
system running SCO UnixWare 7.1.1 and gcc 2.95.2 (yes I know these are
a bit out of date, as is the book, but this is a production machine
that I don't want to alter without knowing what I'm doing).
Just trying to compile I get the following:
# gcc bsort-length.c
Undefined first referenced
symbol in file
inputLine /var/tmp/ccrFR1Xi.o
outputLine /var/tmp/ccrFR1Xi.o
UX:ld: ERROR: a.out: fatal error: Symbol referencing errors. No output
written t
o a.out
collect2: ld returned 1 exit status
Now I have managed to produce an object file and tried to link that
instead (by simply running gcc on it), but I get the same error.
Presumably I've got the linker blues. I would like to get beyond this
point and start learning, without becoming a linker expert on the way.
Has anyone got a simple "and with one step he was free" type of
solution for me?
Oh - the program itself:
#include <string.h>
#define NSTRINGS 16 /* max. number of strings */
#define MAXLENGTH 1024 /* max. length of one string */
void bubbleSort(char **, int);
void outputLine(char *);
char *inputLine(void);
int
main(int argc, char **argv)
{
int n, nstrings;
char *p, *q, *line;
char *strptrs[NSTRINGS];
char strings[NSTRINGS][MAXLENGTH];
/*
* Read in NSTRINGS strings from the standard input.
*/
for (nstrings = 0; nstrings < NSTRINGS; nstrings++) {
/*
* Get a line from the input.
*/
if ((line = inputLine()) == NULL)
break;
/*
* Copy the line.
*/
for (p = line, q = strings[nstrings]; *p != '\0'; p++, q++)
*q = *p;
*q = '\0';
/*
* Save a pointer to the line.
*/
strptrs[nstrings] = strings[nstrings];
}
/*
* Sort the strings.
*/
bubbleSort(strptrs, nstrings);
/*
* Print the strings.
*/
for (n = 0; n < nstrings; n++)
outputLine(strptrs[n]);
exit(0);
}
/*
* bubbleSort - implementation of the basic bubble sort algorithm.
*/
void
bubbleSort(char **strings, int nstrings)
{
int i, j;
char *tmp;
int notdone;
j = nstrings;
notdone = 1;
while (notdone) {
notdone = 0;
j = j - 1;
for (i = 0; i < j; i++) {
/*
* Use strlen() to compare the strings
* by length.
*/
if (strlen(strings[i]) > strlen(strings[i+1])) {
tmp = strings[i+1];
strings[i+1] = strings[i];
strings[i] = tmp;
notdone = 1;
}
}
}
}
Regards,
Jim McNeill
First of all let me apologise for such an utterly green question. I
have tried to find the answer myself on the web, but let's just say
that I lack the perceptual machinery necessary to understand what I've
turned up.
I've opened up a copy of O'Reilly's Unix System Programming, and I'm
trying to run the first example in the book. I have available to me a
system running SCO UnixWare 7.1.1 and gcc 2.95.2 (yes I know these are
a bit out of date, as is the book, but this is a production machine
that I don't want to alter without knowing what I'm doing).
Just trying to compile I get the following:
# gcc bsort-length.c
Undefined first referenced
symbol in file
inputLine /var/tmp/ccrFR1Xi.o
outputLine /var/tmp/ccrFR1Xi.o
UX:ld: ERROR: a.out: fatal error: Symbol referencing errors. No output
written t
o a.out
collect2: ld returned 1 exit status
Now I have managed to produce an object file and tried to link that
instead (by simply running gcc on it), but I get the same error.
Presumably I've got the linker blues. I would like to get beyond this
point and start learning, without becoming a linker expert on the way.
Has anyone got a simple "and with one step he was free" type of
solution for me?
Oh - the program itself:
#include <string.h>
#define NSTRINGS 16 /* max. number of strings */
#define MAXLENGTH 1024 /* max. length of one string */
void bubbleSort(char **, int);
void outputLine(char *);
char *inputLine(void);
int
main(int argc, char **argv)
{
int n, nstrings;
char *p, *q, *line;
char *strptrs[NSTRINGS];
char strings[NSTRINGS][MAXLENGTH];
/*
* Read in NSTRINGS strings from the standard input.
*/
for (nstrings = 0; nstrings < NSTRINGS; nstrings++) {
/*
* Get a line from the input.
*/
if ((line = inputLine()) == NULL)
break;
/*
* Copy the line.
*/
for (p = line, q = strings[nstrings]; *p != '\0'; p++, q++)
*q = *p;
*q = '\0';
/*
* Save a pointer to the line.
*/
strptrs[nstrings] = strings[nstrings];
}
/*
* Sort the strings.
*/
bubbleSort(strptrs, nstrings);
/*
* Print the strings.
*/
for (n = 0; n < nstrings; n++)
outputLine(strptrs[n]);
exit(0);
}
/*
* bubbleSort - implementation of the basic bubble sort algorithm.
*/
void
bubbleSort(char **strings, int nstrings)
{
int i, j;
char *tmp;
int notdone;
j = nstrings;
notdone = 1;
while (notdone) {
notdone = 0;
j = j - 1;
for (i = 0; i < j; i++) {
/*
* Use strlen() to compare the strings
* by length.
*/
if (strlen(strings[i]) > strlen(strings[i+1])) {
tmp = strings[i+1];
strings[i+1] = strings[i];
strings[i] = tmp;
notdone = 1;
}
}
}
}
Regards,
Jim McNeill