使用带有 link 个静态库的 cmake 编译 dpdk 应用程序

compiling dpdk application using cmake with link static libraries

我指的是通过CMake编译dpdk应用程序的DPDK示例makefile。正确的编译命令应该是这样的

/usr/bin/x86_64-redhat-linux-gcc-8  -I/usr/local/include -include rte_config.h -march=native -mno-avx512f -g CMakeFiles/dpdk_test.dir/main.c.o -o dpdk_test  -Wl,-Bstatic -lrte_pci -lrte_ethdev -lrte_meter -lrte_net -lrte_mbuf -lrte_mempool -lrte_ring -lrte_eal -lrte_kvargs -Wl,--whole-archive -lrte_bus_pci -lrte_pmd_vmxnet3 -Wl,--no-whole-archive -lrte_pci -lrte_ethdev -lrte_meter -lrte_net -lrte_mbuf -lrte_mempool -lrte_ring -lrte_eal -lrte_kvargs -Wl,-Bdynamic -lpthread -ldl -lnuma

这是我的一部分 CMakeLists.txt:

#DPDK_INFRA some DPDK base libs
#DPDK_DRIVER some DPDK driver libs
SET(DPDK_LIBS -Wl,-Bstatic ${DPDK_INFRA} -Wl,--whole-archive ${DPDK_DRIVER} -Wl,--no-whole-archive ${DPDK_INFRA} -Wl,-Bdynamic)
target_link_libraries(${PROJECT_NAME}  ${DPDK_LIBS} pthread numa dl)

-Bdynamic 后再次生成DPDK_DRIVER 使应用程序崩溃。如果我删除它们并重新编译,它将正常工作并得到正确的结果。

/usr/bin/x86_64-redhat-linux-gcc-8  -I/usr/local/include -include rte_config.h -march=native -mno-avx512f -g CMakeFiles/dpdk_test.dir/main.c.o -o dpdk_test  -Wl,-Bstatic -lrte_pci -lrte_ethdev -lrte_meter -lrte_net -lrte_mbuf -lrte_mempool -lrte_ring -lrte_eal -lrte_kvargs -Wl,--whole-archive -lrte_bus_pci -lrte_pmd_vmxnet3 -Wl,--no-whole-archive -lrte_pci -lrte_ethdev -lrte_meter -lrte_net -lrte_mbuf -lrte_mempool -lrte_ring -lrte_eal -lrte_kvargs -Wl,-Bdynamic -lpthread -lnuma -ldl **-lrte_bus_pci -lrte_pmd_vmxnet3** -lpthread -lnuma -ldl

希望得到大家的回答

pkg-config 的结果在 DPDK 20.11.1 LTS 中存在已知错误。由于未提及 DPDK 版本,我将不得不假设您使用的是更高版本 (> 20.11)。

我能够使用 CMAKE 成功地在第 3 方项目中编译和使用 DPDK 库,方法是

  • 在父文件夹中
+find_package(PkgConfig REQUIRED)
+
+if (PKG_CONFIG_FOUND)
+       pkg_check_modules(DPDK "libdpdk")
+       if (DPDK_FOUND)
+               message(STATUS "found dpdk via pkg-config")
+       endif()
+endif()
  • 源文件夹
+pkg_check_modules(DPDK "libdpdk")
+if (DPDK_FOUND)
+  add_definitions(${DPDK_CFLAGS})
+  set(MYDPDK_LIBRARIES -Wl,--whole-archive ${DPDK_LIBRARIES} -lpthread -lnuma -ldl -Wl,--no-whole-archive)
+  include_directories(${DPDK_INCLUDE_DIR})
+  link_libraries(${MYDPDK_LIBRARIES})
+  add_definitions(-DHAVE_DPDK)
+endif(DPDK_FOUND)