[英]How to install a C++ library (OpenAL)
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++ 中安装 OpenAL 音频库时遇到困难。我猜标题在正确的文件夹中,问题出在 lib 文件中,因为当我包含库时 VS Code 没有显示任何错误( #include <AL/al.h>
和#include <AL/alc.h>
),但是当我尝试从终端编译我的代码时,出现以下错误:
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 .我已经下载并解压缩了Windows 二进制文件。 Then I've run the cpp -v
command to find the C++ include directories, which showed 3 directories.然后我运行cpp -v
命令来查找 C++ 包含目录,其中显示了 3 个目录。 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.我将AL
文件夹(包含标题)移动到C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\x86_64-w64-mingw32\include
目录和 Win32 lib 文件(称为libOpenAL32.dll.a
, OpenAL32.def
和OpenAL32.lib
) 到C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\x86_64-w64-mingw32\lib
目录。 I'm not sure if I have to put there the 3 of them or just the one called libOpenAL32.dll.a
.我不确定我是否必须将其中的 3 个放在那里,或者只放一个名为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.我知道在stackoverflow上有类似的帖子,我也看过其中的一些,但是我是第一次在C++安装一个库,我很难理解它们。 If someone could provide a clear explanation on how to complete the installation I'd be very grateful.如果有人能就如何完成安装提供明确的解释,我将不胜感激。
I've found a solution to my problem thanks to @drescherjm and to this answer by @MemzIcon.感谢@drescherjm 和@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}
.我对答案中的代码进行了一些修改,因此我可以通过将${workspaceFolder}
更改为${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.此外,我创建了一个C:\Libraries
目录,其中包含两个文件夹: Include
和Libs
,我将在其中存储我的外部标头文件,我将在其中存储我的外部库文件。 This way, the libraries I install are not mixed with the other C++ libraries.这样,我安装的库就不会与其他 C++ 库混在一起。
This was the first tasks.json
I created:这是我创建的第一个tasks.json
:
{
// 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.但是,它无法正常工作,因为它在<file>.o
尚未创建时执行了第二条名为“Linker”的命令。 In consequence, when I pressed the Ctrl+Shift+B
shortcut, the "Linker" command throwed an error because it didn't find the object file.因此,当我按下Ctrl+Shift+B
快捷键时,“链接器”命令抛出错误,因为它没有找到 object 文件。 To solve this, I created just ONE command instead of 3. Inside of this command I used the ;
为了解决这个问题,我只创建了一个命令而不是 3 个。在这个命令中我使用了;
symbol to separate the Compiler and the Linker parts.用于分隔编译器和 Linker 部分的符号。 Finally, this is the aspect of my tasks.json
:最后,这是我的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
}
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.