1

I'm having difficulties installing the OpenAL audio library in C++. I guess the headers are in the correct folder and the problem is in the lib files, because VS Code doesn't show any error when I include the library (#include <AL/al.h> and #include <AL/alc.h>), but when I try to compile my code from the terminal I get the following error:

C:\Users\ALEXAN~1\AppData\Local\Temp\ccNZ4t3C.o:Proves.cpp:(.text+0x1c): undefined reference to `__imp_alcOpenDevice'
C:\Users\ALEXAN~1\AppData\Local\Temp\ccNZ4t3C.o:Proves.cpp:(.text+0x30): undefined reference to `__imp_alcCloseDevice'
collect2.exe: error: ld returned 1 exit status

Where and how I've installed the library:

I've downloaded and unziped the Windows binaries. Then I've run the cpp -v command to find the C++ include directories, which showed 3 directories. I moved the AL folder (that contains the headers) to the C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\x86_64-w64-mingw32\include directory and the Win32 lib files (called libOpenAL32.dll.a, OpenAL32.def and OpenAL32.lib) to the C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\x86_64-w64-mingw32\lib directory. I'm not sure if I have to put there the 3 of them or just the one called libOpenAL32.dll.a.

I know there are similar posts on stackoverflow and I've read some of them, but it's the first time I install a library in C++ and it's difficult for me to understand them. If someone could provide a clear explanation on how to complete the installation I'd be very grateful.

ARD
  • 333
  • 1
  • 13
  • 1
    Linking and including headers are 2 separate things. Getting the header paths right for the compiler to find the headers is separate from the commands you add to the linker to link to the libraries. – drescherjm Aug 20 '20 at 16:28
  • 1
    ***but when I try to run my code from the terminal*** At that point you are still not running your application because the build has failed. – drescherjm Aug 20 '20 at 16:31
  • @drescherjm And what do I have to do to link it properly? Isn't it enough putting it in the \lib directory? I know this might be dumb questions but I come from Python where it was a lot easier to install libraries and I'm a bit confused – ARD Aug 20 '20 at 16:31
  • 1
    ***Isn't it enough putting it in the \lib directory?*** No absolutely not. The linker won't search for libraries in your project folders unless you tell it to use a library. – drescherjm Aug 20 '20 at 16:31
  • @drescherjm Sorry, I meant "when I try to compile" – ARD Aug 20 '20 at 16:31
  • @drescherjm Ok, then what else I have to do? – ARD Aug 20 '20 at 16:32
  • Related: [https://stackoverflow.com/questions/52910102/vscode-c-task-json-include-path-and-libraries](https://stackoverflow.com/questions/52910102/vscode-c-task-json-include-path-and-libraries) – drescherjm Aug 20 '20 at 16:33
  • Probably something like [this](https://stackoverflow.com/a/4446085/212858) – Useless Aug 20 '20 at 16:33
  • @drescherjm but it's not on my project folder... It's in the folder where chocolatey installed MinGW – ARD Aug 20 '20 at 16:33
  • The linker won't search in either case. It doesn't know what to search for anyways. – drescherjm Aug 20 '20 at 16:34
  • 1
    You have to modify your `tasks.json` like this answer: [https://stackoverflow.com/a/59600574/487892](https://stackoverflow.com/a/59600574/487892) the -L argument is the path to the library folder. The -l is the library file. – drescherjm Aug 20 '20 at 16:36
  • @drescherjm The "library file" is the one ended in ".dll.a", ".def" or ".lib"? – ARD Aug 20 '20 at 16:40
  • 1
    Its the one ending in `.a` but you can omit the extension – drescherjm Aug 20 '20 at 16:40
  • Thank you both, when I get home I'll try what you said and tell you if it worked. – ARD Aug 20 '20 at 16:41

1 Answers1

0

I've found a solution to my problem thanks to @drescherjm and to this answer by @MemzIcon. I've modified a bit the code in the answer so I can use it for any file in the project by changing ${workspaceFolder} by ${fileDirname}.

Moreover, I've created a C:\Libraries directory with two folders: Include, where I'll store my external headers, and Libs, where I'll store my external library files. This way, the libraries I install are not mixed with the other C++ libraries.

This was the first tasks.json I created:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compiler",
            "type": "shell",
            "command": "g++",
            "args": [
                "-c",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.o",
                "-IC:\\Libraries\\Include"
            ]
        },
        {
            "label": "Linker",
            "type": "shell",
            "command": "g++",
            "args": [
                "${fileDirname}\\${fileBasenameNoExtension}.o",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-LC:\\Libraries\\Libs",
                "-llibOpenAL32"
            ]
        },
        {
            "label": "Build OpenAL",
            "dependsOn": [
                "Compiler",
                "Linker"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

However, it didn't work properly, because it executed the second command called "Linker" when the <file>.o hadn't been created yet. In consequence, when I pressed the Ctrl+Shift+B shortcut, the "Linker" command throwed an error because it didn't find the object file. To solve this, I created just ONE command instead of 3. Inside of this command I used the ; symbol to separate the Compiler and the Linker parts. Finally, this is the aspect of my tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build OpenAL",
            "type": "shell",
            "command": "g++",
            "args": [
                "-c",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.o",
                "-IC:\\Libraries\\Include",

                ";",

                "g++",
                "${fileDirname}\\${fileBasenameNoExtension}.o",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-LC:\\Libraries\\Libs",
                "-llibOpenAL32",

            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
ARD
  • 333
  • 1
  • 13