用.c替代.h进行包含 – MISRA C

huangapple go评论50阅读模式
英文:

Include .c instead of header(.h) - MISRA C

问题

#include "component.c" 被认为是不良做法吗?是否违反了任何 MISRA 标准规定?(可能是规则 3-3-1)

到目前为止,我了解到这通常被归类为不良做法,但在某些场景中可以使用,这是否会对安全关键应用产生特定的担忧?

MISRA 规则 3-3-1 规定具有外部链接的对象或函数应在头文件中声明。

英文:

Is using #include "component.c" considered bad practice or is there any misra standard rule violation? (potentially rule 3-3-1)

So far, I understand that it is a commonly categorized as bad practice but can be used in certain scenarios, would it be any particular concern for safety-critical applications?

Misra rule 3-3-1 states that Objects or functions with external linkage shall be declared in a header file

答案1

得分: 1

当你编写这样的程序时,它被称为统一构建。这样的程序的一个很好的例子是Odin编程语言的编译器。所以这是可行的,但像任何事情一样,都有权衡。

优点

  • 它将更多的源代码放入单个翻译单元中。这有助于优化,类似于链接时优化的工作原理,因为编译器实际上可以看到其他C文件中函数的源代码。

  • 项目文件中的混乱较少(较少的头文件),因此重复较少。

  • 构建大大简化;即使项目中有许多文件,构建可能只需构建一个文件。

缺点

  • 对于使用更常见实践的同事来说,理解起来更困难。

  • 无法在C文件中使用非常通用的名称定义静态变量,因为该C文件可能是更大翻译单元的一部分。你将得到更冗长的变量名称。

  • 它通常会减慢构建速度。构建系统(如make)可以通过仅重新编译对所有更改具有依赖关系的内容来加快编译速度。如果将事物合并到单个翻译单元中,那么每次更改都需要构建整个东西。在大型项目中,这将导致构建期间的内存消耗增加。

  • 无法并行构建。

这违反了MIRSA吗?

我其实不知道,但它不违反你发布的规则:

>Misra规则3-3-1规定具有外部链接的对象或函数应在头文件中声明。

通过包含一个C文件,你不再具有外部链接,因为它们在同一个翻译单元中。

英文:

When you write a program like that, it is called a unity build. A good example of such a program is the compiler for the Odin programming language. So it can be done, but like anything else, there are tradeoffs.

pros

  • It puts more source into a single translation unit. This can help optimization, similar to how link-time-optimization works since the compiler can actually see the source from functions in other C files.

  • Less clutter in project files (less headers), and therefor, less repetition.

  • A greatly simplified build; It may be as easy as building only one file even if many files are in the project.

cons

  • Harder to understand for co-workers who use more common practices.

  • You can no longer define a static variable with a very generic name in a C file, because that C file could be part of a bigger translation unit. You will wind up with more verbose variable names.

  • It will generally slow down the build. Build systems (like make), can speed up compilation by only re-compiling what is dependent on all changes. If you are combining things into a single translation unit, the whole thing will need to build for every change. In a big project, this will lead to more memory consumption during the build.

  • You cannot parallelize the build.

Does this violate MIRSA?

I don't actually know, but it doesn't violate the rule you posted:

>Misra rule 3-3-1 states that Objects or functions with external linkage shall be declared in a header file

By including a C file, you know longer have external linkage because they are in the same translation unit.

答案2

得分: 1

你在引用的是MISRA C++标准,而不是MISRA C标准。在MISRA C++标准中,规则3-1-1并不限制你在其他C文件中使用#include,因为任何C文件都可以相互包含,并且不声明任何东西具有外部链接。这个规则/要求是关于外部链接的,而不是关于C文件包含的。我认为,如果他们要添加一个关于包含其他C文件的规则,那会在“源文件包含”规则16-2-X下,但我刚刚检查了一下,没有关于包含其他C文件的内容。就我个人而言,我不介意包含其他C文件,因为我把#include看作是一种复制和粘贴操作,可以根据需要使用。

英文:

You are referencing the MISRA C++ standard not the MISRA C standard. In the MISRA C++ standard rule 3-1-1 does not restrict you from #include-ing C files in other C files, because any C file may include one another and not declare anything as having external linkage. The rule/requirement is about external linkage, not about C file inclusion. I believe that if they were to add a rule for including other C files it would be under section "Source file inclusion" rules 16-2-X, but i have just checked and there is nothing about including other C files. Personally, i dont mind including other C files because i see #include as just a copying and pasting operations to be used however you see fit.

答案3

得分: 0

这也涉及到安全性问题,你可能不想提供你的代码,只需提供头文件和目标文件。

英文:

