【发布时间】:2012-12-03 09:39:01
【问题描述】:
我正在尝试使用 rand、srand 和 time 在 C 中生成随机(足够)数字。我使用 DEVC++。我收到以下错误: [链接错误]未定义对“gettimeofday”错误的引用
这是我的代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
static unsigned long next = 1;
int myrand(void) {
next = next * 1103515245 + 12345;
return((unsigned)(next/65536) % 32768);
}
void mysrand(unsigned seed) {
next = seed;
}
struct {
long tv_sec;
long tv_usec;
}timeval ;
int main(){
int num=0; //random number
struct timeval t1;
gettimeofday(&t1, NULL);
srand(t1.tv_usec * t1.tv_sec);
arg_num=rand();
printf("Number of arguments is:%d\n",arg_num);
}
进行在线研究,我发现 DEVC++(不知何故)包含 GNU 编译器,但它并没有真正使用它,导致无法识别所有“通用”函数。 除了解决链接错误之外,我想知道在 Windows 中是否有一个使用 GNU 的 C 编程 IDE,或者不会产生此类问题..
【问题讨论】:
-
对于后者,您可能想选择 Cygwin。
-
windows没有提供
gettimeofday()。做我们自己的,你可能想在这里雕刻:social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/… -
来自 Cygwin 网站:Cygwin 是:为 Windows 提供 Linux 外观和感觉环境的工具集合。Cygwin 不是:在 Windows 上运行本机 Linux 应用程序的方法。
-
您使用 Ecplise for Window 和基于 Cygwin 的 GNU 工具链。这使您可以使用基于 GNU 的源代码构建 Windows 二进制文件。
标签: c windows time ide compiler-errors