How to install a C++ library (OpenAL) - c++

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.

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
}
}
]
}

Related

launch: program "directory of the program " does not exit error

I have a header file, a CPP file that defines member functions of the header file and a caller main file that sets and gets some values. When I run the code in ubuntu 20.04 vs code, I get the error "launch:program does not exist error" all the time.
When it comes to running CPP programs without headers involved, it seems that the program builds and runs but after opening header files inside the project folder, I started to get the same type of error. I create the launch json file and try to build it basically. Should I configure json files for the headers as well. They are in the same folder so I thought this would not be required.
I have run the codes in visual studio already and there it works. As you can see from the terminal window, I get undefined reference error. I haven't uploaded the codes because the class and the member functions and all the data variables seem to match with each other and doesn't give any error. I would appreciate it if anyone would help me with this code, I switched to linux and trying to get used to vs code.
this is the json file I created.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
}
]
}
I followed the exact steps explained in the vs code's Getting started c++ on Linux.

how do I configure VS code to build in Release mode (clang + macOS)

I am new to VS code. I have configured it using Clang on macOS using the provided [VS code documentation] (https://code.visualstudio.com/docs/cpp/config-clang-mac)
It works, I can build and debug. Great!
My question is: how do I configure VS Code to build in Release mode? I don't seem to be able to find any info in the documentation or a tutorial on how to do it
In case it helps, this is the way tasks.json looks right now
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"-v",
"${file}",
"${workspaceFolder}/source/*.cpp",
"${workspaceFolder}/FFTreal/*.cpp",
"-I.",
"-L",
"${workspaceFolder}/BassLibrary",
"-lbass",
"-o",
"${workspaceFolder}/final.out",
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Any idea? Thanks!
Sorry, for the last answer...
So first of all, I need to claim that I'm not really familiar to Clang Family, but I did some research and found that there are two special groups out in tasks.json which VSCode provides: Build and Test. So, I guess that we need to build it in release mode manually where again I don't have any confidence on me...
I found that you can build your Clang scripts using CMake. CMake is simply a builder tool to help you reassemble your whole code, including libraries, dependencies, modules etc.
So, I'm not sure if that'll be possible but you can change your tasks.json configuration from Clang++ to CMake, maybe?
First of all, for a quick answer, I edited your tasks.json snippet to this:
{
"tasks": [
{
"label": "cmake init",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}"
},
"command": "/path/to/cmake",
"args": [
"-S",
"${cwd}",
"-B",
"${cwd}/build"
],
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "cmake release builder",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}"
},
"command": "/path/to/cmake",
"args": [
"--build",
"${workspaceRoot}/build",
"--config",
"Release"
],
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
What it does is, basically CMake scans your project folder and create configuration files via the cmake init task. After that, when you run cmake release builder, the task will assume that you have a build folder - which you can change easily - and creates a compact executable file, in build/Release.
But also, in order to use CMake, you need to create a CMakeLists.txt file whose baseName is case-sensitive. So, I created a really simple CMake configuration file which you can add your source files manually, unfortunately, like this:
cmake_minimum_required(VERSION 3.13) # CMake version check
project(simple_example) # Create project "simple_example"
set(CMAKE_CXX_STANDARD 14) # Enable c++14 standard
set(SOURCE_FILES test.cpp add.cpp) # Append source files
# Add executable target with source files listed in SOURCE_FILES variable
add_executable(simple_example ${SOURCE_FILES})
Finally, to make this process REAL, you need to go Terminal > Run Build Task in VSCode.
I'm not really sure that you can use Clang compiler to build a whole application, like an .exe file in Windows. During the research, I focused a lot on the difference between compiler and builder which I recommend to look it up!
I hope that I used English in a comprehensible way and not made confused.

Why is "C/C++:" being inserted into the label of my tasks.json file in Visual Studio Code?

I am using Visual Studio Code with MinGW-w64. This involves creating two files (launch.json and tasks.json) that allow me to build and debug my C++ code. I select my tasks.json file by going to Terminal --> Configure Default Build Task... and then selecting "C/C++: g++.exe build active file."
Normally, the tasks.json file appears as follows:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
However, in the last few days, it has appeared as follows:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
When the file is in this format, I'm not able to debug my code. Instead, I get the message "Could not find the task 'g++.exe build active file'.
The only apparent difference is that C/C++is now preceding g++.exe build active file in the "label" line. Once I delete this label, I'm able to get the code to compile and/or debug.
This leads me to ask two questions: first, why is C/C++ being inserted in the label? It doesn't appear in the Microsoft documentation for setting up MinGW with Visual Studio Code: https://code.visualstudio.com/docs/cpp/config-mingw
Secondly, how am I able to permanently remove C/C++ from my label so that I don't have to manually delete it each time? I have tried choosing Configure Task next to C/C++:g++.exe build active file in the Configure Default Build Tasks in the dropdown menu; removing C/C++, and then saving the tasks.json file, but this doesn't appear to cause the build task to change permanently.
Thank you as always for your help.
It took me a while to read the source code of the cpptools extension, and then I found some problems. Under different conditions, the extension has different response
If there is ".vscode" folder in the folder root directory, then when adding the debug configuration, it will only guide the creation of launch.json.The user needs to be guided to configure "tasks.json" again.But the name of the task created at this time is not consistent with value of "preLaunchTask". This is a problem with this extension. I think the reason is that the ".vscode" folder was not found when initializing the debug configuration, some conditions are missing when the extension's code is executed
If there is a empty ".vscode" folder in the root directory of the folder, "launch.json" and "tasks.json" will be created at the same time when initializing the debug configuration.The two files are exactly matched and do not need any modification
If the user configures the task as first.When initializing the debug configuration, the extension will recreate a task called "g++.exe build active file" which is not start with "C/C++".This of course works, but you may need to delete the task created at the beginning whichhas not been used
This design is reasonable because "C/C++: g++.exe build active file" is actually a task template built into cpptools (there are two others). The built-in templates can be referenced by "preLaunchTask", but the user cannot see them in "tasks.json"
If you delete the file "tasks.json" and change the value of "preLaunchTask" to "C:C++: g++.exe build active file".It also works,but this is not a standard usage
I think this is a mistake of them, it started to appear after a recent version

