Reputation: 28090
Arduino has some good C++ libraries which I would like to use on a PC platform. Another advantage is that one can test and debug Arduino code on a PC. It is much easier to do debugging on a PC.
One library I am particularly keen on is the String library. 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? How can this be done?
Upvotes: 0
Views: 1286
Reputation: 83
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. 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". If you are coming from using the Arduino IDE environment you will probably appreciate this.
First of all, some details on the development environment:
So far, so good. Confirm that you can blink the 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.
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. 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.
Now in app_main() add the following lines:
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.
Rebuild your project. You will most likely get some errors of "(something).h: no such file or directory". That is because at the time of this writing, the Arduino core provided by Espressif is not complete. 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. So I removed SD.cpp from the project. 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. This line of code will write to the serial port once every time the LED blinks. Since Serial.println is an Arduino function, you can be assured that at least this Arduino library is up and running. 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).
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. 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. For a related discussion, see this link on sysprogs.com.
Upvotes: 5
Reputation: 345
The String library is mostly free of platform-specific dependencies, so you could simply add WString.h and WString.cpp to your source files. 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; anything that touches peripherals of the MCU are not going to port well.
Upvotes: 3