It is also a matter of security in terms that you might not want to provide your code, so you just give the header and the object files

答案4

得分: 0

使用 #include "component.c" 被认为是不良实践吗

一般而言,是的。

通常,预处理器的文件包含特性被保留用于支持将共同的声明因子化到它们自己的文件中,这样它们就不需要在多个源文件中重复。这种因子化对于除了最小的项目之外的所有项目都有巨大的维护优势,并且对于可用性来说几乎是必需的。仅包含这些声明的文件通常被称为“头文件”,并且通常以.h后缀命名。从这个意义上说,问题部分涉及文件命名和编程模式。

另一方面,包含函数或对象定义(而不是声明)的C源文件通常以.c后缀命名。C语言禁止在同一程序中有相同标识符(函数或变量名)的多个外部定义,因此这些.c文件不能被包含到贡献到程序的多个翻译单元中的一个以上,并且如果它们被包含到其中一个中,则不能直接编译。从多个.c文件构建程序的常用方法是将它们独立编译,然后将它们链接在一起。

只要确保避免重复定义,将一个.c文件#include到另一个文件中并不是本质上错误的。但这只有在特殊情况下才有意义。这会增加额外的风险,并且在语言规范方面并没有特别的优势。而且,由于与惯例相违,它往往会使开发人员感到惊讶和困惑——甚至是更有经验的未来的你。

这样做是否违反了MISRA标准的规定?(潜在的规则3-3-1)

它并不本质上违反MISRA规则3-3-1,但它使得编写违反该规则的代码变得不那么痛苦。这是通过使得所有所需的外部声明都在一个.c文件及其包含中变得可行,而不是提供一个单独的头文件。

我没有看到其他MISRA-2012规则会违反这样的包含。但在代码审查中,这对我来说并没有太多说服力。

英文:

> Is using #include "component.c" considered bad practice

Generally speaking, yes.

Usually, the preprocessor's file-inclusion feature is reserved to support factoring common declarations into their own files, so that they don't need to be duplicated in multiple sources. Such factoring provides huge benefits for maintainability in all but the smallest projects, and it is a near-requirement for libraries to be usable. A file containing only such declarations is conventionally called a "header", and is conventionally named with a .h suffix. In this sense, then, the issue is partially about file naming and programming patterns.

On the other hand, C source files containing function or object definitions (as opposed to declarations) are conventionally named with a .c suffix. The C language forbids multiple external definitions of the same identifier (function or variable name) in the same program, so such .c files cannot be #included into more than one translation unit contributing to a program, and if they are included into even one, then they must not also be compiled directly. The usual way to build a program from multiple .c files is to compile them independently and then link them together.

Provided one is sure to avoid duplicate definitions, it is not inherently wrong to #include one .c file into another. But this makes sense only as a special-purpose arrangement. It creates extra risk, and it provides no particular advantage as far as the language specification goes. Also, because it runs contrary to convention, it tends to surprise and confuse developers -- maybe even more experienced future you.

> is there any misra standard rule violation? (potentially rule 3-3-1)

It does not inherently violate MISRA rule 3-3-1, but it makes it less painful to write code that does violate that rule. That is by making it feasible to have all the needed external declarations in a single .c file plus its inclusions, instead of providing a separate header.

I don't see any other MISRA-2012 rule that such an inclusion would violate. But that would not hold much water with me in code review.

答案5

得分: 0

规则 3-1-1 是 MISRA C++:2008 规则

适用的 MISRA C:2012 指南可能是规则 20.2 和 20.3 - 这并不禁止使用 .c 扩展名。

在 MISRA C:2004 中,规则 8.5 禁止在头文件中包含源代码(假设为 .h 文件)。这个规则在 MISRA C:2012 中被取消,以允许使用 inline 函数。

个人而言,我认为 #include 一个 .c 文件是一个不好的主意 - 这表明对语言、编译器和链接器的理解存在误解。

如果这样做,扩展后的文件将在分析目的上计为单一翻译单元...

(请查看个人资料获取隶属关系)

英文:

Rule 3-1-1 is a MISRA C++:2008 rule

The applicable MISRA C:2012 guidelines are (probably) Rules 20.2 and 20.3 - which do not prohibit the use of .c extensions.

--

In MISRA C:2004, Rule 8.5 prohibited source code in a header file (assumed to be a .h file). This Rule was dropped for MISRA C:2012 to permit the use of inline functions.

--

Personally, I think #include-ing a .c file is a bad idea - it suggests a mis-understanding of the language, the compiler and the linker.

If you do so, the expanded file would count as a Single Translation Unit for analysis purposes...

(see profile for Affiliation)

huangapple
  • 本文由 发表于 2023年3月31日 23:23:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900211.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定