Using C and C++ in projects

See a video demo here: https://youtu.be/OTcDzEyJw8Y

A demo project is here: https://github.com/swiftforarduino/community/tree/master/contributed%20examples/Using%20C%20functions.swift4a


From Swift for Arduino 4.3 onward, you can add C and C++ files into the build. Either import existing files, drag them into the file list or add new tabs with filenames ending .c or .cpp.

You can also include header files for the .c and .cpp files to use.

Each file is compiled by AVR GCC. standard avr-gcc for files ending .c and avr-g++ for files ending .cpp.

Headers are not automatically imported to swift. To make definitions visible, add a file called main.h (or use the menu option to add a clang import header). This will be used as an import header and definitions will be visible to swift using the clang importer submodule of swift, with the usual restrictions.


WORDS OF WARNING:

We held back on adding this feature for a long time. It is not a good idea to just drag C or especially C++ files in here that you don't understand to give yourself a shortcut to developing a project.

That doesn't make a sustainable or safe approach to development and it goes against the ethos of Swift for Arduino. You will bypass all of the protections put in place that make coding microcontroller code with swift *both* quicker *and* safer in the long run, just to save a bit of time in the short run.

The undefined behaviour you introduce can be very hard to contain unless you're an absolute expert.


Generally it is better to port C or C++ code to Swift. This feature has been added after it was requested many times, to try and help move people from beginner to more experienced with S4A and to allow experienced people to get the extra 1% that might not be available in S4A normally (such as embedded assembly code).

Comments

Popular posts from this blog

Halloween LED lights on a plastic trick or treat cauldron

All about bootloaders

code signing, entitlements, bundles, sandboxes, hardened runtime, notarisation, app store security