Discussion:
Compiling and linking simple C program with GCC on UnixWare
(too old to reply)
Jim McNeill
2003-09-15 13:16:08 UTC
Permalink
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
Larry Rosenman
2003-09-15 16:37:53 UTC
Permalink
Post by Jim McNeill
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).
# 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
The issue is inputLine and outputLine are USER written functions. They
are NOT part of the UnixWare API/ABI.

You need to find the files for those, and compile them as well.

Or, use the functions in <stdio.h> to read/write lines.

Hint: fgets,fputs.

LER
--
Larry Rosenman http://www.lerctr.org/~ler
Phone: +1 972-414-9812 E-Mail: ***@lerctr.org
US Mail: 1905 Steamboat Springs Drive, Garland, TX 75044-6749
Jim McNeill
2003-09-17 08:49:44 UTC
Permalink
It would help, I suppose, if I read the book: "All of the examples in
this chapter assume the existence of two functions that are not part
of the standard C library" . . . and they're actually in the same
directory in iosubs.c. So I may not have that much of a battle with
the linker yet . . . I just have to figure out how to compile shared
code and make it available to the linker.

I know I must sound lazy here, but I'm just trying to avoid complexity
if I can to concentrate on what, let's face it, is pretty unforgiving
stuff. Perhaps you can tell me, am I facing in the right direction? Is
this doable?

Thanks,
Jim
Post by Larry Rosenman
The issue is inputLine and outputLine are USER written functions. They
are NOT part of the UnixWare API/ABI.
You need to find the files for those, and compile them as well.
Or, use the functions in <stdio.h> to read/write lines.
Hint: fgets,fputs.
LER
Larry Rosenman
2003-09-17 14:45:55 UTC
Permalink
Post by Jim McNeill
It would help, I suppose, if I read the book: "All of the examples in
this chapter assume the existence of two functions that are not part
of the standard C library" . . . and they're actually in the same
directory in iosubs.c. So I may not have that much of a battle with
the linker yet . . . I just have to figure out how to compile shared
code and make it available to the linker.
I know I must sound lazy here, but I'm just trying to avoid complexity
if I can to concentrate on what, let's face it, is pretty unforgiving
stuff. Perhaps you can tell me, am I facing in the right direction? Is
this doable?
cc -O -o testing file.c iosubs.c

LER
Post by Jim McNeill
Thanks,
Jim
Post by Larry Rosenman
The issue is inputLine and outputLine are USER written functions. They
are NOT part of the UnixWare API/ABI.
You need to find the files for those, and compile them as well.
Or, use the functions in <stdio.h> to read/write lines.
Hint: fgets,fputs.
LER
--
Larry Rosenman http://www.lerctr.org/~ler
Phone: +1 972-414-9812 E-Mail: ***@lerctr.org
US Mail: 1905 Steamboat Springs Drive, Garland, TX 75044-6749
Jim McNeill
2003-09-21 07:43:45 UTC
Permalink
Larry - thank you very much!

It musn't seem like much to you, but now I can get ahead with all the
examples in the book, and that great lumbering brute of a server
sitting in the corner running UnixWare now becomes amenable to reason!
Uistekend!

Perhaps I should get out more . . .

Jim
Post by Larry Rosenman
Post by Jim McNeill
It would help, I suppose, if I read the book: "All of the examples in
this chapter assume the existence of two functions that are not part
of the standard C library" . . . and they're actually in the same
directory in iosubs.c. So I may not have that much of a battle with
the linker yet . . . I just have to figure out how to compile shared
code and make it available to the linker.
I know I must sound lazy here, but I'm just trying to avoid complexity
if I can to concentrate on what, let's face it, is pretty unforgiving
stuff. Perhaps you can tell me, am I facing in the right direction? Is
this doable?
cc -O -o testing file.c iosubs.c
LER
Larry Rosenman
2003-09-21 14:58:16 UTC
Permalink
Post by Jim McNeill
Larry - thank you very much!
It musn't seem like much to you, but now I can get ahead with all the
examples in the book, and that great lumbering brute of a server
sitting in the corner running UnixWare now becomes amenable to reason!
Uistekend!
I **DO** understand. I've learned an AWFUL LOT from folks on the net
over the last 15-20 years. This is my way of repaying it.

Go enjoy!

LER
Post by Jim McNeill
Perhaps I should get out more . . .
That's a whole different matter!

LER
Post by Jim McNeill
Jim
Post by Larry Rosenman
Post by Jim McNeill
It would help, I suppose, if I read the book: "All of the examples in
this chapter assume the existence of two functions that are not part
of the standard C library" . . . and they're actually in the same
directory in iosubs.c. So I may not have that much of a battle with
the linker yet . . . I just have to figure out how to compile shared
code and make it available to the linker.
I know I must sound lazy here, but I'm just trying to avoid complexity
if I can to concentrate on what, let's face it, is pretty unforgiving
stuff. Perhaps you can tell me, am I facing in the right direction? Is
this doable?
cc -O -o testing file.c iosubs.c
LER
--
Larry Rosenman http://www.lerctr.org/~ler
Phone: +1 972-414-9812 E-Mail: ***@lerctr.org
US Mail: 1905 Steamboat Springs Drive, Garland, TX 75044-6749
Loading...