xref: /spdk/doc/pkgconfig.md (revision 32999ab917f67af61872f868585fd3d78ad6fb8a)
1# Linking SPDK applications with pkg-config {#pkgconfig}
2
3The SPDK build system generates pkg-config files to facilitate linking
4applications with the correct set of SPDK and DPDK libraries. Using pkg-config
5in your build system will ensure you do not need to make modifications
6when SPDK adds or modifies library dependencies.
7
8If your application is using the SPDK nvme library, you would use the following
9to get the list of required SPDK libraries:
10
11~~~
12PKG_CONFIG_PATH=/path/to/spdk/build/lib/pkgconfig pkg-config --libs spdk_nvme
13~~~
14
15To get the list of required SPDK and DPDK libraries to use the DPDK-based
16environment layer:
17
18~~~
19PKG_CONFIG_PATH=/path/to/spdk/build/lib/pkgconfig pkg-config --libs spdk_env_dpdk
20~~~
21
22When linking with static libraries, the dependent system libraries must also be
23specified. To get the list of required system libraries:
24
25~~~
26PKG_CONFIG_PATH=/path/to/spdk/build/lib/pkgconfig pkg-config --libs spdk_syslibs
27~~~
28
29Note that SPDK libraries use constructor functions liberally, so you must surround
30the library list with extra linker options to ensure these functions are not dropped
31from the resulting application binary. With shared libraries this is achieved through
32the `-Wl,--no-as-needed` parameters while with static libraries `-Wl,--whole-archive`
33is used. Here is an example Makefile snippet that shows how to use pkg-config to link
34an application that uses the SPDK nvme shared library:
35
36~~~
37PKG_CONFIG_PATH = $(SPDK_DIR)/build/lib/pkgconfig
38SPDK_LIB := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH)" pkg-config --libs spdk_nvme
39DPDK_LIB := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH)" pkg-config --libs spdk_env_dpdk
40
41app:
42	$(CC) -o app app.o -pthread -Wl,--no-as-needed $(SPDK_LIB) $(DPDK_LIB) -Wl,--as-needed
43~~~
44
45If using the SPDK nvme static library:
46
47~~~
48PKG_CONFIG_PATH = $(SPDK_DIR)/build/lib/pkgconfig
49SPDK_LIB := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH)" pkg-config --libs spdk_nvme
50DPDK_LIB := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH)" pkg-config --libs spdk_env_dpdk
51SYS_LIB := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH)" pkg-config --libs --static spdk_syslibs
52
53app:
54	$(CC) -o app app.o -pthread -Wl,--whole-archive $(SPDK_LIB) $(DPDK_LIB) -Wl,--no-whole-archive \
55		$(SYS_LIB)
56~~~
57