undefined reference

Hey guys, I'm really trying, but I dont get it.

I get an 'undefined reference' error wenn calling a function that is defined like this.

#define A
//#define B
#include test.h

void setup(){
function();
}

My files looks like these

//test.h
void function();
//test.cpp
#include test.h

#if defined (A)
void function(){}
#endif

#if defined (B)
void function(){}
#endif

How can I get rid of that "undefined reference to `function()'-error?

#if defined (A)
void function();
#endif

You misspelled #ifdef. And lose the parenthesis.

Does also not work with

#ifdef A
void function();
#endif

Does also not work with

Why do you have a function prototype inside an ifdef/endif block? You are NOT calling the function.

The prototype goes in the .h file. You can have two different implementations guarded by ifdefs if you want.

#define A
//#define B
#include test.h

...

//test.cpp
#include test.h

#if defined (A)
void function();
#endif

The #define in the .ino file is not carried through to test.cpp.

You can have one declaration of the function, with 2 implementations.

It doesn't make much sense to me, to declare alternatively two different functions with the same name. That isn't going to do anything useful for you.

Sorry for not describing it exactly. Of course I want 2 different implementations for 2 different MCUs. The functions are not meant to be prototypes. As you can see, the prototype is already in the h.file.

You are NOT calling the function.

Can someone explain?

DonVido: Does also not work with

#ifdef A
void function();
#endif

That does not call the function, so the #ifdef is irrelevant here.

Ah man, come on.

I want to call 'function()' in void setup(). Its declared in 'test.h' and in test.cpp there are 2 different functions with the same name. Which one I need depends on the definition of A and B. How do I solve that problem?

I want to call 'function()' in void setup(). Its declared in 'test.h' and in test.cpp there are 2 different functions with the same name. Which one I need depends on the definition of A and B. How do I solve that problem?

How would you CALL the functions if they had different names?

void functionA(); // Does NOT call the function
functionB(); // DOES call the function

As has been pointed out, the #define statement in the sketch is NOT seen by the library, so you are barking up the wrong tree anyway.

If you were to step back and explain WHAT you want to do, rather than how you insist on doing it, perhaps there are solutions. Obviously, you are not the first person to want to do whatever it is you want to do.

Please have look at post #1 where I wrote how I call the function.

#define A
//#define B
#include test.h

void setup(){
function();
}

I dont want to use different names for the function, because with more and more hardware it becomes more and more complex. You can see 'function()' as a driver. Each sensor for example needs its own driver, but each sensor has to be initialised with the same function in my main code. So why is the #define statement in the sketch is NOT seen by the library and what can I do.

So why is the #define statement in the sketch is NOT seen by the library

The #define statement is in one compilation unit (the .ino file gets converted to a .cpp file to be compiled). The library is a separate compilation unit. When the preprocessor runs, as part of the process of compiling the library's source code, it has no way of knowing that other compilation units contain preprocessor directives.

and what can I do.

It still isn't clear what you want to do. I see a bunch of hand-waving that doesn't convey enough information. But, I'm going to guess that you need a class for each kind of sensor you want to deal with. The class for a given sensor can have a readTheSensor() method. Twelve classes can all have a readTheSensor() method, and the compiler has no trouble selecting the correct readTheSensor() method to call, since the correct one depends on the sensor object that needs to be read.

Enough with the generic names and hand-waving. It's time for some specifics.

DonVido: So why is the #define statement in the sketch is NOT seen by the library and what can I do.

I told you why in reply #5. Any defines in the .ino file are not used when compiling the separate .cpp file.

Solved the problem by putting the define into a cpp-file.

Thanks anyway

Solved the problem by putting the define into a cpp-file.

It would have been smarter to put it in a header file that you could include in the source file and in the sketch.