cwinapibuild-errorplaysoundwinmm

Build error while trying to use Winmm.h library


I am trying to make program in C, that will play sound from .wav file. Here's the code that I use:

#include <stdio.h>
#include <Windows.h>
#include "winmm.h"

int main()
{
    printf("Hello World!\n");
    PlaySound(TEXT("music002.wav"), NULL, SND_FILENAME);
    system("pause");
}

To use this library, I downloaded winmm.h and winmm.dll. I added header file to the project file and also added is to "Header Files" in Solution Explorer. I added path to the winmm.dll file in the Proprieties->Linker->General->Additional Library Directories. I also added **#include <Windows.h>, some online sources suggested it's needed to use library. Overall, as far as I know, I did everything I could to add library. All functions are listed as good, there's no error underline in all code.

But here's the problem. Visual Studio doesn't compile code. It just pops out "There were build errors." window and doesn't continue.

What do I do wrong? I'm almost sure, that I'm doing some mistake while adding library, however I cannot figure out what I should do. Thanks for all replies


Solution

  • You don't need to include "winmm.h" there is no such thing.

    Use the Project>Properties command or press Alt+F7 and enter winmm.lib under Linker>Input>Additional Dependencies:

    enter image description here

    Also make sure you read and understand the documentation.


    You also may simply add #pragma comment(lib, "winmm.lib"), for example right after #include <Windows.h>:

    This should compile and link without hassle:

    #include <stdio.h>
    #include <Windows.h>
    
    #pragma comment(lib, "winmm.lib")
    
    int main()
    {
      printf("Hello World!\n");
      PlaySound(TEXT("music002.wav"), NULL, SND_FILENAME);
      system("pause");
    }