How to split this into header and source files?

最后都变了- 提交于 2021-01-28 04:40:16

问题


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

What would be the proper format of the resulting benchmark.h and benchmark.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

#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

回答1:


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.

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.

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

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

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.




回答2:


I would simplify it to this, the only thing needed in the header file is the function prototype.

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


来源:https://stackoverflow.com/questions/28715445/how-to-split-this-into-header-and-source-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!