2

I'm building an arduino program on linux, using the following Makefile:

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

Currently all of my code is spread among a dozen header files and a single .ino file - it gets compiled and it runs properly when flashed. I know that's not correct though and I'm trying to split my code into the proper structure of source and header files but when I do and try to build it I get the following output:

In file included from gear.c:1:0:
gear.h:19:1: error: unknown type name ‘byte’
 byte GEAR = 3;
 ^

Which leads me to believe that the source file is getting compiled separately and does not know what a byte is, which is supposedly defined somewhere within the arduino library.

What's the proper way to build my project with source files?

3
  • 1
    avrdude is an uploader/downloader ... it does not do any building
    – jsotola
    Feb 16, 2022 at 20:28
  • @jsotola I'm sorry, my mistake.
    – php_nub_qq
    Feb 16, 2022 at 20:45
  • There is a byte datatype in C, its called char. for flipping bits in ardunio you could use bitSet, which allows you to change values of bits in a datatype. arduino.cc/reference/en/language/functions/bits-and-bytes/… not really sure what exactly your doing.
    – j0h
    Feb 16, 2022 at 22:59

1 Answer 1

2

First of all, I recommend you write in C++ (file extension .cpp) rather than in plain C (files in .c). The reason is that the Arduino programming environment is based on C++, some of the libraries are C++-only, and your main .ino file will be compiled as C++ source. Mixing this with C is possible, using some extern "C" declarations, but it is probably more hassle that it's worth.

Now, to answer your actual question, just add thsi near the top of your source files:

#include <Arduino.h>

This includes the type definitions specific to the Arduino environment, together with the core API. This include is added automatically to any file ending in .ino, but you have to add it explicitly to your .cpp files.

3
  • Mixing this with C is possible, using some extern "C" declarations, but it is probably more hassle that it's worth. Are you talking about having to prefix all variable definitions with extern? Even by renaming the file to .cpp I still had to add that or I would get errors that variables were not declared. There's a high probability I'm doing something wrong though, have literally 0 practical experience with C/C++
    – php_nub_qq
    Feb 16, 2022 at 21:37
  • 1
    @php_nub_qq: No, I am not talking about extern. My point is that you cannot access C++ objects (e.g. Serial) from C, and accessing C functions from C++ requires extern "C" declarations. Feb 16, 2022 at 21:41
  • Well for now it seems to work fine with the Wire library. Thanks for mentioning though, if I come across it I'll know what to look up and not waste 3 hours.
    – php_nub_qq
    Feb 16, 2022 at 21:44

Your Answer

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

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