C++ Usage with CMake and Meson
Prerequisites
Before proceeding, ensure the following are installed and set up:
- C++ Compiler: Ensure you have a C++ compiler installed. For example,
g++
on Linux. - filerix: Ensure the
filerix
library and its development files are installed.
CMake Prerequisites
- CMake: Install CMake. If you don't have it already:
Meson Prerequisites
- Meson: Install Meson. If you don't have it already:
Installing Dependencies
Depending on your distribution, run one of the following commands to install the required build tools:
sudo dnf install cmake make meson ninja
sudo apt install cmake make meson ninja
sudo pacman -Syu cmake make meson ninja
Building the Example with CMake
Navigate to the
example/cpp+cmake
directory:bashcd example/cpp+cmake
Create a build directory to keep the build files organized:
bashmkdir build cd build
Run
cmake
to configure the project:bashcmake ..
Build the project with
make
:bashmake
After building, the executable
example
will be available in thebuild
directory.
Building the Example with Meson
Navigate to the
example/cpp+meson
directory:bashcd example/cpp+meson
Create a build directory to keep the build files organized:
bashmkdir build cd build
Run
meson
to configure the project:bashmeson ..
Build the project with
ninja
:bashninja
After building, the executable
example
will be available in thebuild
directory.
Running the Example
Once the build process completes, run the compiled executable:
bash./example
The example will fetch and log the disk usage information for the specified path (
/
by default). You should see output like this:bashUsed space: <used_bytes> bytes Total space: <total_bytes> bytes
Understanding the Code
The C++ code interacts with the filerix
library to fetch the disk usage of a given directory (/
by default). Here’s a quick breakdown of the key components:
- DriveUtils::GetDriveUsage: This function retrieves the disk usage details (used space and total space) for the given path.
- std::cout: The result is printed to the console for the user to see.
This simple program is designed to showcase how to use filerix
for filesystem operations and how to integrate it into a C++ project with CMake and Meson.
Common Issues
Missing
filerix.so
(Shared Library): If you encounter errors about missing libraries (e.g.,filerix.so
), ensure the library path is correctly set:bashexport LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
If
filerix
was installed manually, ensure the path where it's located is included inLD_LIBRARY_PATH
.Meson or Ninja Errors: If you run into issues during the Meson configuration step, verify that you have the correct
filerix
development package installed and that it's being detected by Meson.You can also try running:
bashmeson .. --reconfigure
This will force Meson to reconfigure the project and show more detailed output to help troubleshoot.