Problems with compiler not finding header files

Hi,

I am having big issued getting a header file included in my code.

I am really puzzled as I have been developing in C for some time in MPLAB and never had an issue with this before.

I find the way the files are included a little strange, there's no clear way to include a file in the build or exclude it!

I have a folder called "example" which contains "i2t.h" and "i2t.c" and the .ino file.

Here's a really short example: ..The main file - no idea where this is physically stored? is this:

include "i2t.h"

void setup() { // put your setup code here, to run once:

}

void loop() { // put your main code here, to run repeatedly: i2t(); }

..My header file - "i2t.h"

void i2t (void);

..And my C file "i2t.c"

void i2t (void) {

}

When I try to compile I get an error:

C:\Users\monty\AppData\Local\Temp\build366599356838748552.tmp\example.cpp.o: In function loop': C:\Program Files (x86)\Arduino/example.ino:10: undefined reference toi2t()' collect2.exe: error: ld returned 1 exit status Error compiling.

It looks like the compiler can't see the i2t function BUT I know the compiler can see the i2t.h file and the i2t.c file. If I type #error in either of them the compiler finds it.

I am guessing I have just missed something blatant here and I'll kick myself later?

Thanks in advance!

MontyBigglesworth: When I try to compile I get an error:

C:\Users\monty\AppData\Local\Temp\build366599356838748552.tmp\example.cpp.o: In function loop': **C:\Program Files (x86)\Arduino/example.ino**:10: undefined reference toi2t()' collect2.exe: error: ld returned 1 exit status Error compiling.

In bold where your sketch is; that is where the header file and the cpp (if applicable) also should be if you include them using double quotes as you do.

PS Next time post your code (and error messages) between code tags; [code]your code or error message here[/code] will result in

your code or error message here

Change i2t.h to:

extern "C" {
void i2t (void);
}

Thanks for the tips guys,

I'll give these suggestions a go tonight!

I've made a note about how to post text/code/error messages on here too :)

One last thing; is it possible to get the IDE to list the file names down the side of the environment (like every other IDE) rather than along the top?

I have about a dozen files, with the headers as well that makes over 20 files, as I am also quite "verbose" in my file naming so the top of the screen is getting rather full! The fact it doesn't group them into .c and .h doesn't help either :(

I am guessing I am stuck with the current format but if it's possible to change it would be great!

Thanks again for the help!

Rob

The alternative is to rename the *.c file to *.cpp.

Plus to avoid errors it’s always a good idea to prevent a header being included more than once like this:-

#ifndef _I2T_H
#define _I2T_H

void i2t (void);

#endif //_I2T_H

Then, if you want to use Arduino stuff in your function(s), you’ll need to include “Arduino.h” in the header:-

#ifndef _I2T_H
#define _I2T_H

#include <Arduino.h>

void i2t (void);

#endif //_I2T_H

One last thing; is it possible to get the IDE to list the file names down the side of the environment (like every other IDE) rather than along the top?

Unfortunately not - what you see is what you get. (Maybe you could turn your monitor on it’s side? :smiley: )

sterretje: In bold where your sketch is; that is where the header file and the cpp (if applicable) also should be if you include them using double quotes as you do.

PS Next time post your code (and error messages) between code tags; [code]your code or error message here[/code] will result in

your code or error message here

Thanks, this seemed to be the main issue.

I did have an #ifndef _I2T_H

define _I2T_H wrapper but I removed it thinking that may have been the error.

I also re-named the c file cpp- it's a C file no a C++ file but I'll live with it if it works.

I still can't get over how complicated Arduino have made the file and folder system! I never thought I'd say it but I really miss MPLAB!

I am guessing the IDE adds all the files in "C:\Program Files (x86)\Arduino" to your project even if they are not listed in the bar at the top?

Next question is how do I change the location of that folder? Logically there should be something in preferences but I can't find anything.

Thanks again for the help guys!

MontyBigglesworth: I also re-named the c file cpp- it's a C file no a C++ file but I'll live with it if it works.

If it's a c file then keep the extension as .c and use my solution.

MontyBigglesworth: I am guessing the IDE adds all the files in "C:\Program Files (x86)\Arduino" to your project even if they are not listed in the bar at the top?

no

MontyBigglesworth: Next question is how do I change the location of that folder? Logically there should be something in preferences but I can't find anything.

File > Preferences > Sketchbook location. Using C:\Program Files (x86)\Arduino is a bad idea because then you will lose everything every time you upgrade to a new version of the Arduino IDE.

pert: If it's a c file then keep the extension as .c and use my solution. noFile > Preferences > Sketchbook location. Using C:\Program Files (x86)\Arduino is a bad idea because then you will lose everything every time you upgrade to a new version of the Arduino IDE.

Okay, the ".c" problem was def my issue, I just went to the main .ino file which I have on my desktop and found Arduino had indeed copied the files I put in the " C:\Program Files (x86)\Arduino" in there.

I'll give your "extern "C" {} " solution another go and see what happens, though if I want it to be portable to other IDEs I think I'll leave it off.

To be honest I only got the Arduino board as an experiment and I think I'll go back to MPLAB after this, no offence to you guys!

I guess the Arduino is set up for C++ as default rather than C like MPLAB.

MontyBigglesworth: I'll give your "extern "C" {} " solution another go and see what happens, though if I want it to be portable to other IDEs I think I'll leave it off.

#ifdef __cplusplus
extern "C" {
#endif
void i2t (void);
#ifdef __cplusplus
}
#endif

I'll give your "extern "C" {} " solution another go and see what happens, though if I want it to be portable to other IDEs I think I'll leave it off.

This is the STANDARD technique for allowing C libraries to be called from C++ code - it IS portable, and it's not an arduino-specific thing.

I guess the Arduino is set up for C++ as default rather than C like MPLAB.

Yep. Or rather, the main sketch and many libraries are C++ programs. The IDE is smart enough to compile C++, C, and asm files with separate rules, based on the file extensions. The IDE is set up to hide the details of the languages used from the users.

westfw: This is the STANDARD technique for allowing C libraries to be called from C++ code - it IS portable, and it's not an arduino-specific thing.

Yep. Or rather, the main sketch and many libraries are C++ programs. The IDE is smart enough to compile C++, C, and asm files with separate rules, based on the file extensions. The IDE is set up to hide the details of the languages used from the users.

Got it, I've only ever used C and assembler so I didn't know about the C++ thing- good to know!

Am I right in thinking I can go on writing C in the C++ files with no adverse effects?

Cheers again for your help!

BTW, is there any way to change the layout of the IDE so your files are listed horizontally down the side in a list? The bar along the top is already with files :(

MontyBigglesworth: BTW, is there any way to change the layout of the IDE so your files are listed horizontally down the side in a list? The bar along the top is already with files :(

You already asked this. I answered it in reply #4. Didn't you believe me?

OldSteve: You already asked this. I answered it in reply #4. Didn't you believe me?

Sorry- missed it at the bottom in my haste! I was pretty frantic at the time! Cheers!

MontyBigglesworth: Sorry- missed it at the bottom in my haste! I was pretty frantic at the time! Cheers!

No problem. It was a bit hard to see, added as an afterthought.

And as I said, you could always lay your monitor on it's side. (Sorry - couldn't help that quip. :D )