Arduino Not Recognizing Header File

Hello, I am trying to run a code that one of my colleagues provided me. However, when I try to run it I am getting an error. I have tried using <> with the include statement, as well as "". I have placed the file within the same folder as the .ino file. Below we can see the code from the header file.

#ifndef FreqCount_h
#define FreqCount_h

#include <Arduino.h>

class FreqCountClass {
public:
	#if(!defined(__IMXRT1062__))
		static void begin(uint16_t msec);
	#else
		static void begin(uint32_t usec);
	#endif
	static uint8_t available(void);
	static uint32_t read(void);
	static void end(void);
};

extern FreqCountClass FreqCount;

#endif

If I write #include <FreqCount.h>, I get the following error,
C:\Users\ccruz\OneDrive\Desktop\School\MASc-Research\Code\Ians Code\IRnozzle\IRnozzle.ino:4:10: fatal error: FreqCount.h: No such file or directory
#include <FreqCount.h>
^~~~~~~~~~~~~
compilation terminated.

exit status 1

Compilation error: FreqCount.h: No such file or directory

If I do the same using either just "FreqCount.h" or the full directory location, I recieve the following error,

C:\Users\ccruz\AppData\Local\Temp\ccdAluTj.ltrans0.ltrans.o: In function setup': C:\Users\ccruz\OneDrive\Desktop\School\MASc-Research\Code\Ians Code\IRnozzle/IRnozzle.ino:51: undefined reference to FreqCountClass::begin(unsigned int)'
C:\Users\ccruz\AppData\Local\Temp\ccdAluTj.ltrans0.ltrans.o: In function loop': C:\Users\ccruz\OneDrive\Desktop\School\MASc-Research\Code\Ians Code\IRnozzle/IRnozzle.ino:56: undefined reference to FreqCountClass::available()'
C:\Users\ccruz\OneDrive\Desktop\School\MASc-Research\Code\Ians Code\IRnozzle/IRnozzle.ino:57: undefined reference to `FreqCountClass::read()'
collect2.exe: error: ld returned 1 exit status

exit status 1

Compilation error: exit status 1

If anybody would have any advice on why this error occurs it would be appreciated. Thank you.

Hi @ccruz1000

In this case you must use the double quotes syntax in the #include directive.

This syntax:

#include <filename.h>

causes the include path (e.g., where the libraries are installed) to be searched for the file.

This syntax:

#include "filename.h"

causes the local path to be searched, then the include path if the file was not found in the local path.

What can be very confusing is that, even when the angle brackets #include syntax would be most appropriate, the quotes syntax will almost always work. So you will find that the two syntaxes are used very inconsistently in #include directives for library files. But in this case there is only one usable syntax.

You can use this approach with header files. However, you should be aware that there is a difference between specifying a path vs the header filename alone when it comes to libraries. An #include directive essentially just copies the contents of the referenced file into the file that contains the #include directive. But there is a secondary effect from an #include directive for a filename alone. This effect is that all the source files in the library folder are compiled. That secondary effect does not occur when you provide the path to the file in the #include directive.

In addition, using the absolute path means your code will break if the referenced file is moved, making it fragile and also not very suitable for sharing with others. For this reason, it is better to use the filename alone or a relative path in cases where the file is not in the discovery or include paths.

This is actually progress. It might not look like it because the progress only got you to the next error in the stack of problems you have, but you at least fixed one of the problems by using the appropriate #include directive syntax.

These error messages are accurate. The file FreqCount.h only contains the declarations of the functions. The definitions are missing. Usually the definitions will be in a .cpp file that accompanies the .h file. Did your colleague provide a .cpp file(s)? If so, just put that in the sketch folder along with the .h file and those errors should no longer occur.

Thank you for the advice. Fortunately, I am relatively proficient in C++ programming. I had the same intuition as you about needing a .cc file, but I asked my colleague, and he said that everything should be in the .h file. I do not have any .cc file, and my colleague seems convinced that one doesn't exist. Would it be appropriate to create a .cc file, and copy the contents from the ,h file into it?

Please use the .cpp extension for C++ files. I know there are various conventions for file extensions in the C++ world, but the Arduino framework uses .cpp, as documented here in the Arduino Sketch Specification:

https://arduino.github.io/arduino-cli/latest/sketch-specification/#additional-code-files

That won't help because the code you shared above is only the declarations of the functions. These declarations only tell the compiler the signature of the function. But you also need a definition that provides the actual code of the function. The signature alone doesn't do anything.

A super simple example:

// foo declaration:
int foo(int bar);

// foo definition
int foo(int bar) {
  return bar;
}

Thank you. I will contact my colleague again to ask him to resend me the code.