Seemingly Simple Header File Problem

Hello All,

I’ve been running up against something that seems really simple, but just isn’t working. I have a main file (which includes Wire.h), and a header file of my creation with the associated .c file. Functions in my .c file can’t see any functions from Wire.h, everything else seems to link up just fine (i.e. my functions to the primary .ino file). Is there something obvious that I’m missing? I’ve tried silly stuff like reordering the include statements and even just including the .c file directly, no joy. I’m using platformIO and I can’t tell if my problems stems from a C problem, or something else. Thanks for looking!

Here’s some code to gaze at:


#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <math.h> 
#include "BWGI2C.h"  //This is my creation


#ifndef BWGI2C_H_   /* Include guard */
#define BWGI2C_H_

int scan(void);
int verifyFreeAddress(int address);  

#endif // BWGI2C_H_


// too long to post in it's entirety, but here's a sample
int scan(void)
    int error, address;

    Wire.begin();  //this is where the compiler says that Wire is undefined

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

The Wire library is C++. Try changing the filename of BWGI2C.c to BWGI2C.cpp.

Thanks for the reply and the note about

.  Unfortunately the rename didn't change the output:

src\BWGI2C.cpp: In function 'int verifyFreeAddress(int)':
src\BWGI2C.cpp:4:5: error: 'Wire' was not declared in this scope
src\BWGI2C.cpp:6:14: error: 'delay' was not declared in this scope
src\BWGI2C.cpp: In function 'int scan()':
src\BWGI2C.cpp:30:5: error: 'Wire' was not declared in this scope

btw...I fixed the omission of the code tag in the original post.

Did you include Wire.h in your .cpp or .h file?

To fix the delay() error you need to add this line to your .h or .cpp file:

#include <Arduino.h>

I hate to be one of those guys...But I swear I tried including wire.h in the BWGI2C.c file before. With that being said, it now compiles. The primary difference now is that BWGI2C.c is now a .cpp file. I also included Arduino.h in the primary file, but I don't think that's what caused it to work.

Thank you so much for your time and for the reminder that I need to relearn everything I've forgotten since my C classes in college (which can be measured in decades ago).

Glad to hear it's working! I think you originally had two problems, trying to use C++ in a C file and also not including the library.

Arduino's great for learning or relearning. I really enjoy working within the constraints of the microcontroller's limited resources.

FYI, if you had the opposite situation where you wanted to include a C file in a .cpp or .ino file (which is turned into a .cpp file by the Arduino IDE after some minor preprocessing) you just need to wrap it in extern "C" {}.