Using .c and .h files - how?

I made a small program where I created a .c file with a function definition to blink a led. I then made a .h file that contained a function prototype for that function. In the main sketch file I include the .h file in quotes. In loop(), I call functions from the included .h/.c file and I get a compile error that states "In function loop, unidentified reference to functionA() and functionB().

Why does this happen? I am trying to use the same file organization I would use if writing in C on a dedicated micro / compiler. In addition I am trying to use some work I already completed.

I have tried everything to get this to work with no luck. I see others struggling with similar issues and actually using char foo; to attend to correct it. I hope this generic sample explains things clearly.

extern "C" {

// The contents of your header file go here.

}

Pirate959:
I hope this generic sample explains things clearly.

What generic sample?

I made a small program where I created a .c file with a function definition to blink a led. I then made a .h file that contained a function prototype for that function.

How about posting these files, in code tags, than just describing them in prose?

Example:

In file foo.c:

#include <Arduino.h>
void foo ()
{
PORTD = bit (1);
}

Main sketch:

extern "C"
 {
 void foo ();
 }

void setup ()
  {
  foo ();
  }  // end of setup

void loop ()
  {
  }  // end of loop

I don't ever seem to have the need for "extern C" to do this. I don't know why you do.

sketch:

#include "functions.h" 


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

  fun1(3) ;

  int b= fun2( 3.5) ;
}

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

}

functions header file. functions.h

#ifndef FUNCTIONS_H
#define FUNCTION_H

void fun1(int) ;

int fun2(float) ;

#endif

function code. This is function.cpp, maybe the suffix makes a difference.

#include <Arduino.h>
#include "functions.h"

void fun1(int c) 
{
    Serial.println(c);
}

int fun2(float f) 
{
  int d = (int) f ;
  return d ;
}

Yes it does. It applies to C files, not CPP files. (Name mangling).

Oooo, that's like a halfway stage to using a class and library, and might have been an easier way of me doing some bwod stuff over the weekend.

So just to clarify from Nick's example, it seems only foo.c is needed and no foo.h? And where do they go?- do they need to be in the sketch's folder?

And does foo() the function have to be the same name as foo.c the file?

michinyon:
maybe the suffix makes a difference

Probably not much effort to test that theory.

"Name mangling" is the key-phrase when you Google.

Argh! Curse you Nick Gammon! I worked hard writing this now useless post (iPads are horrible devices).

JimboZA:
So just to clarify from Nick's example, it seems only foo.c is needed and no foo.h? And where do they go?- do they need to be in the sketch's folder?

If you make a tab in the IDE, the resulting file will go in the sketch folder. And files found in that folder automatically make up your "project" next time you open it.

The file name is irrelevant. The foo.h would be useful if you had a lot of functions in foo.c, and wanted to use them in multiple places, however it isn't required as such.

In general, the .h files are just useful places to put function prototypes, which are used in many places, so save having to copy and paste them all over the place.

an easier way of me doing some bwod stuff over the weekend.

The Blue Window Of Death?

JimboZA:
So just to clarify from Nick's example, it seems only foo.c is needed...

foo.cpp for C++ (classes).

...and no foo.h?

Header files are not required but are highly recommended. Especially when working with classes. Otherwise you will typically end up with a complete copy of the declarations.

And where do they go?- do they need to be in the sketch's folder?

Yes. (Or in a library.)

And does foo() the function have to be the same name as foo.c the file?

No. In general, the module name (foo.cpp) is almost irrelevant in C / C++. It is irrelevant in regards to the contents (your class).

Thanks CB and Nick, and thanks to the OP for the thread!

OK, I see that michinyon has done basically what I have done. The only difference is I have .c extensions and his example is .cpp. I simply renamed my c file extensions to cpp and the issue is now solved.

Thanks so much for the help!

In the other IDE which I often use, it doesn't seem to matter if I call files c or cpp, it seems to work out what it needs to do. A lot of the work I did used .cxx files, I'm not 100% sure what they are supposed to do.

Yes .....

Well they do different things, so that is kinda surprising. C and C++ are different languages, it seems odd you can just plonk code into the files without caring about their names.

Well they do different things...

By default. gcc allows the language to be forced (-x).

@JimboZA
I use .h files in the library folder to share macros and functions with sketches.
You only have to include the file in a new sketch to gain access to them.

I think Nick shared this idea on his web site a while back (belated thanks).

.

Does .c (vs .h or .cpp) have any effect on whether variable names and #defines within the file are global or not?

Are there any other advantages to using .c for an include file when it compiles and works just fine as .h?