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