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

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

问题描述

我习惯于在一个 C 文件中完成所有编码.但是,我正在从事一个足够大的项目,以至于这样做变得不切实际.我一直将它们#include 在一起,但我遇到过我多次#include 某些文件的情况,等等.我听说过 .h 文件,但我不确定它们的功能是什么(或者为什么有 2 个文件比 1 个更好).

我应该使用什么策略来组织我的代码?是否可以将特定文件的公共"功能与私人"功能分开?

这个问题引发了我的询问.tea.h 文件不引用tea.c 文件.编译器是否知道"每个 .h 文件都有对应的 .c 文件?

解决方案

您应该将.h 文件视为.c 文件的接口文件.每个 .c 文件都代表一个具有一定功能的模块.如果 .c 文件中的函数被其他模块(即其他 .c 文件)使用,则将函数原型放在 .h 接口文件中.通过将接口文件包含在您的原始模块 .c 文件和您需要该功能的所有其他 .c 文件中,您可以使该功能可用于其他模块.

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

跨多个模块使用的变量也是如此.它们应该放在头文件中,并且必须用关键字extern"标记.注意:对于函数,关键字extern"是可选的.函数始终被视为外部".

头文件中的包含保护有助于避免多次包含同一个头文件.

例如:

模块1.c:

<上一页>#include模块1.h"静态无效 MyLocalFunction(void);静态无符号整数 MyLocalVariable;无符号整数 MyExternVariable;无效我的外部函数(无效){MyLocalVariable = 1u;/* 做一点事 */MyLocalFunction();}静态无效 MyLocalFunction(无效){/* 做一点事 */我的外部变量 = 2u;}

模块1.h:

<上一页>#ifndef __MODULE1.H#define __MODULE1.Hextern unsigned int MyExternVariable;无效我的外部函数(无效);#万一

模块2.c

<上一页>#include "模块.1.h"静态无效 MyLocalFunction(void);静态无效 MyLocalFunction(无效){我的外部变量 = 1u;我的外部函数();}

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).

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

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?

解决方案

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.

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.

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.

For example:

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:

    #ifndef __MODULE1.H
    #define __MODULE1.H

    extern unsigned int MyExternVariable;

    void MyExternFunction(void);      

    #endif

Module2.c

    #include "Module.1.h"

    static void MyLocalFunction(void);

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

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

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