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, and the DPDK libraries for the 10DPDK-based environment layer: 11 12~~~bash 13PKG_CONFIG_PATH=/path/to/spdk/build/lib/pkgconfig pkg-config --libs spdk_nvme spdk_env_dpdk 14~~~ 15 16When linking with static libraries, the dependent system libraries must also be 17specified. To get the list of required system libraries: 18 19~~~bash 20PKG_CONFIG_PATH=/path/to/spdk/build/lib/pkgconfig pkg-config --libs --static spdk_syslibs 21~~~ 22 23Note that SPDK libraries use constructor functions liberally, so you must surround 24the library list with extra linker options to ensure these functions are not dropped 25from the resulting application binary. With shared libraries this is achieved through 26the `-Wl,--no-as-needed` parameters while with static libraries `-Wl,--whole-archive` 27is used. Here is an example Makefile snippet that shows how to use pkg-config to link 28an application that uses the SPDK nvme shared library: 29 30~~~bash 31PKG_CONFIG_PATH = $(SPDK_DIR)/build/lib/pkgconfig 32SPDK_LIB := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH)" pkg-config --libs spdk_nvme spdk_env_dpdk 33 34app: 35 $(CC) -o app app.o -pthread -Wl,--no-as-needed $(SPDK_LIB) -Wl,--as-needed 36~~~ 37 38If 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 40linker args will ensure that linker uses the static versions: 41 42~~~bash 43PKG_CONFIG_PATH = $(SPDK_DIR)/build/lib/pkgconfig 44SPDK_LIB := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH)" pkg-config --libs spdk_nvme spdk_env_dpdk 45SYS_LIB := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH)" pkg-config --libs --static spdk_syslibs 46 47app: 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