Problem with my own library

Hi,

I created a my own library for delay function.So I created two files, one is Delay.c and other is Delay.h file.After that, I created a folder in Arduino/Libraries/Delay.In this Delay folder, I pasted Delay.c and Delay.h files.Then, I go to sketch/include library and there I selected my delay library.After I called delay function in loop().Then It showing "undefined reference to 'Delay_own(unsigned int).So, what is the problem.I tried in different ways,but I didn’t succeed.In below,I attached my files.

Thanks and Regards,
M Sai Kiran.

Delay.c (116 Bytes)

Delay.h (165 Bytes)

C vs C++, perhaps?
See Standard C++

It might be easier to just rename your .c file to .cpp.

The Arduino is programmed using C++. Why is your source file named Delay.c?

Do you understand HOW C++ supports function overloading? If not, google "C++ name mangling". The short story is that the C++ compiler can not directly call C code. You need to use extern "C" {} around any C code that is to be called from C++.

By the way, that delay function will not work. The compiler will optimize away the useless for loop(), and then see that the while loop is useless, and get rid of it. Your function will be left with:

void Delay_own(unsigned int t)
{
 unsigned int i;
}

I’ve not checked but would suggest to rename Delay.c into Delay.cpp

then the IDE will likely do the right thing and link your file with the other files

(of course make sure you have#include <Delay.h>at the top of your sketch)

side note - what’s the intent of your delay function? feels very processor speed dependent even if you were to turn off code optimization. what’s wrong with the standard delay()? (especially in light of the compiler optimization highlighted above by PaulS)

And what is the purpose of that delay library? It's as blocking as using delay() of delayMicroseconds() but this one is completely imprecise...

Thanks for the reply guys.I changed the source file extension as cpp. Now its working.Thank you so much.

take into account the comment though that this for loop as it stands will get just ignored at compile time because the compiler will deem it's unnecessary to waste resources for that :slight_smile:

And thank you for answering all the questions! ::slight_smile: