-
Notifications
You must be signed in to change notification settings - Fork 14
CMake ‐ Linking 3rd party libraries
vcpkg who's dependencies and installation are all completely managed by the CMake setup, allows us to run find_package()
from CMakeLists.txt
, which finds and defines external libraries thanks to it's library ports list.
Once we find and define a library through vcpkg in CMake, we then include it in target_link_libraries
by pointing to it's declaration.
Important: Libraries also must be defined by their name in vcpkg.json's "dependencies" entry. Check the libraries supported by vcpkg here.
Run the vcpkg initializer:
include(vcpkg-init)
Install vcpkg dependencies:
include(vcpkg-install-deps)
2 default 3rd party libraries are linked for godot-roguelite:
Find 3rd party libs with vcpkg:
find_package(fmt CONFIG REQUIRED)
find_package(spdlog CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME}
PUBLIC godot::cpp
PRIVATE fmt::fmt
PRIVATE fmt::fmt-header-only
PRIVATE spdlog::spdlog_header_only
PUBLIC: Would mean that other targets could access functionality from those libraries as well.
PRIVATE: Means just it's scoped to that binary and nothing else
If you're only building one thing and there aren't libraries that could depend on others mixed in you can tend to just make everything private.
godot::cpp
CMakeLists.txt)
fmt::fmt
Usage CMakeLists.txt
fmt::fmt-header-only
Usage CMakeLists.txt
spdlog::spdlog_header_only
CMakeLists.txt
are all identifiers which are specified in each project's CMakeLists.txt
.
Alternatively you can also find out the library identifiers by reading the CMake process output when opening the VS project after having added the library names to vcpkg.json
, which displays some linking instructions for CMakeLists.txt, including the identifiers.
Libraries that use CMake but aren't registered with vcpkg, and libraries that don't use CMake build system at all are trickier, so THIS SECTION IS A WIP, yet here's some ideas:
Firstly, see if the library you are trying to link has a guide on how to add it to your project, some libraries even provide information on how to link them using CMake (for example on their github page if existing).
If the library also uses CMake as it's own build system, you can try adding it as a submodule. This approach would involve:
-
Running the following command. If you want to keep things tidy, point to the roguelite
extern
folder as location for it:git submodule add <repo url> extern/libraryname
If you want a specific branch from the library run:
git submodule add -b <branch name> <repo url> extern/libraryname
note that the path is relative for this example so you should run this command from the root path for your project).
-
Run the following command for initializing the submodule and fetching the repo code to populate the directory:
git submodule update --init --recursive
-
Add
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extern/libraryname)
in your
CMakeLists.txt
-
Like with the vcpkg approach, don't forget to add the library with
target_link_libraries
The godot-roguelite CMake setup for linking 3rd party libraries also enforces static linking whenever possible with vcpkg thanks to vcpkg-init.cmake