libpcap icon indicating copy to clipboard operation
libpcap copied to clipboard

cmake: fix when using libpcap Cmake out of this directory

Open clementperon opened this issue 3 years ago • 3 comments
trafficstars

Hi,

I'm using FetchContent_Declare() and FetchContent_MakeAvailable() to build locally the libpcap.

But i got an issue when I link with pcap_static target the compiler can't find pcap.h.

This is because no include_directories are defined for the target.

Let's add a proper define.

clementperon avatar Mar 01 '22 09:03 clementperon

This uses generator expressions, which appear to have been introduced in CMake 2.8.11. As such, the cmake_minimum_required() value for UN*X should be increased to at least 2.8.11 from the 2.8.6 in the current CMakeLists.txt.

guyharris avatar Mar 02 '22 23:03 guyharris

@guyharris what do you think about the new revision pushed ?

clementperon avatar Mar 17 '22 07:03 clementperon

@guyharris coud you make another review on the new version? Thanks

clementperon avatar Apr 12 '22 21:04 clementperon

Hi, @clementperon, I'm not 100% why you closed this, but this is a feature I'm still interested in!

Our CMake workaround to get pcap_static working using FetchContent is doing the following and making a dummy PCAP::pcap target and adding include_directories to that, but merging this would fix it:

  FetchContent_Declare(
    libpcap
    URL https://github.com/the-tcpdump-group/libpcap/archive/refs/tags/libpcap-1.10.1.tar.gz
    URL_HASH SHA3_256=9aedcbec09b7b3b01c78cc80822c505846d73928a72ae96eb907b1f467eee649
  )
  FetchContent_MakeAvailable(libpcap)

  # pcap_static does not declare include_directories, so we need to manually add them
  add_library(PCAP::pcap INTERFACE IMPORTED)
  target_link_libraries(PCAP::pcap INTERFACE pcap_static)
  target_include_directories(PCAP::pcap INTERFACE "${libpcap_SOURCE_DIR}")
  target_compile_definitions(
    # pcap requires BSD types to be defined, e.g. u_int/u_short/u_char
    # the following syntax works for uClibc, glibc, musl libc
    PCAP::pcap INTERFACE
      "_BSD_SOURCE" # deprecated in glibc >2.20
      "_DEFAULT_SOURCE" # only added in glibc >2.19, musl >=1.1.5
  )

aloisklink avatar Nov 12 '22 20:11 aloisklink

@aloisklink I have push -f in my master branch so this automatically close this MR.

You can add you're test to this new MR https://github.com/the-tcpdump-group/libpcap/pull/1135

clementperon avatar Nov 12 '22 20:11 clementperon