Pages: [1]   Go Down
Author Topic: Including Header Files  (Read 5658 times)
0 Members and 2 Guests are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 1
Posts: 320
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
#include <MyBlink.h>
void setup() {               
  pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
  BlinkFunction();
  delay(1000);              // wait for a second
}
MyBlink.c
Code:
#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
Code:
#include <Arduino.h>
void BlinkFunction(void);

When I compile the sketch I get the following error messages
Quote
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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 721
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 721
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
What directory do you have the files in?
Quote
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.
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 320
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Quote
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.

Quote
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:
Quote
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.

Quote
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?
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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"
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 320
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Quote
What is the "current directory" of the IDE anyway.

It's the current directory of the file.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 197
Posts: 12739
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
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.
Logged

Pages: [1]   Go Up
Jump to: