如何将其分为头文件和源文件? [英] How to split this into header and source files?

查看:98
本文介绍了如何将其分为头文件和源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些C代码拆分为头文件和源文件:

I have some C code I'd like to split into a header file and a source file:

#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.h benchmark.c 的正确格式是什么?

What would be the proper format of the resulting benchmark.h and benchmark.c?

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

I know the header file should contain function declarations, while the source file should be where the actual function definitions reside. Would this following code be correct? Namely, should the #ifdef WIN32 directive be in both files as I have it below? Or should it all be in the .c file?

benchmark.h

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

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

推荐答案

头文件和c文件一起构成一个代码模块"(或者,如果愿意的话,它是ADT,类等).

Together, a header file and a c file form a "code module" (or if you will: an ADT, a class etc).

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

The header file is always to be regarded as the user interface of your code, where the "user" is the programmer who is going to use your module. It shall never contain any code or variable definitions, period.

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

While the c file contains the actual implementation, which is of no interest to the user, and should not be of any concern to them. The c file should use private encapsulation and everything that the user need not know should be in that file.

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

The above is how you design C programs, or any program in any language. This is not subjective, it is not opinion-based, it is the only way. If you are doing your program design differently, you are doing it wrong.

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

As for your specific program, it should be designed in the following way:

benchmark.h

#ifndef BENCHMARK_H
#define BENCHMARK_H

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

#endif

基准.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)含义相同.

Note that double get_time() means "function that accepts any parameter" in C. That is poor style, use void instead. C and C++ are different in this regard. In C++, func() and func(void) mean the same thing.

这篇关于如何将其分为头文件和源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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