[英]Using Arduino C++ libraries on PC platform
Arduino has some good C++ libraries which I would like to use on a PC platform. Arduino有一些很好的C ++库,我想在PC平台上使用。 Another advantage is that one can test and debug Arduino code on a PC.
另一个优点是可以在PC上测试和调试Arduino代码。 It is much easier to do debugging on a PC.
在PC上进行调试要容易得多。
One library I am particularly keen on is the String library. 我特别热衷的一个库是String库。 https://www.arduino.cc/en/Reference/String
https://www.arduino.cc/en/Reference/String
Is it possible to somehow import the Arduino String library into a C++ IDE like Visual Studio given that the Arduino library is open-source? 假设Arduino库是开源的,是否可以以某种方式将Arduino字符串库导入到Visual Studio这样的C ++ IDE中? How can this be done?
如何才能做到这一点?
I have been struggling with this issue on and off for the last three months, and I have spent a few hundred hours staring at my computer screen, trying stuff, documenting the results, trying other stuff (repeat). 在过去的三个月中,我一直在努力解决这个问题,并且花了几百个小时盯着我的电脑屏幕,尝试做一些事情,记录结果,然后尝试做其他事情(重复)。 I have done a fair amount of digging on the internet and have found very little that constitutes a definitive resource on this topic.
我已经在互联网上进行了大量的挖掘工作,却发现很少有关于该主题的权威资源。 That being said, here is what I have found that WORKS.
话虽如此,这就是我发现的工作原理。 One caveat is that these instructions are based on ESP32 hardware.
请注意,这些指令基于ESP32硬件。 Note that this is still a work-in-process, as there is some clean-up left to do.
请注意,这仍在进行中,因为还有一些清理工作要做。 Also note that this avoids having to do the manual/command line tasks such as "makefile".
另请注意,这避免了执行手动/命令行任务,例如“ makefile”。 If you are coming from using the Arduino IDE environment you will probably appreciate this.
如果您来自使用Arduino IDE环境,则可能会感激不尽。
First of all, some details on the development environment: 首先,关于开发环境的一些细节:
So far, so good. 到现在为止还挺好。 Confirm that you can blink the LED.
确认您可以使LED闪烁。 Clap your hands, shout with glee, grab a beer, or do whatever else you want to celebrate.
拍拍手,大声欢呼,喝啤酒,或做任何你想庆祝的事情。 At this point we have not really done much to help the importing of Arduino code, so here is where the fun REALLY begins.
在这一点上,我们还没有做很多事情来帮助导入Arduino代码,所以这才是真正有趣的地方。
In your project source code, add the following line: 在您的项目源代码中,添加以下行:
#include "Arduino.h"
Build the project and you should get no errors. 生成项目,您应该不会出错。 Note that in the MSVS Solution Explorer window for your project, under External Dependencies, you will see a list of all the *.h files that you copied into the project folder.
请注意,在项目的“ MSVS解决方案资源管理器”窗口中的“外部依赖关系”下,您将看到复制到项目文件夹中的所有* .h文件的列表。 This won't happen right away, but in the lower left corner of the MSVS app you will see a bunch of things going on in the background, parsing files etc., THEN the External Dependencies will appear.
这不会立即发生,但是在MSVS应用程序的左下角,您会看到许多事情在后台进行,解析文件等,然后将显示“外部依赖关系”。
Now in app_main() add the following lines: 现在在app_main()中添加以下行:
initArduino();
Serial.begin(115200);
If you rebuild the project you will get a few errors during linking, because all of the file references to the *.cpp files are not (yet) included in your project. 如果您重建项目,则在链接过程中会遇到一些错误,因为对* .cpp文件的所有文件引用都尚未包含在项目中。
Rebuild your project. 重建您的项目。 You will most likely get some errors of "(something).h: no such file or directory".
您很可能会收到“(something).h:无此类文件或目录”的错误。 That is because at the time of this writing, the Arduino core provided by Espressif is not complete.
这是因为在撰写本文时,Espressif提供的Arduino核心尚未完成。 In the Error list, note the file location of the error, and REMOVE this file from the project in the Solution Explorer.
在“错误”列表中,记下该错误的文件位置,然后在“解决方案资源管理器”中从项目中删除此文件。 For example, I had the following error: "vfs_api.h: no such file or directory", and the error was located in the file SD.cpp .
例如,我遇到以下错误:“ vfs_api.h:没有这样的文件或目录”,并且错误位于文件SD.cpp中 。 So I removed SD.cpp from the project.
所以我从项目中删除了SD.cpp。 Keep iterating through this step one file at a time until you get a build with no errors.
一次一次遍历一个文件,直到获得没有错误的构建。 Take notes as you remove files (don't delete them) so you can add them back if you make a mistake.
删除文件时记下笔记(不要删除它们),以便在出错时可以重新添加它们。 You may need to do some additional debugging here depending on what source files you added to your project.
根据您添加到项目中的源文件,您可能需要在此处进行一些其他调试。 Be patient and expect a little trial-and-error as you review the Error List that is generated during the Rebuild.
请耐心等待并检查您在重建过程中生成的错误列表时会遇到一些试错。 When you have a project that compiles without errors, celebrate with another beer (or two).
当您的项目编译无误时,可再喝一两啤酒。
In your project source code, add the line: 在项目源代码中,添加以下行:
Serial.println("Hello World!");
I put this in the while(1) loop of the blink-task function. 我把它放在了眨眼任务功能的while(1)循环中。 This line of code will write to the serial port once every time the LED blinks.
每次LED闪烁时,此行代码将写入串行端口。 Since Serial.println is an Arduino function, you can be assured that at least this Arduino library is up and running.
由于Serial.println是Arduino函数,因此可以确保至少此 Arduino库已启动并正在运行。 I believe you should be able to add more #includes (such as Wire.h) to your project and proceed in the same way (but wait on that for now).
我相信您应该能够在项目中添加更多的#include(例如Wire.h),并以相同的方式进行操作(但请稍候)。
Now, before you give me any grief about "dumping" all the library, header, and source files into my project folder, I realize this is not best practice. 现在,在给我关于将所有库,标头和源文件“转储”到我的项目文件夹中的任何悲痛之前, 我意识到这不是最佳实践。 If you created a "bulk libraries" folder as I suggested, you should be able to organize your project better.
如果按照我的建议创建了一个“大量库”文件夹,则应该能够更好地组织项目。 This is left a simple step for the reader.
对于读者来说,这仅是简单的一步。
Since this solution relies on ESP32 hardware and VisualGDB, it won't work for everyone. 由于此解决方案依赖于ESP32硬件和VisualGDB,因此它不适用于所有人。 However it allows you to migrate away from the "mysteries" that take place behind-the-scenes in the Arduino IDE, and allows you to create a foundation for better source control and project development.
但是,它使您能够摆脱Arduino IDE幕后发生的“谜”,并为更好的源代码控制和项目开发奠定基础。 For a related discussion, see this link on sysprogs.com .
有关相关讨论,请参见sysprogs.com上的此链接 。
The String library is mostly free of platform-specific dependencies, so you could simply add WString.h and WString.cpp to your source files. String库大多数没有特定于平台的依赖项,因此您只需将WString.h和WString.cpp添加到源文件中即可。 You will likely have to do a little bit of porting (cleaning up some macros, I suspect), but there's no reason it shouldn't eventually build.
您可能需要进行一些移植(我怀疑是清理了一些宏),但是没有理由不应该最终构建它。
Many other libraries are going to be considerably more difficult to port over to your PC; 许多其他库将很难移植到您的PC上。 anything that touches peripherals of the MCU are not going to port well.
碰到MCU外围设备的任何东西都不能很好地移植。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.