Hey there everyone!
Im trying heavily to implement a private library in the lib folder int o my platformio project but its not compiling and i tried everything i found on that topic in the forums, so im a bit frustrated.
I’m pretty sure – about 86% – that because the hello() function does nothing, the compiler has optimised it away and the linker can’t see it. I might be wrong though – I’m away from my “toys” at present.
If you put some code in it that does something, light an LED for example, does it compile and link?
Whaa-Whaa-Whaaaaaaaaa… Compilers and linkers can be a bit dumb, thou, can’t they… too smart for their own good at times (or is it for our sanity?)…
It’s the file extension… the compiler probably thinks you’re trying to include C code in C++, and it doesn’t match.
If you’re not writing C code, change the file extension of your main library code file from myLib.c to myLib.cpp… otherwise, you need to wrap the include in main.cpp in extern C, like so:
Wow thank you very much for your fast replies! The extern C wrapper did the trick! Thanks @pfeerick! And it looks like the compiler didn’t optimized the empty function away for some reason @NormanDunbar
But another question:
When I’m Referring to other C headers from my mainLib.h do I need to add the extern C header in every header?
Short story: not if those libraries are in files with a .cpp extension.
Long version:
As @pfeerick pointed out, the file extension being .c is causing the C compiler to be used. Had it been .cpp then the C++ compiler would have been used. Your main.cpp is compiled by the latter and your library with the former.
C++ “decorates” function named to show which type of parameters they take as this allows for many functions with the same name, but different parameters, to be defined. C doesn’t – function names must be unique.
Your setup() function was looking for some C++ function internally called something like helloNULL() but only the C hello() was there. The extern "C" stuff tells the compiler not to decorate calls to those functions, or, to decorate them to be called from C++ – one or the other, I forget. (I’m old and decrepit!)
I wouldn’t go so far as that… the glasses were probably just in need of a clean!
Nice… I didn’t know the why, just the what was happening… see… being
has its uses… as you just explained the why
Talking of learning new tricks… I only recently found out about… string literals in C++… didn’t exist when I studied it… I studied formally learnt C++ in 2005/2006 at uni… they didn’t arrive until c++11, and by that point I wasn’t doing programming… handy tool… those string literals… you can basically tell the compiler to leave your alone with complex strings and just throw down a blob of text. i.e.
Only lines 20-22 will count, and will be stored exactly as laid out… no need to escape the double quotes, or backslashes, etc.
In Oracle, those string literals are called “Q Strings”. They are pretty much the same except if your opening delimiter is one of <[{( then the closing one must be one of >]}), otherwise, it’s the same as the opening one. Very handy for embedding “stuff” in a string.
mySQLQuery := q'<SELECT stuff FROM table_x WHERE surname = 'O'Brien'....>';
(I think I got that right! )
No need for escapes etc, great! They do make strings look odd though, when you are used to escaping and doubling up on quotes etc.