LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 656|回复: 7

有关include的问题

[复制链接]
发表于 2007-3-21 21:06:19 | 显示全部楼层 |阅读模式
最近开始学习linux,在学书上的一个程序时产生这个问题.
我在include了一个time.h文件后使用tm这个数据结构,结果编译出错,说tm没有定义.我查了文件,发现很多目录下有time.h文件.有的确实没有tm这个数据结构.请问我们在程序中include进来的到底是哪个目录下的?我要怎样才能用到有tm定义的time.h文件?
发表于 2007-3-21 21:20:57 | 显示全部楼层
可能你要用  #include<time.h>来包含试试
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-3-21 21:24:25 | 显示全部楼层
我就是这么写的阿,书上也是这么写的,可就是不能编译
回复 支持 反对

使用道具 举报

发表于 2007-3-21 21:37:10 | 显示全部楼层
帖出代码吧.

另外声明 tm 型的变量时, 楼主是用下面哪一种方法?

  1. tm a;
  2. struct tm b;
复制代码

a 是错的, b 是正确的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-3-21 21:39:46 | 显示全部楼层
代码如下,请各位看看:
#include <time.h>
#include <stdio.h>
                                                                                                                                               
int main()
{
        struct tm  *tm_ptr;
        time_t the_time;
                                                                                                                                               
        (void) time(&the_time);
        tm_ptr = gmtime(&the_time);
                                                                                                                                               
        printf("Raw time is %ld\n",the_time);
        printf("gmtime gives:\n");
        printf("date:%02d/%02d/%02d\n",tm_ptr->tm_year,tm_ptr->tm_mon+1,tm_ptr->tm_mday);
        printf("time:%02d:%02d:%02d\n",tm_ptr->tm_hour,tm_ptr->tm_min,tm->tm_sec);
        exit(0);
}
回复 支持 反对

使用道具 举报

发表于 2007-3-21 22:31:37 | 显示全部楼层
楼主注意看编译时的错误信息, 它会告诉你错误出在哪一行, 自己在源程序里数到那一行, 自己看看它提示的 tm 未定义究竟是什么意思.

不要想当然地认为错误是如何发生的, 要经过思考
回复 支持 反对

使用道具 举报

发表于 2007-3-22 11:22:28 | 显示全部楼层
Post by fxb248
代码如下,请各位看看:
#include <time.h>
#include <stdio.h>
                                                                                                                                               
int main()
{
        struct tm  *tm_ptr;
        time_t the_time;
                                                                                                                                               
        (void) time(&the_time);
        tm_ptr = gmtime(&the_time);
                                                                                                                                               
        printf("Raw time is %ld\n",the_time);
        printf("gmtime gives:\n");
        printf("date:%02d/%02d/%02d\n",tm_ptr->tm_year,tm_ptr->tm_mon+1,tm_ptr->tm_mday);
        printf("time:%02d:%02d:%02d\n",tm_ptr->tm_hour,tm_ptr->tm_min,tm->tm_sec);
        exit(0);
}


这是Beginning Linux Programming上的例程代码,应该没有错误,除非楼主打字大错喽!!
回复 支持 反对

使用道具 举报

发表于 2007-3-22 11:26:48 | 显示全部楼层
#include <time.h>
#include <stdio.h>

int main()
{
    struct tm *tm_ptr;
    time_t the_time;

    (void) time(&the_time);
    tm_ptr = gmtime(&the_time);

    printf("Raw time is %ld\n", the_time);
    printf("gmtime gives:\n");
    printf("date: %02d/%02d/%02d\n",
        tm_ptr->tm_year, tm_ptr->tm_mon+1, tm_ptr->tm_mday);
    printf("time: %02d:%02d:%02d\n",
        tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec);
    exit(0);
}
贴出书上的代码, 编译后没有错误,楼主自己比较吧,看看哪个地方有出入。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表