How to get debugging c++ to work in VSCode on a mac?

Can someone explain how to get building and debugging to work in VSCode on a Mac?
Let's assume we successfully installed cpp tools:
-Including creating a proper task file that works on a mac.
-The required changes to launch.json
-Any other step required.
(Don't get me wrong, I'm not lazy, I have been trying for more then 2 hours now and it seems that a proper answer for this question can help a lot of people.)
Once you have the C/C++ extension downloaded, you can use the configurations to generate a project.json in debug window of VsCode. If you do not currently have a project.json under the project's .vscode folder, hit F5 and a dropdown list should show up. There you can select C++ (GDB/LLDB), and this should generate a project.json for you to use.
If you want to just hit F5 so it automatically compiles and debugs your program, you will need to add a tasks.json. This can be done by hitting F1 and selecting Tasks: Configure Task Runner and select Other. Replace "echo" with "gcc" (or clang) and replace the args with your .cpp files and don't forget to add -g.
You can find more information in their documentation: https://code.visualstudio.com/docs/languages/cpp
I don't count the time I lost looking for an answer to this question!
I found the vscode-lldb extension and it works fine, all other solutions I found don't work for me.
You still have to create configuration files, here are mine to debug my unit tests:
I'm using googletest and extension c++14 in this example
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build & debug tests",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-std=c++14",
"-I/src",
"-lgtest",
"tests/MainTest.cpp",
"-o",
"bin/testMain"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [
"$gcc"
]
}
]
}
launch.json
{
"version": "0.2.0",
"configurations":
[
{
"name": "Debug Tests C/C++",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/bin/testMain",
"args": [],
"cwd": "${workspaceFolder}/tests",
"preLaunchTask": "build & debug tests"
}
]
}

How do I install PDCurses in Windows for use with C++?

I want to use it in some of my programs instead of the standard IOStream.
Also, does NCurses work on Windows, and if so, any better?
Download the zip file, unpack it wherever you typically put external libraries, and check the readme, which tells you the following:
PDCurses has been ported to DOS, OS/2, Win32, X11 and SDL. A directory containing the port-specific source files exists for each of these platforms. Build instructions are in the README file for each platform.
The readme file in the Win32 directory tells you that there are makefiles for several different compilers. In short, you run make:
make -f makefilename
It tells mentions a couple of options you can set, including WIDE and UTF8.
To then use the library, add the directory that contains curses.h to your include path and link with the pdcurses.lib file that make generates for you. How you modify your include path and your linked libraries depends on your development environment and is largely irrelevant to PDCurses.
On VSCode
[Step 1] Install MinGW :
MinGW installation steps
^(make sure you followed the steps carefully)
[Step 2] BUILD PDCurses:
Download PDCurses-master.zip and extract the content
Open\Run MSYS2 MinGW 64-bit (or MSYS2 MinGW 32-bit^1)
cd into the wincon folder and run make -f Makefile WIDE=Y DLL=Y source
[Step 3] Copy Files:
If you followed the steps above so far correctly, there should be 2 specific files inside wincon folder called pdcurses.a and pdcurses.dll
rename pdcurses.a to libpdcurses.a
copy pdcurses.dll into C:\msys64\mingw64\bin
copy libpdcurses.a into C:\msys64\mingw64\lib
copy curses.h and panel.h form PDCurses-master folder into C:\msys64\mingw64\include
[Step 4] Build an example:
Install the C/C++ extension
Follow those steps to create a working enviroment inside VSCode
Add "-lpdcurses" under "args": into tasks.json
and you are Done (at least those steps worked for me)
Extra
you can also just manually build an example by runing g++ your_example.c -o your_example -lpdcurses inside MSYS2 MinGW 64-bit terminal if you want so [...]
^1 if you want to build for 32 systems a good rule is to follow all steps above but wherever you see 64 replace it with 32
demos / examples
how things should look like:
c_cpp_properties.json
{
"configurations": [
{
"name": "Win64",
"includePath": [
"${default}"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/msys64/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-lpdcurses"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
I finally made it. First Build/Compile the Source according to docs.
make -f Makefile # did for me, Windows 10
Copy curses.h and panel.h into your include folder. And, Copy wincon/pdcurses.a into your lib folder. Rename pdcurses.a to libpdcurses.a. (Because it's the standard).
Now, You can include curses.h and compile it like this.
g++ main.cpp -lpdcurses