asheeshr
asheeshr

Reputation: 4114

Function declared in header not accessible in main file

I have a file structure as follows:

interface.h --> interface.c
      |
      |
effects.h --> effects.c
      |
      |
    main

However, functions declared in effects.h are not accessible in main.

Code snippets :

main :

#include "interface.h"
#include "effects.h"
void setup()  //Initialize all variables here
{

....
turnoff();
};

effects.h :

#ifndef EFFECTS
#define EFFECTS
void turnoff();
#endif

effects.c :

#include "interface.h"
#include "effects.h"
void turnoff()
{
....
};

interface.h :

#ifndef INTERFACE
#define INTERFACE
....
#endif

Error message : In function ``loop':undefined reference to ``turnoff()'

The error message doesnt make sense as loop function is empty !

Upvotes: 0

Views: 540

Answers (4)

Udo Klein
Udo Klein

Reputation: 6882

I think the IDE wants *.cpp files instead of *.c files.

Anyway you should change the settings under file->preferences to get verbose compiler output. Usually this gives some hints. At least it shows you the temporary directory that contains the files that are actually compiled. This in turn allows much more precise analysis of the issue.

Upvotes: 1

jpmuc
jpmuc

Reputation: 1154

what are the flags you are using? maybe you need to declare your functions as extern explicitly?

extern void turnoff();

Upvotes: 0

Ganesh
Ganesh

Reputation: 5980

From the latest response, I feel that effects.c is not part of the compilation. I am not aware of the development environment, but from the available data, this is my observation.

Upvotes: 0

Alexey Frunze
Alexey Frunze

Reputation: 62058

You need to compile and link all 3 .c files together. With gcc it's as simple as

gcc main.c interface.c effects.c -o executable_name

Upvotes: 1

Related Questions