c++clionjetbrains-ideplaysound

PlaySound in C++ using CLion


I am trying to make a simple C++ program that plays music, but I'm getting this error.

undefined reference to __imp_PlaySoundA'

From what I read I found out that I don't have any source code that implements PlaySound. I know I should add a reference to winmm.lib to my linker, but I only found how to do this in VisualStudio. Is there a way how to do it in CLion? I'm not even sure if CLion has something like that.

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

int main() {  
    PlaySound("file_name.mp3", nullptr, SND_FILENAME | SND_ASYNC);
    return 0;
}

This doesn't help:

What is an undefined reference/unresolved external symbol error and how do I fix it?


Solution

  • I have already solved it, you need to add winMM.Lib to the CMake file. Plus the file can't be .mp3 but .wav (maybe some others, I did't try).

    # Link to GLFW, GLEW and OpenGL
    target_link_libraries(template PUBLIC
            ${GLFW_LIBRARIES}
            ${GLEW_LIBRARIES}
            ${OPENGL_LIBRARIES}
            winMM.Lib)