简体   繁体   English

无法为Arduino编译多个文件

[英]Can't compile multiple files for Arduino

I'm having an issue with compiling code for Arduino if the code is in multiple files. 如果代码在多个文件中,我遇到了为Arduino编译代码的问题。 What I have been doing in the past is have a script concatenate the files in another directory and make the project there. 我过去一直在做的是让脚本连接另一个目录中的文件并在那里make项目。 I would like to be able to compile directly from my build folder without having to jump through hoops of making sure everything is defined in the right order, etc. 我希望能够直接从我的构建文件夹编译,而不必跳过确保按正确顺序定义所有内容的箍,等等。

I'm using avrdude to compile from Linux command line, because the Arduino IDE doesn't work very well with my window manager. 我正在使用avrdude从Linux命令行编译,因为Arduino IDE与我的窗口管理器不能很好地工作。 When I make with multiple files (with appropriate #include statements, I get errors of the following nature, but for all of my methods and variables. 当我使用多个文件(使用适当的#include语句)时,我会得到以下性质的错误,但对于我的所有方法和变量。

./../lib/motor.ino:3:21: error: redefinition of ‘const long unsigned int MOVE_DELAY’
./../lib/motor.ino:3:21: error: ‘const long unsigned int MOVE_DELAY’ previously defined here

The only other place that MOVE_DELAY is used is inside the void loop() function, and it doesn't redefine it there. 使用MOVE_DELAY的唯一其他地方是在void loop()函数内部,并且它不会在那里重新定义它。 The code also compiles fine if concatenate it into one file and run make in that directory, but not if they are in separate files with includes. 如果将代码连接到一个文件并在该目录中运行make ,代码也可以编译好,但如果它们在包含的单独文件中则不会编译。

I believe your problem is solvable by declaring the objects with the "extern" prefix or external. 我相信你的问题可以通过声明具有“extern”前缀或外部的对象来解决。 For example. 例如。 I often use the SdFat library, in which it is included in both my main sketch and instanced in other libraries. 我经常使用SdFat库,它包含在我的主草图和其他库中的实例中。

/**
 * \file test.ino
 */
#include <SdFat.h>
#include <foo.h>
SdFat sd;
...

Where I also use the same object in other libraries, such as foo.h. 我也在其他库中使用相同的对象,例如foo.h.

/**
 * \file foo.h
 */
#include <SdFat.h>
extern SdFat sd;
...

If it was not for the prefix of "extern" it would error like yours, as "sd" can not exist twice. 如果它不是“extern”的前缀,它会像你的错误一样,因为“sd”不能存在两次。 Where the extern prefix tells the linker don't make a new instantiation, rather link to the externally instance elsewhere. extern前缀告诉链接器不进行新的实例化,而是链接到其他地方的外部实例。

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

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