#include "lib.h"
// Taken from https://stackoverflow.com/a/3974138/1124423
void printBits(size_t const size, void const * const ptr){
//something
}
called from the ino file:
#include <Arduino.h>
#include "lib.h"
void setup() {
int a = 5;
printBits(sizeof(a), &a);
}
void loop() {
// put your main code here, to run repeatedly:
}
And I am getting this error:
test.ino:6: undefined reference to `printBits(unsigned int, void const*)'
Why is this? I have my variable declared and defined in the lib files. Also, why is it thinking unsigned int and void const*? sizeof should return size_t. The code for the printBits is taken from here.
You will often find that compilers do not point to the line with the actual error, more likely it is from something that has been done prior to that line.
If you meant for the #include <Arduino.h> on the condition that LIB_H was defined, this needs to be in a separate #ifdef
This is because size_t happens to be an alias for unsigned int for this specific platform. The linker only sees the actual type, not its aliased name. (Note: on other platforms size_t might be an alias to a different type, so don't count on it being the same as unsigned int.)
They are compatible, but they are still different languages with different rules, so you have to tell the C++ compiler which functions are C functions. This is because in C++, in order to support function overloading, function argument types are part of the function's “name” as seen by the linker. This is not the case in C.
Not really. Since, I am not doing object oriented programming C seemed to be easier than C++. Therefore, I went with C code. Renaming the file worked immediately.
Does that include all the .h files #include(d) by Arduino.h and all the .h files #include(d) by those file, and so on, and so on....? If so, great, good to know, thanks.
I regard myself as a reasonably good hobby programmer in C. I understand a bit of C++ but a lot of it I find difficult to understand. Mostly I write Arduino code as if it were C but keep in mind that occasionally the differences between C and C++ will cause me problems. When that happens I don't generally have difficulty solving whatever the problem is. I suggest that if you are comfortable with C then write as if using C and be aware that there are differences.