简体   繁体   English

来自Arduino库的包含文件冲突

[英]Conflict of include files from an Arduino library

I'm writing Arduino libraries and have the folder structure as follows: 我正在编写Arduino库,文件夹结构如下:

libraries   
  +Foo
    -Foo.h
    -Helper.h   
  +Bar
    -Bar.h
    -Helper.h   
  +Helper
    -Helper.h

Foo and Bar are the libraries I created. Foo和Bar是我创建的库。 The reasoning behind putting Helper.h in their folders is that, end user will have an easier time getting the libraries to work. 将Helper.h放在其文件夹中的原因是,最终用户将更轻松地使库开始工作。 Also, some libraries can only be configured by having their source code edited. 另外,某些库只能通过编辑其源代码来进行配置。

However, if I write #include <Helper.h> in my sketch , it'll be impossible to control which "Helper.h" I'm including. 但是,如果我在草图中编写#include <Helper.h> ,将无法控制要包括的“ Helper.h”。

Is there a way to hide Helper.h from Foo and Bar from sketches? 有没有办法从草图中隐藏FooBar Helper.h

The obvious answer is: don't write #include <Helper.h> . 显而易见的答案是:不要编写#include <Helper.h> Your files aren't part of the implementation, and should be included using #include "Helper.h" . 您的文件不是实现的一部分,应使用#include "Helper.h"包括在内。 If you do this, the first place the compiler will look for the include file is in the directory containing the file which is including it: if you include it from Foo/Foo.h , the compiler will pick up Foo/Helper.h ; 如果这样做,编译器将首先在包含该文件的目录中查找包含文件:如果从Foo/Foo.h包含它,则编译器将选择Foo/Helper.h if you include it from Bar/Bar.h , the compiler will pick up Bar/Helper' and so on. 如果从Bar/Bar.h包含它,编译器将选择Bar/Helper' ,依此类推。

Client code should only set the include path to the root, and do things like #include "Foo/Foo.h" ; 客户端代码应仅将包含路径设置为根,并执行#include "Foo/Foo.h" if necessary, they can also do #include "Foo/Helper.h" . 如有必要,他们还可以执行#include "Foo/Helper.h"

The one thing you do have to do with this strategy is to ensure the uniqueness of all of the header guards. 使用此策略必须做的一件事是确保所有标头防护的唯一性。 If this is an application, it will usually be sufficient to mangle the path into the include guard, eg use Foo_Helper_h , instead of just Foo_h . 如果这是一个应用程序,通常将路径Foo_Helper_h到包含保护中就足够了,例如,使用Foo_Helper_h而不是Foo_h Alternatively (and I would use this for any library which third parties should use), generate some random string for the include guard. 或者(我将把它用于第三方应使用的任何库),为include保护生成一些随机字符串。 (If I open a file named abc.h which doesn't exist, my editor automatically generates the following boilerplate: (如果打开了一个不存在的名为abc.h的文件,我的编辑器会自动生成以下样板:

/********************************************************/
/*      File:       abc.h                               */
/*      Author:     J. Kanze                            */
/*      Date:       02/05/2013                          */
/* ---------------------------------------------------- */

#ifndef abc_h_20130502O481MBxFZeAzz4dgIb7iC4Q9
#define abc_h_20130502O481MBxFZeAzz4dgIb7iC4Q9

#endif

It's a safe bet that the include guard here will never conflict with any other. 可以肯定的是,这里的include防护不会与其他任何冲突。 (And you have to do something along these lines anyway, in order to get your copyright message in the file.) (而且无论如何,您都必须按照这些原则做一些事情,以便在文件中获得版权信息。)

Just figured out a way that seemingly solves the problem: 只是想出了一种看似可以解决问题的方法:

libraries   
  +Foo
    -Foo.h
    +extra
      -Helper.h   
  +Bar
    -Bar.h
    +extra
      -Helper.h   
  +Helper
    -Helper.h

In this way, Helper.h in Foo and Bar would be invisible to clients, and I can write #include "extra/Helper.h" in Foo and Bar respectively to include the desired files. 这样, FooBar Helper.h对客户端是不可见的,我可以在FooBar分别编写#include "extra/Helper.h"以包括所需的文件。

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

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