0

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

Johnaudi
  • 257
  • 1
  • 23
  • 1
    I'd recommend that you copy+paste your source code and the error messages (as text) into your question. It will make it easier for people to find in future if they have a similar problem, and it means your question doesn't depend on an external link. – Peter Bloomfield Jan 10 '14 at 14:28

3 Answers3

5

"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.

Peter Bloomfield
  • 5,578
  • 26
  • 37
3

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.

Community
  • 1
  • 1
sbaker
  • 437
  • 2
  • 7
2

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;
}
transporter_room_3
  • 2,583
  • 4
  • 33
  • 51