的C文件组织 [英] Organization of C files

查看:143
本文介绍了的C文件组织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经习惯了做我的编码一个C文件。然而,我的工作足够大,它变得不切实际这样做的一个项目。我一直在#包括他们在一起,但我已经运行到哪里我#包括一些文件多次,等我听说过.h文件的情况下,但我不知道它们的功能是什么(为什么有2个文件大于1)更好。

I'm used to doing all my coding in one C file. However, I'm working on a project large enough that it becomes impractical to do so. I've been #including them together but I've run into cases where I'm #including some files multiple times, etc. I've heard of .h files, but I'm not sure what their function is (or why having 2 files is better than 1).

我应该用什么样的战略组织我的code?是否有可能以特定文件分开私人一公的功能呢?

What strategies should I use for organizing my code? Is it possible to separate "public" functions from "private" ones for a particular file?

<一个href=\"http://stackoverflow.com/questions/47878/$c$creview-tiny-encryption-algorithm-for-arbitrary-sized-data\"相对=nofollow>这问题precipitated我的询问。该tea.h文件没有提及到tea.c文件。该编译器知道每个.h文件中都有相应的.c文件?

This question precipitated my inquiry. The tea.h file makes no reference to the tea.c file. Does the compiler "know" that every .h file has a corresponding .c file?

推荐答案

您应该把.h文件作为你的.c文件中的接口文件即可。每个.c文件重新presents具有一定的功能模块。如果.c文件功能由其他模块使用(即其他.c文件)把函数原型中的.h接口文件。通过在.c文件和所有其他.c文件,你需要在你的功能模块,原接口文件,可以使此功能可在其他模块。

You should regard .h files as interface files of your .c file. Every .c file represents a module with a certain amount of functionality. If functions in a .c file are used by other modules (i.e. other .c files) put the function prototype in the .h interface file. By including the interface file in your original modules .c file and every other .c file you need the function in, you make this function available to other modules.

如果你只需要在某个.c文件(而不是任何其他模块)功能,声明其范围静态的。这意味着它只能在其中定义的C文件中调用。

If you only need a function in a certain .c file (not in any other module), declare its scope static. This means it can only be called from within the c file it is defined in.

也是一样的跨多个模块使用的变量。他们应该去头文件在那里,他们必须标有关键字EXTERN。注意:对于功能的关键字的extern是可选的。功能总是被认为的extern。

Same goes for variables that are used across multiple modules. They should go in the header file and there they have to marked with the keyword 'extern'. Note: For functions the keyword 'extern' is optional. Functions are always considered 'extern'.

在头文件中列入警卫有助于不包含相同的头文件多次。

The inclusion guards in header files help to not include the same header file multiple times.

例如:

Module1.c:

Module1.c:


    #include "Module1.h"

    static void MyLocalFunction(void);
    static unsigned int MyLocalVariable;    
    unsigned int MyExternVariable;

    void MyExternFunction(void)
    {
        MyLocalVariable = 1u;       

        /* Do something */

        MyLocalFunction();
    }

    static void MyLocalFunction(void)
    {
      /* Do something */

      MyExternVariable = 2u;
    }

Module1.h:

Module1.h:


    #ifndef __MODULE1.H
    #define __MODULE1.H

    extern unsigned int MyExternVariable;

    void MyExternFunction(void);      

    #endif

Module2.c

Module2.c


    #include "Module.1.h"

    static void MyLocalFunction(void);

    static void MyLocalFunction(void)
    {
      MyExternVariable = 1u;
      MyExternFunction();
    }

这篇关于的C文件组织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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