The str-view
port is available in the vcpkg registry. For a review of how to use ports available in the vcpkg registry review the vcpkg guide. This library offers the following port name and usage file.
The vcpkg.json
file.
{
"dependencies": [
"str-view"
]
}
The usage
file.
str_view provides CMake targets:
find_package(str_view CONFIG REQUIRED)
target_link_libraries(main PRIVATE str_view::str_view)
Then, the header in the .c/.h
files.
#include "str_view/str_view.h"
- Use the provided defaults
- Build the library
- Install the library
- Include the library.
To complete steps 1-3 with one command try the following if your system supports make
.
make str_view [OPTIONAL/INSTALL/PATH]
This will use CMake and your default compiler to build and install the library in release mode. By default, this library does not touch your system paths and it is installed in the install/
directory of this folder. This is best for testing the library out while pointing cmake
to the install location. Then, deleting the install/
folder deletes any trace of this library from your system.
Then, in your CMakeLists.txt
:
find_package(str_view HINTS "~/path/to/str_view-v[VERSION]/install")
If you want to simply write the following command in your CMakeLists.txt
,
find_package(str_view)
specify that this library shall be installed to a location CMake recognizes by default. For example, my preferred location is as follows:
make str_view ~/.local
Then the installation looks like this.
.local
├── include
│ └── str_view
│ └── str_view.h
└── lib
├── cmake
│ └── str_view
│ ├── str_viewConfig.cmake
│ ├── str_viewConfigVersion.cmake
│ ├── str_viewTargets.cmake
│ └── str_viewTargets-release.cmake
└── libstr_view_release.a
Now to delete the library if needed, simply find all folders and files with the *str_view*
string somewhere within them and delete. You can also check the build/install_manifest.txt
to confirm the locations of any files installed with this library.
Once CMake can find the package, link against it and include the str_view.h
header.
The CMakeLists.txt
file.
add_executable(my_exe my_exe.c)
target_link_libraries(my_exe str_view::str_view)
The C code.
#include "str_view/str_view.h"
You may wish to use a different compiler and toolchain than what your system default specifies. Review the CMakePrests.json
file for different compilers.
make gcc-rel [OPTIONAL/INSTALL/PATH]
make install
Use Clang to compile the library.
make clang-rel [OPTIONAL/INSTALL/PATH]
make install
If your system does not support Makefiles or the make
command here are the cmake commands one can run that will allow another generator such as Ninja
to complete building and installation.
# Configure the project cmake files.
# Replace this preset with your own if you'd like.
cmake --preset=default-rel -DCMAKE_INSTALL_PREFIX=[DESIRED/INSTALL/LOCATION]
cmake --build build
cmake --build build --target install
If you do not like the default presets, create a CMakeUserPresets.json
in this folder and place your preferred configuration in that file. Here is my preferred configuration to get you started. I like to use a newer gcc version than the default presets specify.
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 23,
"patch": 0
},
"configurePresets": [
{
"name": "deb",
"displayName": "GMake GCC Debug",
"description": "Generated by GMake with GCC base debug preset.",
"generator": "Unix Makefiles",
"inherits": [
"gcc-deb"
],
"cacheVariables": {
"CMAKE_C_COMPILER": "gcc-12"
}
},
{
"name": "rel",
"displayName": "GMake GCC Release",
"description": "Generated by GMake with GCC base release preset.",
"generator": "Unix Makefiles",
"inherits": [
"gcc-rel"
],
"cacheVariables": {
"CMAKE_C_COMPILER": "gcc-12"
}
},
{
"name": "cdeb",
"displayName": "Ninja clang Debug",
"description": "Generated by Ninja with clang base debug preset.",
"generator": "Ninja",
"inherits": [
"clang-deb"
],
"cacheVariables": {
"CMAKE_C_COMPILER": "clang"
}
},
{
"name": "crel",
"displayName": "Ninja clang Release",
"description": "Generated by Ninja with clang base release preset.",
"generator": "Ninja",
"inherits": [
"clang-rel"
],
"cacheVariables": {
"CMAKE_C_COMPILER": "clang"
}
}
]
}