将头文件链接到C++中的实现文件
Linking header files to an implementation file in C++
我有一个关于在C++中链接文件的特殊问题。假设我有一个名为fmttime.h的头文件,我想将它链接到fmttime.cc(实现文件(,这就是我迄今为止所做的
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,%dn", seconds, minutes, hours);
printf("%dn", epochUT);
printf("%dn", timezone);
printf("%dn", daylight);
return etime;
}
所以基本上我已经在标题中包含了fmttime.h。我对整个过程有一些问题。在fmttime.h中,我只有这个函数原型(这是我实际需要的全部(。
// 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链接了它。
所以我基本上想添加到。CC文件char*formatTime(struct-timeval*…..(,但不确定是否仍需要在中声明原型。CC或其在fmttime.h文件中处理。
#include
对文件进行替换实际上是一种文本替换操作。标题的内容只是直接粘贴到包含它的文件中。
所以,你可以自己回答这个问题。想象一下,头文件中的代码实际上也在实现文件中(因为它是(。
头文件仅#包含在源文件中。它们没有链接。在处理了预处理器指令(如#include(后,编译器会将源代码编译到对象文件(.o(中。然后,将对象文件传递给链接器进行链接。
对于您的最后一个问题,我建议您尝试编译您的。不包含#include指令的CC文件,然后#include头文件并再次编译。
记住:
.h/hpp->函数原型、变量/结构/枚举定义
.c/.cpp->函数实现(.c/.cpp需要包含其相应的头(
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 读取文件的最后一行并输入到链接列表时出错
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 如何将内容数组写入文本文件?
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到