0
所以我有3个文件; main.c中,file.c file.hC extern clock_t变量在文件中未按预期工作;
在file.h我宣布3个变量
extern clock_t start_t, end_t, total_t;
在file.c
我写了一个功能,节省的主要运行程序的时间长度; 和file.h我引用它为“void saveLog(void);”
void saveLog(void)
{
end_t = clock();
total_t = (end_t - start_t);
double time_spent = (double) total_t/CLOCKS_PER_SEC;
double *arr = malloc(sizeof(double));
*arr = time_spent;
FILE* fp = fopen("log.txt","wb");
if (fp)
{
printf("Elapsed: %f seconds\n", (double) time_spent);
fwrite(arr, 1, sizeof(double), fp);
fclose(fp);
}
}
在主我写start_t =时钟开始main.c中(); 并在最后写道:atexit(savelog)
我包括(在所有文件time.h中,文件stdlib.h,stdio.h中)所有的库
编译时出现错误链接苹果ID错误
Undefined symbols for architecture x86_64:
"_end_t", referenced from:
_saveLog in file.o
"_start_t", referenced from:
_check_answer in main.o
_saveLog in file.o
"_total_t", referenced from:
_saveLog in file.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
通过我的想法是,开始计算时钟和主要开始,然后简单地在函数中进行数学计算。 我的问题是,为什么它不起作用?我还应该如何使用clock_t
变量?我尝试了一些与int的测试,似乎被引用就好了。
如果没有[完整独立示例](http://stackoverflow.com/help/mcve),不容易发现什么是错误的。另外:在文本文件中使用''b“'模式和'fwrite()'是很不寻常的。 –
你有变量的声明,但没有定义。 – Barmar
谢谢巴尔玛,我怎么看不到它! –