English 中文(简体)
无主食h和c99
原标题:unistd.h and c99 on Linux

这个简单的文件:

#include <unistd.h>

void test() {
   char string[40];
   gethostname(string,40);
}

......通常汇编成册时,做的是罚款:

$ cc  -Wall -c -o tmp.o tmp.c
$

......但如果以C99模式汇编,则发出警告:

$ cc -Wall -std=c99 -c -o tmp.o tmp.c 
tmp.c: In function `test :
tmp.c:5: warning: implicit declaration of function `gethostname 
$

由此而来的卷宗是罚款,并连接工程。 我只想摆脱警告。 我可以通过把声明放在我自己的档案中以黑客的方式做到这一点。

What is it about C99 that means the declarations in unistd.h don t get included? Can this be overcome, without giving up the niceness of C99?

在其他标准校准方面,我也看到同样的问题。

最佳回答

您可能需要以微小的方式确定一些宏观,以获得<代码>植被名>的原型。

http://www.un.org。

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

   gethostname(): _BSD_SOURCE || _XOPEN_SOURCE >= 500
   sethostname(): _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)

因此:

#define _BSD_SOURCE

#include <unistd.h>

void test() {
   char string[40];
   gethostname(string,40);
}

详细情况:

如果您没有具体指明<代码>-std-c99备选办法,则features.h (包含在<代码>unistd.h上) 之后,将不设立<代码>_BSD_SOURCE,使<代码>>的原型(<>gethostname(>)包括在内。 但是,具体规定<代码>-std=c99 促使汇编者自动界定<条码> GMICT_ANSI__,这反过来又导致<条码>features.h,即不界定<条码>_BSD_SOURCE,除非您以自己的特点性宏观定义(如上所示)将其强加于。

问题回答

如果您重新使用<代码>gcc,则使用-std=gnu99,并且你照样照搬了你想要的行为。

或者,看<代码><features.h>,似乎你可以使用<代码>-D_GNU_SOURCE或-D_XOPEN_SOURCE=500,以获得预期行为。

http://www.un.org。 专题测试Macro要求中说,_BSD_SOURCE(或_XOPEN_SOURCE>500)要求从无编码的背井中提取姓名。

下面读到<代码>man 特征_test_macros。 页: 1 参看__STRICT_ANSI__,turns off <代码>_BSD_SOURCE。 这意味着,从<代码>unistd.h到<>。 页: 1 我通常把_GNU_SOURCE放在我的指挥线上(即gcc-D_GNU_SOURCE -std=c99 file.c),大部分内容也转载于_BSD_SOURCE

P.S. 手册载有一个实例方案,可以打印目前的偷盗版。 您可以为一些汇编机构汇编和管理。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签