0

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 !

9
  • 3
    Is it a linker error ? Commented Feb 10, 2013 at 14:57
  • @cnicutar I am using the Arduino IDE. Not compiling or linking manually. Commented Feb 10, 2013 at 14:59
  • It's likely you've not configured it correctly to link both files. Commented Feb 10, 2013 at 15:00
  • If you copied everything correctly... isn't one of your files in C++? (like, *.cxx or *.cpp rather than *.c?) Commented Feb 10, 2013 at 15:02
  • @AntonKovalenko No. Why ? The IDE should handle C code correctly. Commented Feb 10, 2013 at 15:04

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

That's not very relevant. You need to find out how to include all 3 .c files into the project in your IDE or whatever it is, so it invokes the compiler and linker with proper parameters.
The IDE abstracts away a lot of the stuff. The Arduino build process links the code to quite a few of its libraries and also specifies many parameters. At the moment, I really cant get into that.
1

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.

1 Comment

i had the same thing happen in arduino. renaming the .c to .cpp fixed it.
0

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.

Comments

0

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

extern void turnoff();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.