Question

I cannot seem to be able to open the CD Tray. It pops me some error with 'extern C' what does extern mean too?

Thanks! Here's the image! Error in C++ opening CD Tray

Was it helpful?

Solution

"extern C" isn't really relevant here. The actual problem is the "unresolved external" errors on your call to mciSendString(). It means the compiler knows that the function exists (because the declaration has presumably been included in a header). However, it doesn't know where the implementation of that function is.

That usually means you haven't linked to a required external library. Microsoft's documentation indicates that you need the Winnmm.lib library in order to use mciSendString(). You need to specify that library in your project settings, which is usually under something like "Linker -> Input -> Additional Dependencies" in Visual Studio.

OTHER TIPS

extern "C" tells the C++ compiler that the function declaration is a C function. This matters at link time because the C++ compiler generates symbols that are "mangled". For more details on extern "C", see this post: In C++ source, what is the effect of extern "C"?

Your underlying problem is not related to extern "C" though. The linker is telling you that the C function mciSendString() is not found. Your project needs to link to Winmm.lib.

As others have mentioned, the error you are getting indicates that the definition of the function mciSendString cannot be found. If you read the requirements for mciSendString on msdn, you'll see that it requires the winmm.lib library. Below is a demonstration of how you can use the Visual C++ preprocessor directive pragma comment to add the library:

#include "stdafx.h"
#pragma once
#include<windows.h>
#include <mmsystem.h>
#pragma comment (lib, "winmm.lib")
#include <stdlib.h>


int _tmain(int argc, _TCHAR* argv[])
{
    mciSendString(L"set cdaudio door open", 0, 0, 0);
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top