Search code examples
cmakedpdk

compiling dpdk application using cmake with link static libraries


I am referring to the DPDK example makefile to compile the dpdk application through CMake. The correct compile command should look like

/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

This is a part of my 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)

The DPDK_DRIVER generated again after -Bdynamic to make the application panic. If I remove them and recompile, it will work normally and get right result.

/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

I hope your answers


Solution

  • There is known bug with DPDK 20.11.1 LTS for the result of pkg-config. Since the DPDK version is not mentioned I will have to assume you are using Higher version (> 20.11).

    I am able to compile and use DPDK library in 3rd party projects with CMAKE successfully by making the changes as

    • In the parent folder
    +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()
    
    • Source folder
    +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)
    

    [EDIT-1] based on the comment suggestion

    • In the parent folder
    +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()
    
    • Source folder
    +pkg_check_modules(DPDK "libdpdk")
    +if (DPDK_FOUND)
    +  add_definitions(${DPDK_STATIC_CFLAGS})
    +  set(MYDPDK_LIBRARIES ${DPDK_STATIC_LDFLAGS})
    +  include_directories(${DPDK_INCLUDE_DIR})
    +  link_libraries(${MYDPDK_LIBRARIES})
    +  add_definitions(-DHAVE_DPDK)
    +endif(DPDK_FOUND)
    

    Note: Once pkg_check_modules finds libdpdk; the static libraries and cflags can be accessed via LIBDPDK_STATIC_LDFLAGS and LIBDPDK_STATIC_CFLAGS.