2019-08-03 06:42:17 +08:00
# Overlay triplets example
2019-07-18 02:34:31 +08:00
2019-08-03 06:42:17 +08:00
## Building dynamic libraries on Linux
2019-07-18 02:34:31 +08:00
2019-08-03 06:42:17 +08:00
Using **vcpkg** you can build libraries for many configurations out of the box. However, this doesn't currently include shared libraries on Linux and Mac OS.
2019-07-18 02:34:31 +08:00
2019-08-03 06:42:17 +08:00
This doesn't mean that you cannot use **vcpkg** to build your dynamic libraries on these platforms! This document will guide you through creating your own custom triplets with `--overlay-triplets` to easily build dynamic libraries on Linux.
2019-07-18 02:34:31 +08:00
2019-08-03 06:42:17 +08:00
### Step 1: Create the custom triplet files
2019-07-18 02:34:31 +08:00
To save time, copy the existing `x64-linux.cmake` triplet file.
2019-08-03 06:42:17 +08:00
```sh
~/git$ mkdir custom-triplets
~/git$ cp vcpkg/triplets/x64-linux.cmake custom-triplets/x64-linux-dynamic.cmake
2019-07-18 02:34:31 +08:00
```
And modify `custom-triplets/x64-linux-dynamic.cmake` to match the contents below:
2019-08-03 06:42:17 +08:00
```cmake
# ~/git/custom-triplets/x64-linux-dynamic.cmake
2019-07-18 02:34:31 +08:00
set(VCPKG_TARGET_ARCHITECTURE x64)
set(VCPKG_CRT_LINKAGE dynamic)
2019-08-03 06:42:17 +08:00
set(VCPKG_LIBRARY_LINKAGE dynamic) # This changed from static to dynamic
2019-07-18 02:34:31 +08:00
set(VCPKG_CMAKE_SYSTEM_NAME Linux)
```
2019-08-03 06:42:17 +08:00
### Step 2: Use `--overlay-triplets` to build dynamic libraries
2019-07-18 02:34:31 +08:00
Use the `--overlay-triplets` option to include the triplets in the `custom-triplets` directory.
```
2019-08-03 06:42:17 +08:00
~/git$ vcpkg/vcpkg install sqlite3:x64-linux-dynamic --overlay-triplets=custom-triplets
2019-07-18 02:34:31 +08:00
The following packages will be built and installed:
2019-08-03 06:42:17 +08:00
sqlite3[core]:x64-linux-dynamic
2019-07-18 02:34:31 +08:00
Starting package 1/1: sqlite3:x64-linux-dynamic
Building package sqlite3[core]:x64-linux-dynamic...
2019-08-03 06:42:17 +08:00
-- Loading triplet configuration from: /home/victor/git/custom-triplets/x64-linux-dynamic.cmake
2019-07-18 02:34:31 +08:00
-- Downloading https://sqlite.org/2019/sqlite-amalgamation-3280000.zip...
-- Extracting source /home/victor/git/vcpkg/downloads/sqlite-amalgamation-3280000.zip
-- Applying patch fix-arm-uwp.patch
-- Using source at /home/victor/git/vcpkg/buildtrees/sqlite3/src/3280000-6a3ff7ce92
-- Configuring x64-linux-dynamic-dbg
-- Configuring x64-linux-dynamic-rel
-- Building x64-linux-dynamic-dbg
-- Building x64-linux-dynamic-rel
-- Performing post-build validation
-- Performing post-build validation done
Building package sqlite3[core]:x64-linux-dynamic... done
Installing package sqlite3[core]:x64-linux-dynamic...
Installing package sqlite3[core]:x64-linux-dynamic... done
Elapsed time for package sqlite3:x64-linux-dynamic: 44.82 s
Total elapsed time: 44.82 s
The package sqlite3:x64-linux-dynamic provides CMake targets:
find_package(sqlite3 CONFIG REQUIRED)
target_link_libraries(main PRIVATE sqlite3)
```
2019-08-03 06:42:17 +08:00
Overlay triplets enables your custom triplet files when using `vcpkg install` , `vcpkg update` , `vcpkg upgrade` , and `vcpkg remove` .
2019-07-18 02:34:31 +08:00
When using the `--overlay-triplets` option, a message like the following lets you know that a custom triplet is being used:
```
-- Loading triplet configuration from: /home/custom-triplets/x64-linux-dynamic.cmake
```
## Overriding default triplets
2019-08-03 06:42:17 +08:00
As you may have noticed, the default triplets for Windows (`x86-windows` and `x64-windows` ) install dynamic libraries, while a suffix (`-static`) is needed for static libraries. This is different with Linux and Mac OS where static libraries are built by `x64-linux` and `x64-osx` .
2019-07-18 02:34:31 +08:00
2020-03-21 07:37:09 +08:00
Using `--overlay-triplets` it is possible to override the default triplets to accomplish the same behavior on Linux:
2019-07-18 02:34:31 +08:00
* `x64-linux` : Builds dynamic libraries,
* `x64-linux-static` : Builds static libraries.
2019-08-03 06:42:17 +08:00
### Step 1: Create the overlay triplets
2019-07-18 02:34:31 +08:00
2019-08-03 06:42:17 +08:00
Using the custom triplet created in the previous example, rename `custom-triplets/x64-linux-dynamic.cmake` to `custom-triplets/x64-linux.cmake` . Then, copy the default `x64-linux` triplet (which builds static libraries) in your `custom-triplets` folder and rename it to `x64-linux-static.cmake` .
2019-07-18 02:34:31 +08:00
2019-08-03 06:42:17 +08:00
```sh
~/git$ mv custom-triplets/x64-linux-dynamic.cmake custom-triplets/x64-linux.cmake
~/git$ cp vcpkg/triplets/x64-linux.cmake custom-triplets/x64-linux-static.cmake
2019-07-18 02:34:31 +08:00
```
2020-03-21 07:37:09 +08:00
### Step 2: Use `--overlay-triplets` to override default triplets
2019-07-18 02:34:31 +08:00
Use the `--overlay-triplets` option to include the triplets in the `custom-triplets` directory.
```
2019-08-03 06:42:17 +08:00
~/git$ vcpkg/vcpkg install sqlite3:x64-linux --overlay-triplets=custom-triplets
2019-07-18 02:34:31 +08:00
The following packages will be built and installed:
sqlite3[core]:x64-linux
Starting package 1/1: sqlite3:x64-linux
Building package sqlite3[core]:x64-linux...
2019-08-03 06:42:17 +08:00
-- Loading triplet configuration from: /home/victor/git/custom-triplets/x64-linux.cmake
2019-07-18 02:34:31 +08:00
-- Downloading https://sqlite.org/2019/sqlite-amalgamation-3280000.zip...
-- Extracting source /home/victor/git/vcpkg/downloads/sqlite-amalgamation-3280000.zip
-- Applying patch fix-arm-uwp.patch
-- Using source at /home/victor/git/vcpkg/buildtrees/sqlite3/src/3280000-6a3ff7ce92
-- Configuring x64-linux-dbg
-- Configuring x64-linux-rel
-- Building x64-linux-dbg
-- Building x64-linux-rel
-- Performing post-build validation
-- Performing post-build validation done
Building package sqlite3[core]:x64-linux... done
Installing package sqlite3[core]:x64-linux...
Installing package sqlite3[core]:x64-linux... done
Elapsed time for package sqlite3:x64-linux: 44.82 s
Total elapsed time: 44.82 s
The package sqlite3:x64-linux provides CMake targets:
find_package(sqlite3 CONFIG REQUIRED)
target_link_libraries(main PRIVATE sqlite3)
```
Note that the default triplet is masked by your custom triplet:
```
2019-08-03 06:42:17 +08:00
-- Loading triplet configuration from: /home/victor/git/custom-triplets/x64-linux.cmake
2019-07-18 02:34:31 +08:00
```