我开始学习Arduino /对Arduino进行编程,但我不明白为什么在使用Visual Micro或Arduino IDE在Visual Studio 2017中编译程序时会出现语法错误。
sketch\SketchIncomeEligible.ino.cpp.o:(.text.setup+0x0): undefined reference to `WiFi_Setup()'
sketch\SketchIncomeEligible.ino.cpp.o: In function `setup':
C:\Users\C113850\source\repos\Income_Eligible_Price_Display\src\SketchIncomeEligible\SketchIncomeEligible/SketchIncomeEligible.ino:6: undefined reference to `WiFi_Setup()'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Adafruit Feather HUZZAH ESP8266.
Arduino: 1.8.9 (Windows 10), Board: "Adafruit Feather HUZZAH ESP8266, 80 MHz, Flash, 4M (1M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"
sketch\SketchIncomeEligible.ino.cpp.o:(.text.setup+0x0): undefined reference to `WiFi_Setup()'
sketch\SketchIncomeEligible.ino.cpp.o: In function `setup':
C:\Users\C113850\source\repos\Income_Eligible_Price_Display\src\SketchIncomeEligible\SketchIncomeEligible/SketchIncomeEligible.ino:6: undefined reference to `WiFi_Setup()'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Adafruit Feather HUZZAH ESP8266.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
以下是我的代码:
#ifndef _SKETCHWIFI_h
#define _SKETCHWIFI_h
int WiFi_Setup();
#endif
#include "SketchWiFi.h"
int WiFi_Setup()
{
// put your setup code here, to run once:
return 1; // Successful
}
#include "SketchWiFi.h"
void setup() {
Serial.begin(115200);
Serial.println("Begin setup");
WiFi_Setup();
Serial.println("End setup");
}
void loop() {
// put your main code here, to run repeatedly:
}
目录结构:
Visual Studio 2017中的项目结构
答案 0 :(得分:2)
根据发布的建议,我在GitHub上阅读了以下内容:
对草图中具有.ino以外的任何扩展名的文件不进行任何预处理。此外,草图中的.h文件不会自动#include从主草图文件中包含。。此外,如果要从.cpp文件中调用.c文件中定义的函数(例如从草图生成的函数) ,则需要将其声明包装在仅在C ++文件内部定义的“ extern“ C” {}“块中。
所以这解释了我的问题。我将简单的函数“ WiFi_Setup()”放在一个类中,现在它可以成功编译。
class WifiNetwork
{
protected:
public:
void WiFi_Setup();
};
看来Arduino希望一切都生活在课堂上。