在Windows中安装GNU C库 [英] Installing the GNU C Library in Windows

查看:73
本文介绍了在Windows中安装GNU C库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们。我目前正在学习C并且我一直在阅读GNU

网站上我应该如何使用getline()函数而不是

" unsafe"诸如gets()之类的函数;所以为了养成良好的习惯,我希望从现在开始使用这个



但是我很难使用它,因为我可以收集它/>
它是GNU C库的一部分,我找不到任何适用于Windows XP的

安装说明。现在我很欣赏

很多C级人员都是Linux用户,

但是我当然不是现在!


因此,如何在Windows中安装GNU C库?


我尝试使用我在网上找到的库头( http://www.koders.com/c/

fidC1D809893A833A01A1AAE575A68832643C069C07.aspx)叫做getline.h,

然而我得到的错误包括未定义引用`getline''当我使用GNU C编译器编译
时。头文件getline.h位于

相同的目录中,代码如下:


亲切的问候,

Matt

/ *加载函数库* /


#include< stdio.h>

# include< math.h>

#include< string.h>

#include< stdlib.h>

#include" ; getline.h"


/ *定义常量文本表达式* /


/ *声明变量和常量* /


char * my_string;

int nbytes = 100;

unsigned int bytes_read;


/ *声明函数* /


/ *主要功能* /


int main()


{


/ *获取程序以读取用户的字符串* /


printf(请输入句子:) ;


/ *首先分配内存* /

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

bytes_read = getline (& my_string,& nbytes,stdin);


/ *现在检查是否返回-1 * /


if(bytes_read == -1)


{


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

返回1;


}


其他


{


printf(" \ n您输入:%s",my_string);


}


返回0 ;


}

解决方案

Matt写道:


>

嘿伙计们。我目前正在学习C并且我一直在阅读GNU

网站上我应该如何使用getline()函数而不是

" unsafe"诸如gets()之类的函数;所以为了养成良好的习惯我希望从现在开始使用这个


bytes_read = getline(& my_string,& nbytes,stdin);


/ *现在检查是否返回-1 * /


if(bytes_read == -1)


{


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



我有一个类似的:


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

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


我认为主要区别在于get_line返回类型int

并返回EOF以查找文件和文件错误,

并返回0而不是-1表示\ nInsufficient memory !!!


-

pete


[后续设置为comp.lang.c]


马特说:


嘿伙计们。



马特。接下来(下面)是一个comp.lang.c的答案,而且很可能

与我希望你得到的答案非常不同

gnu.gcc.help - 他们可能会给你支持使用getline(),

,从他们的观点来看,这是正确的答案。但是,从

comp.lang.c的角度来看,它并非如此。这不是一个问题

Us vs Them,只是视角的差异。 gcc家伙可能是b $ b,大概是专注于提供gcc帮助,而在comp.lang.c中,我们

更多地关注标准C.


[Meta:gcc伙计们 - 那里怎么样?你有什么需要的吗?他们喂你的好吗? :-)]


我目前正在学习C并且我一直在阅读GNU

网站我应该如何使用getline()函数代替

" unsafe"诸如gets()之类的函数;所以为了养成良好的习惯我希望从现在开始使用这个



get()肯定是不安全的。 getline()是否适合替换取决于你需要代码的可移植性。

它不是标准的C函数,并且实现没有义务

提供它。确实,大多数都没有。


有一个标准的C函数,叫做fgets(),几乎可以实现

gets(),但有一个旨在使其安全的差异

在get()不安全的地方使用,另一个差异就是从第一个差异开始敲击

效果,以及第三个区别是

基本上是目的灵活性的问题 - 也就是说,与gets()相比,它给你额外的

功能。


1)fgets采用size参数,在其中指定

输入缓冲区的大小。 fgets承诺不会在你的缓冲区中写入超过那么多的字节

(包括终止空字符);

2)fgets不会从字符串中删除\ n归还它。它的目的是从文本流中读取整行,但如果你的缓冲区不像行一样大,那么它将无法实现这一点。 。为了让你知道你是否有一整行,fgets会将新行字符复制到你的缓冲区中,如果可以的话。所以如果它在那里,你就知道你已经完全读完了这条线。如果没有,你知道fgets

提前一点轰炸以避免缓冲区溢出,你可以选择

其余的(或至少下一个缓冲区大小)线路的一部分

再次调用fgets;

3)fgets接受第三个参数,这是一个FILE指针,指向一个

FILE对象,描述为文本输入打开的流。如果你是用bgets()替换gets(),你将为这个

参数指定stdin。


但是我很难使用它,因为我可以收集它

它是GNU C库的一部分,



你我怀疑,使用fgets会没有任何困难,虽然我会免费承认它不像getline那么灵活。


无论如何,如果fgets并没有为你削减它,你也无法安装

getline,你可以很容易地编写自己的可调整缓冲区输入例程。

您可能希望阅读我撰写的关于所有这些问题的文章:


< http://www.cpax.org.uk/prg/writings/fgetdata.php>


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



你不需要演员。


my_string = malloc(nbytes + 1);


如果没有编译,你就不会使用C.


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


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - 1999年7月29日dmr


>


因此,如何在Windows中安装GNU C库?



一般来说,你不是。



这是因为它很难或不可能? :)


标题不是库,通常将标题从一个

库复制到您的系统上是一个坏主意。



有道理。


亲切的问候,


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;

}

解决方案

Matt wrote:

>
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/...ine/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!!!"

--
pete


[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:

<http://www.cpax.org.uk/prg/writings/fgetdata.php>

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>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


>

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


In general, you don''t.

Is that because it''s difficult, or impossible? :)

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

Makes sense.

Kind Regards,

Matt


这篇关于在Windows中安装GNU C库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