1

I am writing my own code for the Arduino Uno. I compile my code with AVR-gcc, and then upload it with AVRdude. I am not using the Arduino software, but I do have it installed and working on my PC. I want to include some of the Arduino libraries in my project, like Wire.h for example.

I realise that I could either copy the source file to my current working directory or include the entire path, like: #include "/usr/share/arduino/libraries/Wire/Wire.h" but the issue with that is Wire.h includes Stream.h which includes Print.h which includes...you get the idea. These different libraries are not kept in the same place either. I would have to hunt up and down chains of files, copy them and then use them locally. This does not seem like a good option to me.

Including them like I normally would using the IDE doesn't work at all, which is not surprising.

So, how can I use these libraries in my project without all the pain of finding them manually?

  • the 'core' is independent from IDE github.com/arduino/ArduinoCore-avr – Juraj Jul 26 '18 at 7:29
  • @Juraj: I should have clarified, I am writing my own code, compiling with AVR-gcc and uploading using AVRdude. I am not using the Arduino software at all. – hat Jul 26 '18 at 7:36
  • 1
    Juraj's comment is still valid. You can include and compile the Arduino AVR Core without ever having to install or open the Arduino IDE. These are just C/C++ header and implementation files. Just delete the ones you don't need, and add it to your include path. Finally, just add the folder to your build system as an external library, and link it against your final executable. If you don't want to add all the extra code for Print and Stream, you'll have to modify or rewrite the Wire library yourself. – tttapa Jul 26 '18 at 8:30
  • @Jǝssǝ You could have a look at the Cosa command line build (github.com/mikaelpatel/Cosa/tree/master/build). It is a modification of Arduino-Makefile with a few shell script wrappers to make it easy to use (e.g. do not need to write makefiles). It supports build, upload, serial monitor, etc. – Mikael Patel Jul 26 '18 at 13:37
4

Since you will end up using a significant portion of the Arduino core library, the easiest path is to accept the idea that you are now writing “Arduino sketches” rather than plain C++ code. Don't resist, be assimilated, fire the IDE and write setup() and loop(). ;-)

Once you have something working, the next step is to get rid of the IDE, which is admittedly not very nice. A couple of ways to do that:

  • Use Arduino Builder, the command-line tool for building Arduino sketches. The internals of this are the same as for the Arduino IDE, but it works on the command line.
  • Use a Makefile, like Sudar Muthu's Arduino-Makefile. This is a generic Makefile for building Arduino programs. It is not 100% compatible with the IDE or Aduino builder, as it does not add function declarations for you, but it may suit you better if you are used to a more standard toolchain (it's just make + avr-gcc + avrdude).
0

So, how can I use these libraries in my project without all the pain of finding them manually?

you do not need to find them. they are here

  • My point is that without the Arduino software finding and linking all of the needed libraries together, they are not ready to be used without lumping all files needed into one directory. – hat Jul 26 '18 at 16:24
  • that is not true. they are not in one directory for the arduino toolchain. start Arduino IDE, turn on verbose mode and see the gcc compiler and linker commands command line parameters. why do you use command line if you can't set it up? – Juraj Jul 26 '18 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.