简体   繁体   English

为什么在设置-std = c99时gcc不能找到random()接口?

[英]Why can't gcc find the random() interface when -std=c99 is set?

I do "#include <stdlib.h>" at the top of the source. 我在源代码的顶部做"#include <stdlib.h>"

Example compilation: 示例编译:

/usr/bin/colorgcc -std=c99 -fgnu89-inline  -g -Wall -I/usr/include -I./ -I../ -I../../ -I../../../ -I../../../../    -O3 -o f8  f8.c
In file included from f8.c:7:
ctype-cmp.c: In function ‘randomized’:
ctype-cmp.c:48: warning: implicit declaration of function ‘random’
ctype-cmp.c: In function ‘main’:
ctype-cmp.c:153: warning: implicit declaration of function ‘srandom’
ais@xcalibur:t$ 

When I turn off -std=c99, the function isfinite() can not be found. 当我关闭-std = c99时,无法找到函数isfinite()。 So I do want to use -std=c99 for this and other reasons. 所以我确实想要使用-std = c99这个和其他原因。 Is there some trick I'm missing? 有什么技巧我不见了?

man srandom says that the function is not part of C99 but part of POSIX. man srandom说该函数不是 C99的一部分,而是POSIX的一部分。

Activate _BSD_SOURCE or _XOPEN_SOURCE >= 500 or any other suitable feature test macro that declares the srandom/random function (see man feature_test_macros and man srandom ). 激活_BSD_SOURCE_XOPEN_SOURCE >= 500或声明srandom / random函数的任何其他合适的功能测试宏(请参阅man feature_test_macrosman srandom )。

This one has good chances, but you need to figure out the macros that are defined/not defined implicitly thereby too by reading the manpages above. 这个有很好的机会,但你需要通过阅读上面的联机帮助页找出隐式定义/未定义的宏。

/usr/bin/colorgcc -std=c99 -D_XOPEN_SOURCE=600 -fgnu89-inline -g -Wall 
    -I/usr/include -I./ -I../ -I../../ -I../../../ -I../../../../ -O3 -o f8  f8.c

Yes, there is a trick you are missing: you can use -std=gnu99 instead of -std=c99 . 是的,有一个缺少的技巧:你可以使用-std=gnu99而不是-std=c99

-std=c99 #define s __STRICT_ANSI__ , which /usr/include/features.h interprets as "do not enable anything outside the C standard by default" (without it, you get at least both _SVID_SOURCE and _BSD_SOURCE ). -std=c99 #define s __STRICT_ANSI__ ,其中/usr/include/features.h解释为“默认情况下不启用C标准之外的任何内容”(没有它,你至少得到_SVID_SOURCE_BSD_SOURCE )。 -std=gnu99 , on the other hand, means "C99 plus GNU extensions" (the gcc default is currently -std=gnu89 , its C89 equivalent, which is why you needed to specify something to get the new C99 features). 另一方面, -std=gnu99意味着“C99加上GNU扩展”(gcc默认目前是-std=gnu89 ,它的C89等价,这就是为什么你需要指定一些东西来获得新的C99功能)。

As an alternative, you can enable the feature test macros (as mentioned in @litb's answer). 作为替代方案,您可以启用功能测试宏(如@ litb的答案中所述)。 Looking at /usr/include/stdlib.h in my system, it expects one of __USE_SVID , __USE_XOPEN_EXTENDED , or __USE_BSD . 在我的系统中查看/usr/include/stdlib.h ,它需要__USE_SVID__USE_XOPEN_EXTENDED__USE_BSD /usr/include/features.h tells me that the feature test macros which would enable these are: /usr/include/features.h告诉我,启用它们的功能测试宏是:

  • _SVID_SOURCE (enables __USE_SVID ) _SVID_SOURCE (启用__USE_SVID
  • _BSD_SOURCE (enables __USE_BSD ) _BSD_SOURCE (启用__USE_BSD
  • _XOPEN_SOURCE with a value of at least 500 (enables __USE_XOPEN_EXTENDED ) _XOPEN_SOURCE ,其值至少为500 (启用__USE_XOPEN_EXTENDED
  • _XOPEN_SOURCE_EXTENDED (also enables __USE_XOPEN_EXTENDED ) _XOPEN_SOURCE_EXTENDED (也启用__USE_XOPEN_EXTENDED
  • _GNU_SOURCE (enables everything, including the four feature test macros above) _GNU_SOURCE (启用所有功能,包括上面的四个功能测试宏)

For new programs where you are not too concerned about potential name collisions with new functions from future standards, using both -std=gnu99 and -D_GNU_SOURCE is a good idea. 对于您不太关注未来标准中新功能的潜在名称冲突的新程序,使用-std=gnu99-D_GNU_SOURCE是一个好主意。 It allows you to use all the new standard features and GNU extensions, which combined with some sort of fallback (for instance, autoconf -style feature tests) gives the most flexibility. 它允许您使用所有新的标准功能和GNU扩展,结合某种后备(例如, autoconf风格的功能测试)提供最大的灵活性。

References: 参考文献:

I've created random numbers using gcc in CodeBlocks under Ubuntu 9.10 (with compiler options: -std=gnu99 -D_GNU_SOURCE ) So this worked for me: 我在Ubuntu 9.10下的CodeBlocks中使用gcc创建了随机数(带编译器选项: -std=gnu99 -D_GNU_SOURCE )所以这对我-std=gnu99 -D_GNU_SOURCE

This is my code I had played with: 这是我玩过的代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
enum computer {keyboard, CPU, screen, printer};
int main(void)
{
  enum computer comp;
  time_t casovac;
  comp = CPU;
  srand(&casovac);
  printf("%d", rand());
  return 0;
}

This was only idea, of course you can accomplish it by other ways ;-) [To install CodeBlocks use: sudo apt-get install build-essential and then sudo apt-get install codeblocks ] 这只是一个想法,当然你可以通过其他方式完成它;-) [安装CodeBlocks使用: sudo apt-get install build-essential然后sudo apt-get install codeblocks ]

I use rand() and srand(). 我使用rand()和srand()。 BTW: Did you forget a header or two? 顺便说一句:你有没有忘记一两个头? At least the second warning tells me so. 至少第二次警告告诉我。

Try including math.h. 尝试包括math.h. (Just remembered we always had issues with math library and had to actually force link it with -lm). (记得我们总是遇到数学库的问题,不得不用-lm强制链接它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM