Using in your project
There are many ways to add libbase
to your project. It is up to you to
decide which one suits your needs the best. Below you will find a recommended
(and probably the easiest) way to do so.
Recommended way (CMake + vcpkg)
This is a step-by-step guide to create a new project that uses libbase
library using Git, CMake and vcpkg
. If you wish to add libbase
to your
existing project, please skip here.
Create a new Git repository and add initial files.
$ git init $ git add [...] $ git commit -m "Initial commit"
Create a new CMake script and a simple C++ source file.
CMakeLists.txt1cmake_minimum_required(VERSION 3.13) 2project(project-name VERSION 1.0 LANGUAGES CXX) 3 4add_executable(project-name "") 5 6target_sources(project-name 7 PRIVATE 8 src/main.cc 9)
src/main.cc1#include <iostream> 2 3int main() { 4 std::cout << "Hello World!" << std::endl; 5 return 0; 6}
Initialize
vcpkg
and addlibbase
library as a dependencyImportant
To avoid name clashes with other libraries, the
vcpkg
port for this library is calledripper37-libbase
. This is the name you should use when adding it to your project.$ vcpkg new --application $ vcpkg add port ripper37-libbase
Note
You can customize which parts of
libbase
you want to use by specifying which features you want to enable. To do that, you need to modify the dependency entry in thevcpkg.json
file to look like:vcpkg.json{ "dependencies": [ // ... { "name": "ripper37-libbase", "default-features": false, "features": [ // list features that you need here ] }, // ... ] }
Currently available features are:
net
- enables networking module (enabled by default)win
- enables WinAPI integration modulewx
- enables wxWidgets integration module
Add
libbase
dependency and link with it in your CMake script.CMakeLists.txt1cmake_minimum_required(VERSION 3.13) 2project(project-name VERSION 1.0 LANGUAGES CXX) 3 4find_package(libbase CONFIG REQUIRED) 5 6add_executable(project-name "") 7target_link_libraries(project-name PRIVATE libbase::libbase) 8target_sources(project-name 9 PRIVATE 10 src/main.cc 11)
If you want to use optional modules, you need to add corresponding component names to the
find_package()
function call and link with their targets intarget_link_libraries()
.Available components and their target names Module
Component
Target
Networking module
net
libbase::libbase_net
WinAPI integration module
win
libbase::libbase_win
wxWidgets integration module
wx
libbase::libbase_wx
Use
libbase
library in your project.src/main.cc1#include <iostream> 2 3#include "base/callback.h" 4 5int main() { 6 base::BindOnce([]() { std::cout << "Hello World!" << std::endl; }).Run(); 7 return 0; 8}
Compile, build and run!
$ export VCPKG_ROOT=/path/to/vcpkg $ cmake -S . -b build $ cmake --build build $ ./build/project-name Hello World!
Tip
More advanced example project using this method can be viewed here: RippeR37/libbase-example-vcpkg.
Alternative ways
If you don’t want to use vcpkg
to resolve dependencies, you can replace step
3 and not export VCPKG_ROOT
environment variable and use one of these
methods instead:
Manually build and install
libbase
and all of its dependencies in your system. For more details on how to do that, please refer to the building page.Use CMake’s
FetchContent
module to download and buildlibbase
as part of your project. To do this, add below snippet to your CMake script:CMakeLists.txtinclude(FetchContent) FetchContent_Declare( libbase GIT_REPOSITORY https://github.com/ripper37/libbase.git GIT_TAG <commit_or_tag_to_fetch> ) FetchContent_MakeAvailable(libbase)
Caution
This doesn’t auto-resolve
libbase
required dependencies by itself. You will still need to build and install them manually or useFetchContent
to declare and make them available in your CMake project before including thelibbase
library.Tip
Simple example project using this method can be viewed here: RippeR37/libbase-example-fetchcontent.
Use CMake’s
add_subdirectory()
to addlibbase
to your project whil will work similarly to the aboveFetchContent
method. To getlibbase
source files you can download them or addlibbase
as a Git submodule.Terminal$ git submodule add https://github.com/RippeR37/libbase $ git submodule update --init
CMakeLists.txtadd_subdirectory(libbase)
Caution
Similarly to the
FetchContent
method, this also doesn’t resolvelibbase
dependencies by itself. Furthermore, this type of dependency management is not recommended by many - please consider alternatives before choosing it.Tip
Simple example project using this method can be viewed here: RippeR37/libbase-example-submodules.