I tried linking Winmm.lib it didn't work so I tried linking Winmm.a didn't work so I tried both of them again separately with lower case w's and no luck
I am using codeblocks IDE with mingw as my compiler
Add it to what? You don't add libraries to anything. They sit there on the hard drive and you use them when you need them.
why do we need to use this option lwinmm in the first place?
The function code - the actual binary instructions that make the processor do things - live in libraries. You have to tell the linker which libraries contain the actual function code. -lwinmm is the instruction to the linker to look inside the library named winmm when it's looking for functions being called.
#include <windows.h> has nothing at all to do with linking against a library.
Nothing.
#include <windows.h> means "copy everything from the text file named windows.h to this location".
That's what it means. It's just that. All the text from the file named windows.h is copied, exactly, as if you copy-pasted it yourself. It's as if you typed it in yourself, character by character.
So why would it have anything to do with linking against a library?
To use functions from inside libraries, you must be linking against them. Visual Studio doesn't do just what you tell it to. It does a lot of things without telling you.
I just find it weird that some functions will work without linking against them
Any function that is in a library will not work without linking against it. That's how it works. If you're not directing your build tools to do that link, something else is.