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 vcpkg and add
libbase
library as a dependency$ vcpkg new --application $ vcpkg add port libbase
Note
You can customize which parts of
libbase
you want to use by specifying which features you want to enable. It is also recommended to disable not needed default features (examples and tests) by modifying the dependency in thevcpkg.json
file to look like:vcpkg.json{ "dependencies": [ // ... { "name": "libbase", "default-features": false, "dependencies": [ // list features that you need ] }, // ... ] }
Add
libbase
dependency 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
libbase
library 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.