Including Header Files

I am having problems with my sketch. I am trying to build a sketch with an INO, C and H files.
The following illustrates my problem.

MyProblem.ino

#include <MyBlink.h>
void setup() {                
  pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
  BlinkFunction();
  delay(1000);              // wait for a second
}

MyBlink.c

#include <MyBlink.h>
#include <Arduino.h>
void BlinkFunction(void) {
  digitalWrite(LED_BUILTIN, HIGH);   // set the LED on
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // set the LED off
}

MyBlink.h

#include <Arduino.h>
void BlinkFunction(void);

When I compile the sketch I get the following error messages

MyBlink.c:1:21: warning: MyBlink.h: No such file or directory

MyProblem.cpp:1:21: warning: MyBlink.h: No such file or directory

The problem seems to be that although the header file (MyBlink.h) is part of the project and is one of the tabs in the IDE, however, it can not be found to be included.

How do I identify include libraries to the IDE for my sketch?

MyBlink.c

Why? The compiler is a C++ compiler, and likes cpp files so much better.

Why are you including Arduino.h in both the header file AND the source file? Isn’t one enough?

Try replacing the <> in the include statements for your header file with “” instead.

What Paul was trying to say before he forgot why people come here and decided to pick on everything in your code except the problem is:

What directory do you have the files in? You should have a directory in the same folder as your sketches called Libraries and the headers and source files go in there.

PaulS:

MyBlink.c

Why? The compiler is a C++ compiler, and likes cpp files so much better. You should rename the file .cpp

Why are you including Arduino.h in both the header file AND the source file? Isn’t one enough?

You only need to include Arduino.h in the header file. You don’t need to include it in the .cpp file although it won’t cause an error and isn’t related to your problem.

Try replacing the <> in the include statements for your header file with “” instead.

See how much easier it is to read and understand without the 'tude.

See how much easier it is to read and understand without the 'tude.

What attitude? I asked some questions. Perhaps OP has good reason for naming the file with a c extension, instead of a cpp extension. Without asking for those reasons, you just want to tell OP what to do.

Perhaps OP has a reason for include Arduino.h in both places. Perhaps not. Asking offers OP an opportunity to explain, or not.

What directory do you have the files in?

the header file (MyBlink.h) is part of the project and is one of the tabs in the IDE,

If the file is a tab in the IDE, there can only be only be one directory it is stored in.

The compiler is a C++ compiler, and likes cpp files so much better.

I don’t know it the compiler like C++ files better than C files but in this case it treats them the same.

MyBlink.cpp:1:21: warning: MyBlink.h: No such file or directory

Also the code is ANSI C code. That is why I gave it a C extension.

Why are you including Arduino.h in both the header file AND the source file? Isn’t one enough?

I included the Arduino.h header because the MyBlink.h was not being included and therefore in order to get ONE I had to include it. I would like to have it only once. When I don’t have it in the C/CPP file I get:

MyBlink.cpp:1:21: warning: MyBlink.h: No such file or directory
MyBlink.cpp: In function ‘void BlinkFunction()’:
MyBlink.cpp:2: error: ‘LED_BUILTIN’ was not declared in this scope
MyBlink.cpp:2: error: ‘HIGH’ was not declared in this scope
MyBlink.cpp:2: error: ‘digitalWrite’ was not declared in this scope
MyBlink.cpp:3: error: ‘delay’ was not declared in this scope
MyBlink.cpp:4: error: ‘LOW’ was not declared in this scope

So Arduino.h was added so as to not distract from the real problem.

Try replacing the <> in the include statements for your header file with “” instead.

That was a good suggestion and it worked.

Do we know why?

The angle brackets for include files mean "look in the standard path" while the quotes mean "look in the current directory then the standard path"

When using the IDE, does any know if it is possible to add directories to the "standard path".

What is the "current directory" of the IDE anyway.

RandallR: When using the IDE, does any know if it is possible to add directories to the "standard path".

Only by modifying the source can you modify what command it calls to add another -I option. I'm not sure about changing the default for gcc (pretty easy search though)

What is the "current directory" of the IDE anyway.

It's the current directory of the file.

RandallR: When using the IDE, does any know if it is possible to add directories to the "standard path".

Certainly. Put the code to be shared in a "library" and include the "library" in your sketch. This should get you started... http://arduino.cc/it/Hacking/LibraryTutorial

I don't know it the compiler like C++ files better than C files but in this case it treats them the same.

It most certainly does not.

Also the code is ANSI C code. That is why I gave it a C extension.

But, you want to call the code from a cpp file. That's what the ino/pde sketch gets converted to.

The C++ compiler, invoked for C++ code (files with .cpp extension) performs name mangling. That is the process that allows C++ to support function overloading. The Print::print() function, for instance, has half a dozen variations, each taking a different argument type. The various argument types are used to create unique function names, so that Print::print(int) and Print::print(double) become two separate functions that are completely unrelated to any class.

The C compiler, invoked for C code (files with .c extension) does not perform name mangling.

As a result, the code generated by the C compiler can not, without some additional statements that are not present in your code, actually be called by a C++ function.

The need to include the Arduino.h file in the MyBlink code was not what I was questioning. Clearly, it IS needed. But, it needs to be included in the MyBlink.h file OR the MyBlink.c(pp) file, not both.

It may not be "proper", but what I do is combine the header and the source file into a single .ino file and place that in the same folder as the main sketch. Since it is an ".ino" file, you can get rid of the function prototypes and just keep any #defines from the header file and the function definitions from the source.

Sorry to get late on this topic, but I think that it's interesting for others to have some explanations about the name mangling process and how to work with it.

Suppose you have a function : void foo (int myParam).

When you save this function in a .c file, then the function becomes available under the name foo. When you save this function in a .cpp file, then the function becomes something like foo_@274. The "_@274" added at the end is the "name mangling". This is necessary with C++, because this language supports a specific mechanism called overriding.

You can create as many methods with the same name (foo), but with different parameters. You could have for example : void foo (int myParam); int foo (void); void foo (void); etc...

in the same program. This would be an error in C, but not in C++. The compiler identifies the correct function to call depending on the parameter used by the caller. Since the all functions have the same name, the compiler must add something to identify them, that's why the real name of the functions within the object file will be something like foo_@213 for the first one, foo_@255 for the second one, etc...

If you want to to be sure that the name is not mangled, then you have to add the following line to your .c/.cpp file extern "C" { void foo (int myParam) }

This will make your function available as "foo" (not foo_something) in your object file.