0

This was solved by HolyBlackCat below, my issue was with linking. I think it is important that this question is not simply marked as a duplicate. The solution was not the same as other questions I viewed here, and turned out to be far simpler. My problem was not realizing the library required multiple links to function. All resources I checked online told me my PATH was wrong, or to add -I or -L options, none of which helped. I was only using pkg-config --libs --cflags libpulse to find my necessary links and not pkg-config --libs --cflags libpulse-simple as needed.

I am trying to create a simple program written in C that makes use of the pulseaudio library. This is my file test.c.

#include <stdio.h>
#include <pulse/simple.h>


#define SAMPLE_RATE 44100
#define BUFFER_SIZE 1024

int main(int argc, char *argv[]) {

    pa_sample_spec sample_spec = {
        .format = PA_SAMPLE_S16LE,
        .rate = SAMPLE_RATE,
        .channels = 1
    };

    pa_simple *pulse_input = pa_simple_new(
        NULL,               // Use the default server.
        "My App",           // Name of the application.
        PA_STREAM_RECORD,   // We want to record audio.
        NULL,               // Use the default device.
        "Audio Input",      // Description of the stream.
        &sample_spec,       // Sample format and rate.
        NULL,               // Use default channel map.
        NULL,               // Use default buffering attributes.
        NULL                // Ignore error code.
    );

}

I performed all the necessary set up, but whenever I try to compile the program I get this error:

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\msys64\tmp\ccyzDfRA.o:test.c:(.text+0x72): undefined reference to `pa_simple_new'
collect2.exe: error: ld returned 1 exit status

This doesn't make sense to me as intellisense is finding the header files fine, so I know they exist and when I run gcc -H test.c I can see that C:/msys64/mingw64/include/pulse/simple.h is being found.

I am at a bit of a loss as to why GCC is seemingly unable to read the header files.

To use GCC and compile my code I followed Microsoft's guide to setting up MSYS2. I am using Mingw-w64 rather than UCRT.

I added C:\msys64\mingw64\bin to my PATH.

I used pacman to install pulseaudio, specifically this version: https://packages.msys2.org/package/mingw-w64-x86_64-pulseaudio?repo=mingw64

This version matches the implementation of GCC I am using.

I try to compile test.c using gcc -o test.exe test.c -lpulse but it always seems to fail. I have confirmed that the header files are located in C:\msys64\mingw64\include\pulse

If you have any clue why this is not working, please let me know.

Frank
  • 1
  • 1
  • The error message has nothing at all to do with includes or header files. It is about the linker flags, i.e. `-lpulse` in your case. Appearently you are not linking the correct libraries there. Pulseaudio is separated into multiple libraries (and I think that is also varies from distribution to distribution). – user17732522 Apr 30 '23 at 12:38
  • This is unrelated to headers, but is related to `-l...`. Perhaps try `-lpulse-simple -lpulse`? Or even better, run `pkg-config --libs --cflags libpulse-simple` and use the flags it prints. – HolyBlackCat Apr 30 '23 at 12:39
  • @HolyBlackCat Legend, that fixed it. I can't believe I didn't see anything online about the library being in multiple modules. – Frank Apr 30 '23 at 12:53
  • You're welcome. FYI, haven't used this library before. I googled the function name and found something called "PulseAudio: Simple API". Then I looked at the file list in your link, and found libraries and the `.pc` file with the word "simple" in it, so I figured the function comes from there. – HolyBlackCat Apr 30 '23 at 12:58
  • Thank you, I will make sure to look more closely at what .pc files are present in the future :) – Frank Apr 30 '23 at 13:03

0 Answers0