Using in your project
There are many ways for adding libbase to your project. It is up to you to
decide which one you prefer. Below you will find a recommended (and probably the
easiest) way to do so.
Recommended way (CMake + vcpkg)
This will be 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.txtcmake_minimum_required(VERSION 3.13) project(project-name VERSION 1.0 LANGUAGES CXX) add_executable(project-name "") target_sources(project-name PRIVATE src/main.cc )
src/main.cc#include <iostream> int main() { std::cout << "Hello World!" << std::endl; return 0; }
Initialize
vcpkgand addlibbaselibrary as a dependencyImportant
To avoid name clashes with other libraries, the
vcpkgport 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
libbaseyou want to use by specifying which features you want to enable. To do that, you need to modify the dependency entry in thevcpkg.jsonfile to look like:vcpkg.json{ "dependencies": [ // ... { "name": "ripper37-libbase", "default-features": false, "dependencies": [ // list features that you need here ] }, // ... ] }
Add
libbasedependency and link with it in your CMake script.CMakeLists.txtcmake_minimum_required(VERSION 3.13) project(project-name VERSION 1.0 LANGUAGES CXX) find_package(libbase CONFIG REQUIRED) add_executable(project-name "") target_link_libraries(project-name PRIVATE libbase::libbase) target_sources(project-name PRIVATE src/main.cc )
Use
libbaselibrary in your project.src/main.cc#include <iostream> #include "base/callback.h" int main() { base::BindOnce([]() { std::cout << "Hello World!" << std::endl; }).Run(); return 0; }
Compile, build and run!
$ export VCPKG_ROOT=/path/to/vcpkg $ cmake -S . -b build $ cmake --build build $ ./build/project-name Hello World!
Tip
Repository with the above project can also be viewed here: RippeR37/libbase-example-cmake.