在C中的多线程中包括嵌套头文件的正确方法是什么 [英] What is the correct way to include nested header files in multithreading in c

查看:117
本文介绍了在C中的多线程中包括嵌套头文件的正确方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下头文件和源文件.

clnt_thread.cpp和main.c中包含的

  clnt_thread.hjacc_sim.cpp和main.c中包含的jacc_sim.hsrvr_info.cpp和main.c中包含的srvr_info.hclnt_thread.h和srvr_info.h中包含的constants.h,均参见上文并在global_variables.c中main.c中包含的global_variables.hmain.c 

某些变量在 clnt_thread.h 中声明为 extern ,而其他变量在 jacc_sim.h 中声明.

我的问题是,当我在某些头文件中将全局变量定义为 extern 时,我收到错误消息,指出它们未定义或已定义多次.另外,一些全局变量将在线程之间共享,而某些则将由每个线程独立使用.

我知道这些错误是由于头文件在源文件中嵌套(包括)的方式导致的.

所以:

  1. 包括这些头文件并定义 extern 变量的正确方法是什么?

  2. 如何区分线程之间共享的变量和另一个线程之间不应该共享的变量?

解决方案

请遵循以下规则:

  1. 不在标头中定义变量,仅声明它们.IE.始终将 extern 与变量一起使用.
  2. 在一个完整的代码文件中一次定义每个变量(用1声明 extern ),即不使用 extern .
  3. 即使声明变量也只能在一个标头中执行一次.(尽管如果您始终如一地进行编译,编译器不会抱怨,但是只执行一次仍然更加干净和安全.)
  4. 函数原型一次进入头文件;即以; 结尾.
  5. 函数定义一次进入代码文件;即具有功能标头(不带; 的原型)和功能体( {...} ).
  6. 无论您要访问或引用什么,都将用于声明该文件的标头包含在文件中(您需要在其中包含标头或代码文件).
  7. 使用重新包含防护以提高安全性(主要用于宏等).

重新安置守卫的例子:

MyHeader.h:

  #ifndef MYHEADER_H#定义MYHEADER_H/*仅声明*/#endif/* MYHEADER_H */ 

I have the following header and source files.

clnt_thread.h        included in clnt_thread.cpp and main.c
jacc_sim.h           included in jacc_sim.cpp and main.c
srvr_info.h          included in srvr_info.cpp and main.c
constants.h          included in clnt_thread.h and in srvr_info.h, both see above
                          and in global_variables.c
global_variables.h   included in main.c
main.c

Some variables are declared as extern in clnt_thread.h and others in jacc_sim.h.

My problem is, when I define the global variables as extern in some of the header files I get errors saying that either they are undefined or they are defined multiple times. Plus, some of the global variables are to be shared among threads while some are to be used by each thread independently.

I know these errors are a result of the way the header files are nested(included) in the source files.

So:

  1. What is the correct way to include these header files and define the extern variables?

  2. What distinguishes a variable shared among threads and another one which should not be shared between threads?

解决方案

Do follow these rules:

  1. Do not define variables in headers, only declare them. I.e. always use extern with variables.
  2. Define each variable (declared extern by 1.) exactly once in exactly one code file, i.e. without extern.
  3. Even declaring variables should only be done once, in one header. (Though the compiler will not complain if you do it consistently, it is still much cleaner and safer to do it only once.)
  4. Function prototypes go into headers, once; i.e. with ; at the end.
  5. Function definitions go into code files, once; i.e. with function head (prototype without ;) followed by function body ( {...}).
  6. Whatever you want to access or refer to, include the header which declares it into the file (header or code file you need it in).
  7. Use reinclusion guards for extra safety (mostly for macros etc.).

Example of a reinclusion guard:

MyHeader.h:

#ifndef MYHEADER_H
#define MYHEADER_H

/* only declarations */

#endif /* MYHEADER_H */

这篇关于在C中的多线程中包括嵌套头文件的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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