-->

C++ Undefined reference to MIDI function

2020-05-06 13:30发布

问题:

I simply want to print the amount of connected MIDI inputs. What in the world am I doing wrong?

Using Code::Blocks and GNU GCC Compiler.

#include <windows.h>
#include <mmsystem.h>
#include <stdio.h>

int main() {
    printf("%d", midiInGetNumDevs());
    return 0;
}

I get undefined reference to `midiInGetNumDevs@0' upon compiling.

midiInGetNumDevs

回答1:

You need to link with with winmm.lib. In Visual Studio, you do this by adding it to the Additional Dependencies in your project properties.

Right-click on the project, select Properties, then Linker, then Input. Add winmm.lib to the list of files in Additional Dependencies.

Edit: just noticed you are using GCC. In this case, maybe the solution linked in the comments would be better. Add #pragma comment(lib, "winmm.lib") after your headers.



回答2:

If you do take a look at midiInGetNumDevs you will see that it requires Winmm.lib. You will need to add it to your projects so that the function can get linked to it.