简体   繁体   English

如何使智能感知将当前源文件视为独立的翻译单元

[英]How to make intellisense treat the current source file as a standalone translation unit

While refactoring our project sources for future use in other projects, we're encountering a problem: It's getting difficult to identify what really depends of what, because Intellisense magically fixes everything behind the scenes.在重构我们的项目源以备将来在其他项目中使用时,我们遇到了一个问题:很难确定什么真正取决于什么,因为 Intellisense 神奇地修复了幕后的一切。

Example problem:示例问题:

  • "cc" source file #includes "ah" and then "bh" "cc"源文件 #includes "ah"然后是"bh"

  • So "bh" should be able to use all "ah" without any #include because it is part of the "cc" translation unit所以"bh"应该能够使用所有"ah"而没有任何#include因为它是"cc"翻译单元的一部分

  • While editing "bh" Intellisense "correctly" shows all definitions from "ah" which is awesome在编辑"bh" Intellisense 时“正确”显示来自"ah"所有定义,这很棒

But we need a way to make it evident that there is a missing #include "ah" at the top of "bh" for future reusing... Is there any way to make Intellisense treat "bh" as a standalone translation unit?但是我们需要一种方法来表明在"bh"的顶部缺少一个#include "ah"以供将来重用......有没有办法让Intellisense将"bh"视为一个独立的翻译单元?

Edit: Changed the name of the Main source to "cc" for clarification, even if we do have circular includes in our code, it is not the actual problem we need to fix编辑:为了澄清,将主源的名称更改为"cc" ,即使我们的代码中有循环包含,这也不是我们需要修复的实际问题

For the sake of testing you could make a separate translation unit called test_b_h.c which only has为了测试,您可以制作一个名为test_b_h.c的单独翻译单元,它只有

#include <b.h>

As it would not generate any target code it will not have an effect on the size of the resulting executable.由于它不会生成任何目标代码,因此不会对生成的可执行文件的大小产生影响。

I'm not sure having a seperate translation unit is what you want here... #include is very unmagical (it just concatenates the files), and seperate translation units simply compile to seperate object files ("bh" is unlikely to be a sensible program by itself, as idomatically, headers don't contain anything for which storage is alloted in the object code).我不确定有一个单独的翻译单元是你想要的...... #include 是非常不神奇的(它只是连接文件),并且单独的翻译单元只是编译为单独的目标文件(“bh”不太可能是一个明智的程序本身,就像惯用的那样,标头不包含在目标代码中为其分配存储的任何内容)。

I think what you're looking for is an include guard;我认为你正在寻找的是一个包含警卫; try wrapping all of "bh" in somethng like the following:尝试将所有“bh”包装在如下所示的东西中:

#ifndef HAVE_B_H
# define HAVE_B_H

/* contents of "b.h" here */

#endif /* HAVE_B_H */

This allows you to just include it directly from the top of "ah" (as well as "ac"), ensuring it is visible everywhere and not multiply included.这允许您直接从“ah”(以及“ac”)的顶部包含它,确保它在任何地方都可见,而不是多次包含。 Actually this is good practice in general.实际上,这通常是一种很好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM