头文件与源文件中都分别存放哪些东西

2023-05-16

    在C代码文件中,我们经常会看到两类文件:

                                        一类是:".h"文件

                                        一类是:".c"文件

   ".h"文件,就是我们常说的头文件。

   ".c"文件,就是我们常说的源代码文件。

 

   而在做开发的时候,经常不知道要把哪些东西放到".h"文件之中,又要把哪些东西放到".c"文件中。

   今天就详细介绍一下这两个文件(".h"和".c")中都应该存放写什么东西。

   首先,先介绍".h"文件。

   ".h"文件中存放的东西有:

           1.include所需要的头文件。 如 #include<linux/pid.h>

           2.宏定义。          如: #define SIZE 10;

           3.类型的定义。      如: typedef struct student{} student

           4.外部变量的声明。  如: extern struct task_struct *task;

                                   extern spinlock_t spinlock;

           5.函数的声明。      如: int sum(int a, int b


           6.static inline函数。如:static inline sum(int a, int b);


   以下是一个linux源码包中的头文件:

 
  2 //头文件说明,用于防止头文件重名
  3 #ifndef _LINUX_PID_H
  4 #define _LINUX_PID_H
  5 
  6 #include <linux/rcupdate.h>
  7 
  8 //类型定义
  9 enum pid_type
 10 {
 11     PIDTYPE_PID,
 12     PIDTYPE_PGID,
 13     PIDTYPE_SID,
 14     PIDTYPE_MAX
 15 };
 16 
 17 struct upid {
 18     /* Try to keep pid_chain in the same cacheline as nr for find_vpid */
 19     int nr;
 20     struct pid_namespace *ns;
 21     struct hlist_node pid_chain;
 22 };
 23 
 24 struct pid
 25 {
 26     atomic_t count;
 27     unsigned int level;
 28     /* lists of tasks that use this pid */
 29     struct hlist_head tasks[PIDTYPE_MAX];
 30     struct rcu_head rcu;
 31     struct upid numbers[1];
 32 };
  
 47 //外部变量的声明
 48 extern struct pid init_struct_pid;
 49 extern struct pid_namespace init_pid_ns;
 50 
 51 //外部函数声明
 52 extern void put_pid(struct pid *pid);
 53 extern struct task_struct *pid_task(struct pid *pid, enum pid_type);
 54 extern struct task_struct *get_pid_task(struct pid *pid, enum pid_type);
 55 extern struct pid *get_task_pid(struct task_struct *task, enum pid_type type);
 56 extern void attach_pid(struct task_struct *task, enum pid_type type,
 57             struct pid *pid);
 58 extern void detach_pid(struct task_struct *task, enum pid_type);
 59 extern void change_pid(struct task_struct *task, enum pid_type,
 60             struct pid *pid);
 61 extern void transfer_pid(struct task_struct *old, struct task_struct *new,
 62              enum pid_type); 
 
 
 64 // static inline 函数
 65 static inline struct pid_namespace *ns_of_pid(struct pid *pid)
 66 {
 67     struct pid_namespace *ns = NULL;
 68     if (pid)
 69         ns = pid->numbers[pid->level].ns;
 70     return ns;
 71 }
 72 
 73 static inline pid_t pid_nr(struct pid *pid)
 74 {
 75     pid_t nr = 0;
 76     if (pid)
 77         nr = pid->numbers[0].nr;
 78     return nr;
 79 }
 
 
 81 
 82 //宏定义
 83 #define do_each_pid_task(pid, type, task)               \
 84     do {                                \
 85         struct hlist_node *pos___;              \
 86         if ((pid) != NULL)                  \
 87             hlist_for_each_entry_rcu((task), pos___,    \
 88                 &(pid)->tasks[type], pids[type].node) {
 89 
 90 #define while_each_pid_task(pid, type, task)                \
 91                 if (type == PIDTYPE_PID)        \
 92                     break;              \
 93             }                       \
 94     } while (0)
 95 
 96 #define do_each_pid_thread(pid, type, task)             \
 97     do_each_pid_task(pid, type, task) {             \
 98         struct task_struct *tg___ = task;           \
 99         do {
100 
101 #define while_each_pid_thread(pid, type, task)              \
102         } while_each_thread(tg___, task);           \
103         task = tg___;                       \
104     } while_each_pid_task(pid, type, task)
105 #endif /* _LINUX_PID_H */

 

      ".c"文件中存放的东西有:

                             1.需要的头文件。

                             2.全局变量。即头文件中的"extern int 变量名"声明的变量在这儿要进行定义与初始化;

                             3.只在本文件中使用的变量。即 static 变量类型

                             4.全局函数的实现。即头文件中的"extern int 函数名"声明的函数要在这儿进行实现。

                             5.局部函数的实现。即static 变量类型 函数名


      linux源码中的".c"文件:


 29 //包含的头文件
 30 #include <linux/mm.h>
 31 #include <linux/module.h>
 32 #include <linux/slab.h>
 33 #include <linux/init.h>
 34 #include <linux/rculist.h>
 35 #include <linux/bootmem.h>
 36 #include <linux/hash.h>
 37 #include <linux/pid_namespace.h>
 38 #include <linux/init_task.h>
 39 #include <linux/syscalls.h>
 40 
 41 //只在本文件中使用的宏定义
 42 #define pid_hashfn(nr, ns)  \
 43     hash_long((unsigned long)nr + (unsigned long)ns, pidhash_shift)
 
 
  //定义只在本文件中使用的变量;
 46 static struct hlist_head *pid_hash;
 47 static unsigned int pidhash_shift = 4;
 48 
 49 //全局变量的声明以及初始化,其他文件也可以使用,通过#include<linux/pid.h>即可
 50 struct pid init_struct_pid = INIT_STRUCT_PID;
 51 
 52 int pid_max = PID_MAX_DEFAULT;
 53 int pid_max_min = RESERVED_PIDS + 1;
 54 int pid_max_max = PID_MAX_LIMIT;
 55 struct pid_namespace init_pid_ns = {
 56     .kref = {
 57         .refcount       = ATOMIC_INIT(2),
 58     },
 59     .pidmap = {
 60         [ 0 ... PIDMAP_ENTRIES-1] = { ATOMIC_INIT(BITS_PER_PAGE), NULL }
 61     },
 62     .last_pid = 0,
 63     .level = 0,
 64     .child_reaper = &init_task,
 65 };
 66 EXPORT_SYMBOL_GPL(init_pid_ns);
 
 
 
 68 //函数的实现,其他文件通过#include<linux/pid.h>就可以使用此函数;
 69 int is_container_init(struct task_struct *tsk)
 70 {
 71     int ret = 0;
 72     struct pid *pid;
 73 
 74     rcu_read_lock();
 75     pid = task_pid(tsk);
 76     if (pid != NULL && pid->numbers[pid->level].nr == 1)
 77         ret = 1;
 78     rcu_read_unlock();
 79 
 80     return ret;
 81 }
 82 EXPORT_SYMBOL(is_container_init);
 83 
 84 //只能被本文件中的其他函数使用的函数。
 85 static void free_pidmap(struct upid *upid)
 86 {
 87     int nr = upid->nr;
 88     struct pidmap *map = upid->ns->pidmap + nr / BITS_PER_PAGE;
 89     int offset = nr & BITS_PER_PAGE_MASK;
 90 
 91     clear_bit(offset, map->page);
 92     atomic_inc(&map->nr_free);
 93 }




转载于:https://blog.51cto.com/weiguozhihui/1650331

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

头文件与源文件中都分别存放哪些东西 的相关文章

随机推荐

  • Ai challenger 2017 image caption小结

    参加了今年的 ai challenger 的 image caption比赛 xff0c 最终很幸运的获得了第二名 这里小结一下 Pytorch 越来越火了 前五名有三个 pytorch xff0c 两个 tensorflow 关于哪个 l
  • 哥很无奈 今天看到我的host文件是这个样子

    127 0 0 1 www gtxp2 com 这家无良公司在所谓的网维工具内加入了屏蔽我站的信息 xff0c 我们也是不得已做出反击 xff0c 望见者谅解 127 0 0 1 gtxp2 com 封死此无良网站 xff0c B4此站的相
  • [转载] 以下划线开头的变量

    转自 xff1a https blog csdn net Grevi article details 60581354 今天在公司看 GNU ISO C 43 43 Library库中的stl库时 xff0c 偶然间感觉到一个问题 xff0
  • 如果编程语言是武侠

    如果是武功 C紫霞神功要大成需要很长时间 xff0c 威力还行Cpp九阳神功威力巨大Lisp小无相功你可以把它当做任何武功Shell太极拳四两拨千斤PHP打狗棒法不上台面 xff0c 但威力惊人Java八荒六合唯我独尊神功 无敌C 北冥神功
  • 跳转位置-更改目录(CD)PowerShell命令,可让您读懂

    There 39 s a lovely little utility called autojump for nix consoles that makes the 39 cd 39 command very smart More that
  • C#结构体指针的定义及使用详解

    在解析C 结构体指针前 xff0c 必须知道C 结构体是如何定义的 在c 中同样定义该结构体 C 结构体指针之C 结构体的定义 xff1a StructLayout LayoutKind Sequential public struct V
  • Permutation Test 置换检验

    显著性检验通常可以告诉我们一个观测值是否是有效的 xff0c 例如检测两组样本均值差异的假设检验可以告诉我们这两组样本的均值是否相等 xff08 或者那个均值更大 xff09 我们在实验中经常会因为各种问题 xff08 时间 经费 人力 物
  • LaTeX 中使用三级标题

    需要在导言区加入命令 xff1a setcounter secnumdepth 4 而后 xff1a section 一级标题 subsection 二级标题 subsubsection 三级标题
  • 为啥程序员下班后只关显示器从不关电脑?

    阅读本文大概需要 3 分钟 你下班时是不是只将显示器一关 xff0c 揣上手机就走了 xff1f 曾有安保人员晚上来办公室巡查时问 xff0c 为什么这些人不关机就下班呢 xff1f 作为程序员 xff0c 你会心一笑 对方不明白如果关机了
  • 美国 ZIP Code 一览表

    Zip Code 这个是美国的邮政编码 美国目前只有邮政是国营的 其余的产业都不是国营的 今天给大家提供美国的Zip Code的原因是大家在注册国外的账号时 需要提供这个Zip Code 因为一般美国的服务默认是面向美国的 甚至是仅支持美国
  • pytorch .detach() .detach_() 和 .data用于切断反向传播

    参考 xff1a https pytorch cn readthedocs io zh latest package references torch autograd detachsource 当我们再训练网络的时候可能希望保持一部分的网
  • UPX使用教程

    UPX是一个通用可执行文件压缩器 xff0c 由于其具有 xff1a 压缩率高 xff1a 压缩效果优于zip gzip xff1b 解压速度快 xff1a 在奔腾133上即可达到大约10MB 秒 xff1b 压缩的可执行文件没有额外的内存
  • Prestashop--配置到阿里云

    Prestashop版本 xff1a v1 6 阿里云环境 xff1a 前段时间出来的免费虚拟主机 之前没搞过网站 xff0c 所以这一切都是蛮新鲜的 因为没有接触过 xff0c 所以必然要遇到蛮多的坑 xff0c 将遇到的坑 填好的坑都记
  • 名词解释

    payload http中的payload 有效载荷在一个数据包或者其他传输单元中运载的基本必要数据 xff0c 即加载的基本数据 记载着信息的那部分数据 通常在传输数据中 xff0c 为了使数据传输更可靠 xff0c 要把原始数据分批传输
  • ubuntu和debian_Debian Ubuntu“ netstat:未找到命令”错误解决方案和Netstat安装

    ubuntu和debian Ubuntu is most used Linux distributions Canonical provides enterprise support for Ubuntu desktops and serv
  • ListControl双击实现可编辑

    ON NOTIFY 处理 listControl 消息 列表控件的消息映射同样使用ON NOTIFY宏 xff0c 形式如同 xff1a ON NOTIFY wNotifyCode id memberFxn xff0c wNotifyCod
  • 如何在创业公司工作保持激情?试试这六条建议

    在创业公司里 xff0c 创始人都表现出雄心勃勃的状态 同样 xff0c 他们也希望自己的手下能和他们一样对正在开创的事业充满激情 相比于在成熟的大公司工作 xff0c 创业公司的求职赛场遵循的或许是另一套规则 JasonFreedman是
  • linux pts设备,linux 系统tty、pty和pts 的概念及区别

    基本概念 xff1a 1 tty 终端设备的统称 tty一词源于Teletypes xff0c 或者teletypewriters xff0c 原来指的是电传打字机 xff0c 是通过串行线用打印机键盘通过阅读和发送信息的东西 xff0c
  • sonic——可替代Elasticsearch的简单搜索引擎

    简介 近期 xff0c 笔者在github上发现了一个十分好玩的开源项目 sonic sonic项目的介绍十分简单 Fast lightweight amp schema less search backend An alternative
  • 头文件与源文件中都分别存放哪些东西

    在C代码文件中 xff0c 我们经常会看到两类文件 xff1a 一类是 xff1a 34 h 34 文件 一类是 xff1a 34 c 34 文件 34 h 34 文件 xff0c 就是我们常说的头文件 34 c 34 文件 xff0c 就