问题描述
我有一个Arduino UNO和Linux环境。虽然Arduino的IDE是伟大的,所有的,但它并没有出现某些问题给我很大的投入。而且它也变得极为缓慢,并停止与芯片有时沟通。
I have an Arduino Uno and a Linux environment. Though the Arduino IDE is great and all, however it doesn't give me much inputs if something goes wrong. And it also becomes excruciatingly slow and stops communicating with the chip sometimes.
另一种方法是code。使用AVR-GCC / g ++以及AVRDUDE工具链我Arduino的乌诺。不过,我非常希望能够获得同样的功能在Arduino的(如,的等),而使用一个简单的文本编辑器和命令行工具进行编程。
The alternative is to code my Arduino Uno using the AVR-GCC/g++ and AVRDude toolchain. However, I would very much like to have access to the same functions as in Arduino (like Serial, digitalWrite, etc.) while programming using a simple text editor and command line tools.
于是,我就找到头文件与的/ usr /共享/的Arduino /硬件/ Arduino的/核心/ Arduino的
定义,即Arduino.h的所有功能,
So I tried to find the header file with all the functions defined ie "Arduino.h" in /usr/share/arduino/hardware/arduino/cores/arduino
.
我用这个编译(使用makefile来做到这一点)
I used this for compiling (using a makefile to do this)
avr-gcc -mmcu=atmega328p -I. -gdwarf-2 -DF_CPU=16000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=testarduino.o -I/usr/share/arduino/hardware/arduino/cores/arduino -I/usr/share/arduino/hardware/arduino/variants/standard -std=gnu99 -MMD -MP -MF .dep/testarduino.elf.d testarduino.o --output testarduino.elf -Wl,-Map=testarduino.map,--cref -lm
编译此code:
to compile this code:
#include "Arduino.h"
int main(void)
{
init();
pinMode(13,OUTPUT);
while(1){
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
}
return 0;
}
但没有的功能,似乎当我尝试编译被识别(未定义的说法参考XXXXXXXXX)。那么,什么需要被纳入,以确保我可以使用相同的功能确切的头文件是什么?还是有别的东西,我错过了什么?
But none of the functions seem to be identified when I try to compile (saying undefined reference to 'xxxxxxxxx'). So what is the exact header file that needs to be included to make sure I can use the same functions? Or is there something else I missed?
推荐答案
消息未定义引用XXXXXXXXX
意味着链接器无法找到实际的定义的(不只是声明)的函数有问题。
How to get the definitions for undefined reference
The message undefined reference to 'xxxxxxxxx'
means that the linker can't find the actual definition (not just declaration) for the function in question.
在链接器不能找到一个函数的定义,这个定义是通常在库文件被发现(如:*。所以或* .dll文件),或在一个源文件(如:*的.cpp或*。 C)。为了您的Arduino,你需要告诉AVR-GCC编译器和链接的Arduino * .cpp文件。您可以从Arduino的核心需要的所有的源文件。
When the linker can't find a definition for a function, that definition is typically to be found in a lib file (e.g *.so or *.dll) or in a source file (e.g. *.cpp or *.c). For your Arduino, you need to tell avr-gcc to compile and link the Arduino *.cpp files. You need all the source files from the Arduino core.
ARDCOREDIR = <my arduino folder>/hardware/arduino/cores/arduino
Sudar给了很好的意见,提倡使用一个Makefile中为其配置为包括Arduino的功能的定义的车牌自动辨认系统。 (我将链接到矿山及以下解释。)
Sudar gave good advice, advocating the use of a Makefile for AVRs which is configured to include the Arduino function definitions. (I will link to mine and explain it below.)
在我的makefile,我这样处理的操作如下:
In my makefile, I handled this operation as follows:
# CPP_SOURCE_FILES is a previously-defined variable, holding my project's source files
CPP_SOURCE_FILES += $(filter-out $(ARDCOREDIR)/main.cpp, $(wildcard $(ARDCOREDIR)/*.cpp))
注意,我省略文件main.cpp中,因为Arduino的main.cpp中文件定义的main()
,我希望把它定义自己在我自己的项目文件。
Observe that I omitted the file main.cpp because the Arduino main.cpp file defines main()
, and I wish to define it myself in my own project files.
当然,你也必须通过AVR-GCC的包括Arduino的头标志:
Of course, you must also pass avr-gcc an include flag for the arduino headers:
-I$(ARDCOREDIR)
和您必须在您的实际项目code中的Ardhuino.h文件:
And you must include the Ardhuino.h file in your actual project code:
#include <Arduino.h>
该Arduino.h头文件将包含其他所有Arduino的头文件,包括引脚文件,该文件保存了Arduino的引脚定义。不同Arduinos必须有不同的引脚文件,因此确定你使用的是哪种类型,并告诉AVR-GCC包括包含相应的引脚文件的目录。如果您使用的是标准的Arduino(UNO),使用以下命令:
The Arduino.h header file will include all of the other Arduino header files, including the pinout file, which holds definitions for the Arduino pins. Different Arduinos must have different pinout files, so identify which type you are using, and tell avr-gcc to include the directory containing the appropriate pinout file. If you are using a standard Arduino (UNO), use the following:
-I$(ARDCOREDIR)/../../variants/standard
我的Makefile
不仅您的构建过程中需要实现上述几条指令,它需要知道如何使应编程到您的Arduino(或AVR).hex文件。因此,它是用一个makefile一个好主意。
My Makefile
Not only does your build process need to implement the several instructions described above, it needs to know how to make the .hex file that shall be programmed onto your Arduino (or AVR). Therefore, it is a good idea to use a makefile.
我不喜欢重建我的makefile对每一个项目,所以我只是有我的机器上的一个基本的makefile,每个项目都有自己的,很短的Makefile它调用包含
来包括基本的Makefile。你可以找到 href=\"https://gist.github.com/entrity/5424505\">我基地的makefile。
I don't like to rebuild my makefile for each project, so I just have one base makefile on my machine, and each project has its own, very short makefile which calls include
to include the base makefile. You can find my base makefile here.
短(非基础)的示例的Makefile我的机器上一个项目:
# This is the name of the file that shall be created. (It is also the name of my primary source file, without the file extension.)
TARGET = temp
# create a variable that only pertains to this project
MY_OWN_LIBRARY_DIR = /usr/home/MJ/Arduino/libraries/mj_midi
# "EXTRAINCDIRS" is a variable used by the base makefile. The base makefile creates a -I compiler flag for every item in the "EXTRAINCDIRS" list.
EXTRAINCDIRS = $(MY_OWN_LIBRARY_DIR)
# specify *.c source files pertaining to my project
SRC =
# specify *.cpp source files pertaining to my project
PSRC = temp.cpp $(MY_OWN_LIBRARY_DIR)/midi-listener.cpp $(MY_OWN_LIBRARY_DIR)/midi-song.cpp
# specify additional (non-core) Arduino libraries to include
ARDLIBS = SoftwareSerial
# include my base makefile
include /d/dev/avr/Makefile.base
注意:如果你打算用我的makefile基地,确保您设置(出现接近文件的顶部)变量 ARDDIR
为指向你的机器上的Arduino的目录。
NB: If you're going to use my base makefile, make sure that you set the variable ARDDIR
(which appears near the top of the file) to point to the Arduino directory on your machine.
这篇关于使用定期AVR code Arduino的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!