繁体   English   中英

如何将其分为头文件和源文件?

[英]How to split this into header and source files?

我有一些C代码,我想分为头文件和源文件:

#ifndef BENCHMARK_H
#define BENCHMARK_H

#ifdef WIN32
#include <windows.h>

double get_time()
{
    LARGE_INTEGER t, f;
    QueryPerformanceCounter(&t);
    QueryPerformanceFrequency(&f);
    return (double)t.QuadPart/(double)f.QuadPart;
}

#else

#include <sys/time.h>
#include <sys/resource.h>

double get_time()
{
    struct timeval t;
    struct timezone tzp;
    gettimeofday(&t, &tzp);
    return t.tv_sec + t.tv_usec*1e-6;
}

#endif

#endif

生成的benchmark.hbenchmark.c的正确格式是什么?

我知道头文件应包含函数声明 ,而源文件应位于实际函数定义所在的位置。 下面的代码正确吗? 即, #ifdef WIN32指令是否应该同时存在于两个文件中? 还是应该全部放在.c文件中?

benchmark.h

#ifndef BENCHMARK_H
#define BENCHMARK_H

    #ifdef WIN32
        #include <windows.h>
    #else
        #include <sys/time.h>
        #include <sys/resource.h>
    #endif

    double get_time();

#endif

benchmark.c

#ifdef WIN32

    double get_time()
    {
        LARGE_INTEGER t, f;
        QueryPerformanceCounter(&t);
        QueryPerformanceFrequency(&f);
        return (double)t.QuadPart/(double)f.QuadPart;
    }

#else

    double get_time()
    {
        struct timeval t;
        struct timezone tzp;
        gettimeofday(&t, &tzp);
        return t.tv_sec + t.tv_usec*1e-6;
    }

#endif

头文件和ac文件一起构成一个“代码模块”(或者,如果您将:ADT,类等)。

头文件始终被视为代码的用户界面 ,其中“用户”是将使用您的模块的程序员。 它绝不包含任何代码或变量定义,句点。

尽管c文件包含实际的实现,但对于用户而言这并不重要,因此他们不应该关心它们。 c文件应使用专用封装,并且用户不需要的所有内容都应在该文件中。

以上是设计C程序或任何语言的任何程序的方式。 这不是主观的,不是基于观点的,这是唯一的方法。 如果您以不同的方式进行程序设计,那么您做错了。


对于您的特定程序,应按以下方式进行设计:

benchmark.h

#ifndef BENCHMARK_H
#define BENCHMARK_H

    double get_time (void);
    /* documentation about how this function is used should be put here */

#endif

benchmark.c

#include "benchmark.h"

 /*** Include files ***/
#ifdef WIN32
    #include <windows.h>
#else
    #include <sys/time.h>
    #include <sys/resource.h>
#endif

/*** Other stuff, for example constants, typedefs, static file scope variables ***/


/*** function definitions ***/

#ifdef WIN32

    double get_time (void)
    {
        LARGE_INTEGER t, f;
        QueryPerformanceCounter(&t);
        QueryPerformanceFrequency(&f);
        return (double)t.QuadPart/(double)f.QuadPart;
    }

#else

    double get_time (void)
    {
        struct timeval t;
        struct timezone tzp;
        gettimeofday(&t, &tzp);
        return t.tv_sec + t.tv_usec*1e-6;
    }

#endif

请注意, double get_time()在C语言中表示“可以接受任何参数的函数”。这是较差的样式,请改用void C和C ++在这方面是不同的。 在C ++中, func()func(void)含义相同。

我将其简化为这一点,头文件中唯一需要的就是函数原型。

benchmark.h

double get_time();

benchmark.c

#ifdef WIN32
#include <windows.h>
#include "benchmark.h"

double get_time()
{
    LARGE_INTEGER t, f;
    QueryPerformanceCounter(&t);
    QueryPerformanceFrequency(&f);
    return (double)t.QuadPart/(double)f.QuadPart;
}

#else

#include <sys/time.h>
#include <sys/resource.h>
#include "benchmark.h"

double get_time()
{
    struct timeval t;
    struct timezone tzp;
    gettimeofday(&t, &tzp);
    return t.tv_sec + t.tv_usec*1e-6;
}

#endif

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM