Opening the cd drive

Hi guys I am following a tutorial to open the cd drive on windows

this is the tut http://z1.invisionfree.com/CPPlearningcommunity/ar/t3914.htm

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


ld.exe||cannot find -lwinmm.lib|

thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <windows.h>


using namespace std;

int main()
{
    mciSendString("set cdaudio door open",NULL,NULL,NULL);
    cout << "open" << endl;

    return 0;
}
Ok I used the following code

1
2
3
4
5
6
7
8
9
10
11
12
13

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
mciSendString("open CDAudio", NULL, 0, NULL);
mciSendString("set CDAudio door open", NULL, 0, NULL);
return 0;
}


which I got from another thread in this forum

and manually added the lwinmm in the linker options

so two questions

1 how come when I tried to add the .lib or .a file it failed yet the command in the linker options worked

2 why do we need to use this option lwinmm in the first place?

thanks
Last edited on
when I tried to add the .lib or .a file

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.

Read about libraries (and more): https://www.daniweb.com/programming/software-development/tutorials/466177/understanding-c-from-source-to-binaries
thanks Repeater great link really goes into depth about the compilation and linking stages

just a quick follow up why isn't the winmm library included when we add <windows.h> ?


like many other .lib files

thanks
Last edited on
#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?
Last edited on
but how come we can call other functions from windows.h without linking to this library in the linkers options?
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.

https://support.microsoft.com/en-gb/help/154753/description-of-the-default-c-and-c-libraries-that-a-program-will-link

https://stackoverflow.com/questions/36017915/visual-studio-2015-default-additional-libraries?rq=1
Last edited on
I'm using code blocks at the moment with mingW compiler

I just find it weird that some functions will work without linking against them and some won't such as the mciSendString function above
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.
Topic archived. No new replies allowed.