Multi files error

Hello,

I try to make a multi file project (with .c and .h) but i can't compile, i have this message:
exSousFontion.cpp.o: In function loop':** **C:\Program Files (x86)\Arduino/exSousFontion.ino:11: undefined reference to retourneANumber()'

This is my simple exemple:
exSousFonction.ino
#include "toto.h"
void setup(){
}
void loop()
{

  • int A = retourneANumber();*
    }

And the function i add with the tab option:
toto.c
#include "Arduino.h"
#include "toto.h"
int retourneANumber(void)
{

  • return (10);*

}

toto.h
int retourneANumber(void);

I beleive the problem is during the link, but i dont understand why ?

Thank you for your help !

Hi scapel

Change toto.h to this:

#include "toto.c"
int retourneANumber(void);

Change toto.c to this:

#include "Arduino.h"
int retourneANumber(void)
{
  return (10); 
}

Regards

Ray

Thank you Ray, it's working !

But, it is the preconise solution to share a project in several files?

But, it is the preconise solution to share a project in several files?

No. The problem is that you are trying to call a C function from a CPP file. That can be done, but extra steps need to be taken.

It's generally far easier to just name the source file with a cpp extension and let the compiler do the name mangling so that the linker can find the function with the name it is looking for in the resulting object file.

If you must use a c extension, you need to add:

extern "C" {

before any function(s) to be called from cpp files, and

}

after the function(s).

I like PaulS's solution better. Your exSousFonction.ino file should start:

extern "C"
{
#include "toto.h"
}