Include librairy in a .c file


I have a sketchbook with one main pde file and one tools.c with tools.h file.

In the tools.c I've added "#include <Wire.h>" to get functions from that library, but the compiler says that the file was not found.

It works if I put all functions in main pde but I'd like to make a .c and .h file.



First, the tools.c should probably be tools.cpp.

Second, you need to understand how the Arduino IDE does the compilation and linking. All .pde files are combined into one file. Any include files are used to determine what else needs to be compiled. If the sketch contains an include statement for tools.h, tools.c(pp) will be compiled. Any include files not included by the sketch are not available for inclusion.

So, the sketch needs to also include Wire.h, in order for the tools class/code to be able to include it.

Thanks, it works.

The two errors were :

  • rename tools.c to tools.cpp (because Wire is cpp ?!?)
  • include headers in main pde


You definitely need to include the headers in the sketch. Whether tools.c needed to be renamed to tools.cpp depends on what is in the mysterious tools.h and tools.c files.