Installing the GNU C Library in Windows

M

Matt

Hey guys. I'm currently learnign C and I've been reading on the GNU
website how I should be using the getline() function instead of
"unsafe" functions such as gets(); so to build good habits I wish to
use this from now on.

However I am having difficulty in using it as from what I can gather
it is part of the GNU C Library, for which I can't find any
installation instructions for Windows XP. Now I can appreciate that
many people who are sufficantly advanced with C will be Linux users,
but I'm certainly not either at the moment!

Therefore, how do I install the GNU C Library in Windows?

I tried using a library header I found online (http://www.koders.com/c/
fidC1D809893A833A01A1AAE575A68832643C069C07.aspx) called "getline.h",
however I got errors such as "undefined reference to `getline'" when I
compile using the GNU C Compiler. The header file getline.h is in the
same directory as the code, which is given below:

Kind Regards,

Matt


/* Load function libraries */

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "getline.h"

/* Define constant text expressions */

/* Declare variables and constants */

char *my_string;
int nbytes = 100;
unsigned int bytes_read;

/* Declare functions */

/* Main function */

int main()

{

/* Get programme to read a string from the user */

printf("Please enter a sentence: ");

/* Allocate memory first */

my_string = (char *) malloc (nbytes + 1);
bytes_read = getline(&my_string, &nbytes, stdin);

/* Now check if -1 is returned */

if (bytes_read == -1)

{

printf("\nInsufficient memory!!!");
return 1;

}

else

{

printf("\nYou typed: %s", my_string);

}

return 0;

}
 
P

pete

Matt said:
Hey guys. I'm currently learnign C and I've been reading on the GNU
website how I should be using the getline() function instead of
"unsafe" functions such as gets(); so to build good habits I wish to
use this from now on.
bytes_read = getline(&my_string, &nbytes, stdin);

/* Now check if -1 is returned */

if (bytes_read == -1)

{

printf("\nInsufficient memory!!!");

I have a similar one:

int get_line(char **lineptr, size_t *n, FILE *stream);

http://www.mindspring.com/~pfilandr/C/get_line/get_line.c

The main differences I think, are that get_line returns type int
and returns EOF for end of file and file error,
and returns 0 instead of -1 for "\nInsufficient memory!!!"
 
R

Richard Heathfield

[Followups set to comp.lang.c]

Matt said:
Hey guys.

Hi, Matt. What follows (below) is a comp.lang.c answer, and it's likely
to be very different from the answers I would expect you to get from
gnu.gcc.help - they are likely to give you support in using getline(),
and from their point of view that's the right answer. From a
comp.lang.c point of view, however, it isn't. This isn't a question of
"Us vs Them", just a difference in perspective. The gcc guys are
presumably focused on providing gcc help, whereas in comp.lang.c we
focus more on standard C.

[ Meta: hi, gcc guys - how's it going over there? Have you got
everything you need? Are they feeding you all right? :) ]
I'm currently learnign C and I've been reading on the GNU
website how I should be using the getline() function instead of
"unsafe" functions such as gets(); so to build good habits I wish to
use this from now on.

It is certainly true that gets() is unsafe. Whether getline() is a
suitable replacement depends on how portable you need your code to be.
It is not a standard C function, and implementations are not obliged to
provide it. Indeed, most don't.

There is a standard C function called fgets() which does almost what
gets() does, but with one difference that is designed to make it safe
to use where gets() is not safe, another difference that's a knock-on
effect from the first difference, and a third difference that is
basically a matter of flexibility of purpose - i.e. it gives you extra
functionality compared to gets().

1) fgets takes a size parameter, in which you specify the size of your
input buffer. fgets undertakes not to write more than that many bytes
into your buffer (including the terminating null character);
2) fgets does not remove \n from the string before returning it. Its
purpose is to read a whole line from a text stream, but it will not be
able to achieve this if your buffer isn't as large as the line. To
enable you to tell whether you got a whole line or not, fgets will copy
the newline character into your buffer if it can. So if it's there, you
know you've read the line completely. If not, you know that fgets
bombed out a little early to avoid a buffer overrun, and you can pick
up the rest (or at least the next buffer-sized portion) of the line
with another call to fgets;
3) fgets takes a third parameter, which is a FILE pointer, pointing to a
FILE object that describes a stream open for text input. If you are
replacing gets() with fgets(), you will specify stdin for this
parameter.
However I am having difficulty in using it as from what I can gather
it is part of the GNU C Library,

You will have no difficulty using fgets, I suspect, although I will
freely admit that it isn't quite as flexible as getline.

Anyway, if fgets doesn't cut it for you and you can't manage to install
getline, it's easy to write your own resizeable-buffer input routine.
You may wish to read an article I wrote about all these issues at:

my_string = (char *) malloc (nbytes + 1);

You don't need the cast.

