如何编译一个简单的Arduino程序?

时间:2019-04-24 20:32:59

标签: c++ c arduino

我开始学习Arduino /对Arduino进行编程,但我不明白为什么在使用Visual Micro或Arduino IDE在Visual Studio 2017中编译程序时会出现语法错误。

从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 IDE的编译错误:

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.

以下是我的代码:

SketchWiFi.h文件:

#ifndef _SKETCHWIFI_h
#define _SKETCHWIFI_h

int WiFi_Setup();

#endif

SketchWiFi.c文件:

#include "SketchWiFi.h"

int WiFi_Setup()
{
  // put your setup code here, to run once:

  return 1; // Successful
}

Sketch.ino文件:

#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:
}

目录结构:

enter image description here

Visual Studio 2017中的项目结构

enter image description here

Arduino IDE中的项目结构 enter image description here

1 个答案:

答案 0 :(得分:2)

根据发布的建议,我在GitHub上阅读了以下内容:

对草图中具有.ino以外的任何扩展名的文件不进行任何预处理。此外,草图中的.h文件不会自动#include从主草图文件中包含。。此外,如果要从.cpp文件中调用.c文件中定义的函数(例如从草图生成的函数) ,则需要将其声明包装在仅在C ++文件内部定义的“ extern“ C” {}“块中。

所以这解释了我的问题。我将简单的函数“ WiFi_Setup()”放在一个类中,现在它可以成功编译。

class WifiNetwork
{
protected:

public:
    void WiFi_Setup();
};

看来Arduino希望一切都生活在课堂上。