• C语言项目中头文件/代码文件的组织问题(使用VC6.0)


    个人备忘:

    1.新建一个文本文件命名为main.c,使用VC6.0打开并编译,VC6.0会在当前文件夹下自动创建“工作区间”和“工程”文件,如:main.dsw,main.dsp等;然后就可以在“工程”下新建或添加你的.c和.h文件。

     

    2.消灭头文件被多次include导致的错误。在每个头文件中加上如下的定义:(假设头文件的名字为MyDemo.h)

    #ifndef MY_DEMO_H

    #define MY_DEMO_H

     

    //...

     

    #endif // MY_DEMO_H

     

    3.文件后缀名不要混用.cpp,只使用.c/.h。如果是使用VC6直接创建的C++项目,建议后缀名统一用.cpp,总之不要混用。

     

    4.头文件中一般只包含必要的全局类型/变量定义和函数接口定义。关于函数接口定义,要思考清楚哪些函数是要提供给其它模块使用的,哪些是本模块内部使用的,只需把对外的(供外部模块使用的)函数接口定义在头文件中即可。不对外公开的函数,可用static定义之。

     

    5.一个完整代码示例:

    main.c

     

    #include "stdio.h"

    #include "myfun1.h"
    #include "myfun2.h"

    void main()
    {
        //加减法测试
        test_add_minus(92);

        //乘除法测试
        test_multiply_divide(93);

        printf("Please Press [Enter] to Exit..\n\n");
        getchar();
    }

     

    myfun1.h

     

    #ifndef _MYFUN1_H
    #define _MYFUN1_H

    //加减法测试
    void test_add_minus();


    #endif

     

    myfun1.c

     

    #include "stdio.h"
    #include "myfun1.h"

    //加法
    static int add(int a, int b)
    {
        return (a+b);
    }

    //减法
    static int minus(int a, int b)
    {
        return (a-b);
    }

    //加减法测试
    void test_add_minus(int a, int b)
    {
        int m = add(a, b);
        int n = minus(a, b); 
        printf("a+b=%d\n\n", m);
        printf("a-b=%d\n\n", n);
    }

    myfun2.h

     

    #ifndef _MYFUN2_H
    #define _MYFUN2_H
     
    //乘除法测试
    void test_multiply_divide(int a, int b);


    #endif

    myfun2.c

     

    #include "stdio.h"
    #include "myfun2.h"

    //乘法
    static int multiply(int a, int b)
    {
        return (a*b);
    }

    //除法
    static int divide(int a, int b)
    {
        return (a/b);
    }

    //乘除法测试
    void test_multiply_divide(int a, int b)
    {
        int m = multiply(a, b);
        int n = divide(a, b); 
        printf("a*b=%d\n\n", m);
        printf("a/b=%d\n\n", n);
    }

    6.头文件应是函数功能模块的组织,就像面向对象编程中的一个类的定义一样。为避免代码文件过大,函数功能又能有效组织,可以将多个代码文件对应一个头文件。这里还是要从功能组织去思考,或者说从函数接口的组织去思考,头文件应是将相似的功能函数定义在一起,便于查找或梳理相似功能的函数。这时,与单个头文件对应的多个代码文件很像C#语言中的分部类定义,呵呵。7中是对5中的代码改进后的完整示例。

     

    7.一个完整代码示例(多个代码文件对应一个头文件):

    main.c

     

    #include "stdio.h"

    #include "myfun.h"

    void main()
    {
        //加减法测试
        test_add_minus(92);

        //乘除法测试
        test_multiply_divide(93);

        printf("Please Press [Enter] to Exit..\n\n");
        getchar();
    }

     

    myfun.h

     

    #ifndef _MYFUN_H
    #define _MYFUN_H

    //加减法测试
    void test_add_minus();

    //乘除法测试
    void test_multiply_divide(int a, int b);

    #endif

     

    myfun1.c

     

    #include "stdio.h"
    #include "myfun.h"

    //加法
    static int add(int a, int b)
    {
        return (a+b);
    }

    //减法
    static int minus(int a, int b)
    {
        return (a-b);
    }

    //加减法测试
    void test_add_minus(int a, int b)
    {
        int m = add(a, b);
        int n = minus(a, b); 
        printf("a+b=%d\n\n", m);
        printf("a-b=%d\n\n", n);
    }

     

    myfun2.c

     

    #include "stdio.h"
    #include "myfun.h"

    //乘法
    static int multiply(int a, int b)
    {
        return (a*b);
    }

    //除法
    static int divide(int a, int b)
    {
        return (a/b);
    }

    //乘除法测试
    void test_multiply_divide(int a, int b)
    {
        int m = multiply(a, b);
        int n = divide(a, b); 
        printf("a*b=%d\n\n", m);
        printf("a/b=%d\n\n", n);
    }

     

     作者:夏春涛 xchuntao@163.com 

     

  • 相关阅读:
    Linux Select之坑
    BitCoin p2p通信过程
    2018软工实践——团队答辩
    Ubuntu16安装GTK+2.0教程
    福大软工1816 · 第五次作业
    福大软工1816 · 第五次作业
    福大软工1816 · 第五次作业
    福大软工1816 · 第五次作业
    Notepad++一键编译运行(Python、Java、C++)
    福大软工1816 · 第四次作业
  • 原文地址:https://www.cnblogs.com/SummerRain/p/2605783.html
Copyright © 2020-2023  润新知