You don't actually care about the library version, you care about whether or not the symbol pcap_create
is present. Therefore, this is a job for CheckSymbolExists
:
# This assumes PCAP_INCLUDE_DIRS is appropriately set somehow, maybe via:
#
# find_path(PCAP_INCLUDE_DIRS "pcap/pcap.h")
#
# or something else in your CMakeLists.txt
include(CheckSymbolExists)
set(CMAKE_REQUIRED_INCLUDES "${PCAP_INCLUDE_DIRS}")
check_symbol_exists(pcap_create "pcap/pcap.h" HAVE_PCAP_CREATE)
add_executable(main main.cpp)
target_compile_definitions(main PRIVATE "HAVE_PCAP_CREATE=$<BOOL:${HAVE_PCAP_CREATE}>")
This will define HAVE_PCAP_CREATE
in main.cpp
to 1
or 0
if the pcap_create
function exists or not. Then in your code you would write:
#if HAVE_PCAP_CREATE
pcap_create(...);
#else
pcap_open_live(...);
#endif