my_string = malloc(nbytes + 1);

If that doesn't compile, you aren't using C.

See <http://www.cpax.org.uk/prg/writings/casting.php>
 
F

Flash Gordon

Matt wrote, On 14/07/07 23:47:
Hey guys. I'm currently learnign C and I've been reading on the GNU
website how I should be using the getline() function instead of
"unsafe" functions such as gets(); so to build good habits I wish to
use this from now on.

Not using gets is good, but getline is non-standard. Fortunately, the
standard provides a function called fgets which is safe from buffer
overflow, but you need to read the documentation and understand it
properly, just as with any function.

Therefore, how do I install the GNU C Library in Windows?

In general, you don't.
I tried using a library header I found online (http://www.koders.com/c/
fidC1D809893A833A01A1AAE575A68832643C069C07.aspx) called "getline.h",
however I got errors such as "undefined reference to `getline'" when I
compile using the GNU C Compiler. The header file getline.h is in the
same directory as the code, which is given below:

<snip>

Headers are not libraries, and in general copying the headers from one
library on to your system is a bad idea.
 
M

Matt

It is certainly true that gets() is unsafe. Whether getline() is a
suitable replacement depends on how portable you need your code to be.
It is not a standard C function, and implementations are not obliged to
provide it. Indeed, most don't.

For portability it needs to work on a machine that has the GNU library
installed, so in that sense getline should not be a problem there. he
problem is at the moment I don't have access to that machine so I need
to install my own copy of the GNU Library.
my_string = (char *) malloc (nbytes + 1);

You don't need the cast.

So I don't :)

Kind Regards,

Matt
 
T

Tim Prince

Matt said:
Is that because it's difficult, or impossible? :)
People have been trying, but not with sufficient success to recommend
it. None of the gcc Windows implementations I am aware of use glibc.
Note that getline() is supported by the newlib library in cygwin gcc.
 
F

Flash Gordon

Matt wrote, On 15/07/07 01:22:
For portability it needs to work on a machine that has the GNU library
installed, so in that sense getline should not be a problem there. he

Ah, but the GNU C library is *not* portable. Most systems have there own
C library which is *not* the GNU library. So unless you basically mean
portable between Linux, Linux and Linux, then I doubt that your
portability requirements is what you state above. Or if it is, then it
is not a portability requirement as you have no requirement for portability.

fgets, on the other hand, is portable to ALL hosted implementations,
i.e. Linux, BSD, Windows, DOS, AIX, SCO etc with all the different
compilers available for all these different systems and many other
systems I have no knowledge of.
problem is at the moment I don't have access to that machine so I need
to install my own copy of the GNU Library.

The easiest way to do that is to install a system that uses the GNU C
library, see comment above about possible systems.
 
M

Matt

The easiest way to do that is to install a system that uses the GNU C
library, see comment above about possible systems.

I do have access to a Linux distribution through the use of Putty and
VNC to a Linux machine at my University. I'll try it now and see if I
get the same errors when I try to compile these files.

Kind Regards,

Matt
 
M

Matt

Some success at last :) I have no problems compiling programmes using
getline() on the Linux machine at my University by using Putty and VNC
Viewer. It looks like I'll be using Plato3 to make the files, then
using the Linux environment to compile and run the programmes. Hardly
an ideal situation so if anyone knows how I could do all this in
Windows using Plato3 I would be very grateful.

Kind Regards,

Matt
 
J

Jens Thoms Toerring

In comp.lang.c Matt said:
Some success at last :) I have no problems compiling programmes using
getline() on the Linux machine at my University by using Putty and VNC
Viewer. It looks like I'll be using Plato3 to make the files, then
using the Linux environment to compile and run the programmes. Hardly
an ideal situation so if anyone knows how I could do all this in
Windows using Plato3 I would be very grateful.

getline() is an extension to be found in the GNU libc. On your
Linux machine you have a GNU libc, but on your Windows machine
you obviously don't, so you also don't seem to have a getline()
function - and just adding a prototype for that function to your
code doesn't help at all since this doesn't magically create a
function you could call. What you need to do is find some re-
placement for it for which you have the source code and you thus
can include it into your program. Others have already pointed out
some examples you could use are:

Richard Heathfield's fgetline:

http://www.cpax.org.uk/prg/writings/fgetdata.php#fgetline

Cuck Falconer's ggets:

http://cbfalconer.home.att.net/download/

Eric Sosman's getline:

http://www.cpax.org.uk/prg/portable/c/libs/sosman/index.php

and there's probably quite a number of other implementations
(see e.g. the end of Richard's web page, there are links to
a few more).
Regards, Jens
 
M

Mark McIntyre

Ah, but the GNU C library is *not* portable. Most systems have there own
C library which is *not* the GNU library.

There's nothing to stop one installing it tho. I have it on my mac,
linux and windows boxes.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top