将头文件链接到C ++中的实现文件 [英] Linking header files to an implementation file in C++

查看:186
本文介绍了将头文件链接到C ++中的实现文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于C ++链接文件的问题。说我有一个名为fmttime.h的头文件,我想链接到fmttime.cc(实现文件)到目前为止我已经做了

I had a particular question regarding linking files in C++. Say I have a header file named fmttime.h and i want to link it to fmttime.cc (Implementation file) heres what I've done so far

 ExpandedTime* localTime(struct timeval* tv, ExpandedTime* etime);
 int main()
 {
     struct timeval tv;
     struct ExpandedTime etime;
     gettimeofday(&tv, NULL);
     localTime(&tv,&etime);

 }

 ExpandedTime* localTime(struct timeval* tv, ExpandedTime* etime)
 {
     tzset(); // Corrects timezone

     int epochT = (tv->tv_sec) - timezone; // Epoch seconds with
     int epochUT = tv->tv_usec;  // Timezone correction

     int seconds = epochT % 60;
     epochT /= 60;
     etime->et_sec = seconds;
     etime->et_usec = epochUT;

     int minutes = epochT % 60;
     epochT /= 60;
     etime->et_min = minutes;

     int hours = (epochT % 24) + daylight; // Hours with DST correction
     epochT /= 24;
     etime->et_hour = hours;

     printf("%d,%d,%d\n", seconds, minutes, hours);
     printf("%d\n", epochUT);
     printf("%d\n", timezone);
     printf("%d\n", daylight);
     return etime;
 }

所以基本上我在头文件中包含了fmttime.h。我对这个过程有几个问题。在fmttime.h中,我所有的是这个函数原型(这是所有的实际需要为我的目的)。

So basically I've included fmttime.h in the header as so. I have a few questions about this whole process. In the fmttime.h all I have is this function prototype (Which is all that is actually needed for my purposes).

 // Interface file for fmttime.h which is including the fmttime.c
 // Contains function prototype

 char* formatTime(struct timeval* tv, char* buf, size_t len);

现在,如果我想在我的fmttime.cc实现文件中使用这个函数,我需要重新声明函数原型?或者可以跳过,因为头文件已经包含它,因此包含在fmttime.cc中,因为有通过#include链接。

Now if I want to use this function in my fmttime.cc implementation file do I need to redeclare the function prototype? Or can it be skipped due to the header file already having it included and thus included in fmttime.cc since there are linked through #include.

所以我基本上想添加到.CC文件char * formatTime(struct timeval * .....),但不知道如果我还需要声明

So I basically want to add into the .CC file char* formatTime (struct timeval*.....) but am not sure if i still need to declare the prototype in the .CC or its taken care of in the fmttime.h file.

推荐答案

#include 一个文件确实是一个文本替换操作。标题的内容只是直接粘贴到包含它的文件中。

#includeing a file is literally a text replacement operation. The contents of the header are just pasted directly into the file which includes it.

所以,你可以自己回答这个问题。试想一下,头文件中的代码实际上是在实现文件中(因为它是)。

So, you can answer the question yourself. Just imagine that the code in the header file is actually in the implementation file as well (because it is).

这篇关于将头文件链接到C ++中的实现文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