zoukankan      html  css  js  c++  java
  • 多文件编程

    //添加一个源文件  main1.c

    #include<stdio.h>

    //"":导入自己的头文件

    #include"fun2.h"

    int main()

    {

    //gcc-o hello.exe main1.c fun2功能实现.c fun2.h head.h

    //没有什么先后顺序,编译器会自动找到主函数,再进行文件的整合

      int a=10;

      int b=20;

      printf("%d ",max(a,b));

      return 0;

    }

    //添加一个源文件  fun2功能实现.c

    #include"fun2.h"

    //函数定义

    int max(int a,int b)

    {

      return a>b?a:b;

    }

    //添加一个头文件  fun2.h

    //为避免同一个文件被include多次,C/C++中有两种方试:一种是#ifndef;一种是#pragma once

    /*格式:

    #ifndef __SOMEFILE_H__
    #define __SOMEFILE_H__
     // 声明语句
     #endif

    */

    //#pragma once//防止头文件重复包含,能够保证头文件只被编译一次。

    #ifndef __FUN2_H__

    #define __FUN2_H__

    #include"head.h"

    //全局变量的定义

    //函数的声明 

    extern int max(int a,int b);

    #endif // !1

    //添加一个头文件  head.h

    //#pragma once

    #include"fun2.h"

    //包含头文件的错误 

  • 相关阅读:
    qsort()的使用
    c语言不寻常的类型转换(类型提升)
    堆栈段的三个主要用途
    区分 声明与定义
    宏定义陷阱与typedef
    约瑟夫环解决方案
    线程中断测试
    Redis
    本地缓存
    tomcat优化
  • 原文地址:https://www.cnblogs.com/wanghong19991213/p/13533702.html
Copyright © 2011-2022 走看看