0

This is my structure

program/
  src/
    Makefile
    main.ino
    test.h
    ...
  lib/
    i2c.h

In main.ino I include test.h and in test.h I'm trying to include i2c.h but it is just not working, I keep getting:

make: *** No rule to make target 'i2c.h', needed by 'build-uno/main.ino.o'.  Stop.

I tried including like this

// test.h
#include "../lib/i2c.h"

I also tried making a symlink to my lib folder in arduino libraries and adding it as a library in Makefile

sudo ln -s /home/pi/program/lib /usr/share/arduino/libraries/GAVR

# Makefile
ARDUINO_DIR = /usr/share/arduino
BOARD_TAG = uno
ARDUINO_PORT = /dev/ttyAMA0
ARDUINO_LIBS = GAVR
include /usr/share/arduino/Arduino.mk

I also tried making a symlink to lib inside the src directory

ln -s /home/pi/program/lib /home/pi/program/src/lib

None of the above had any effect, the error message stays the same.

How can I include a file from another directory in my program?

8
  • It's a make error not compilation error. If you have rule, for main.ino, where is that i2c.h listed as dependency and it's not in the same directory, it'll fail. Depends on Makefile directory it might be ../lib/i2c.h (or you can ommit this dependency, but if it changes, it won't get recompiled)
    – KIIV
    Nov 19, 2020 at 11:10
  • @KIIV I'm still quite new to c and I don't really know what I'm doing. My Makefile is in the src directory and I have posted the contents of it. I'm not aware how I must specify the lib directory as part of the build process. I found many examples that suggest adding -I to gcc but the arduino makefile does magic and I don't know how to add parameters to the compiler command.
    – php_nub_qq
    Nov 19, 2020 at 11:15
  • 1
    I was using platformio (as there was no command line interface back then). 1. libraries are usually in it's own subdirectory, so it might need something like lib/i2c_lib/i2c.h and ARDUINO_LIBS=/home/pi/program/lib
    – KIIV
    Nov 19, 2020 at 11:21
  • 1
    You may suffer the same problem with arduino-cli because of how Arduino building is done - not in the source directory. You will have to work out what the build directory's path is relative to where your file is. If you are using a proper library, though, then arduino-cli will know that and handle it all for you.
    – Majenko
    Nov 19, 2020 at 11:40
  • 1
    Put it as an answer. It may help someone. And hey, if it works it works. In C there is more opinion than right and wrong.
    – Majenko
    Nov 19, 2020 at 11:59

1 Answer 1

1

This is definitely on the hacky side and not a proper solution to the problem, but it works nevertheless so I'm sharing it.

After making a symlink in the /usr/share/arduino/libraries directory you must make sure that there is a header file with the exact name of the directory, apparently that's how arduino is set up to include them.

In my case my symlink was named GAVR, so I had to create a GAVR.h file inside my lib directory, which would then go ahead and include my i2c.h file and possibly any other file that I'd need.

Also in test.h or anywhere I need to use i2c.h I must include <GAVR.h> instead.
Turns out Also 1 is false, you just need to include <GAVR.h> once in the .ino file and its members will be available everywhere, just like any other library.

Also 2, now that ARDUINO_LIBS is not empty in the Makefile you will need to manually declare all libraries that your code is using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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