1

I'm trying to utilize the C++ lib Pcapplusplus in my SwiftUI application through the use of objective C bridge classes. I've compiled a standalone C++ executable that makes very basic use of the pcpp library, but I'm lost on how to link and compile it in xcode. Here's the two makefiles I use to run the standalone executable in the terminal. Apologies if this is super simple, I'm just not sure how these makefiles would translate to xcode configuration options. Replies are appreciated!

all:
    g++ $(PCAPPP_BUILD_FLAGS) $(PCAPPP_INCLUDES) -c -o main.o main.cpp
    g++ $(PCAPPP_LIBS_DIR) -o Tutorial-HelloWorld main.o $(PCAPPP_LIBS)

# Clean Target
clean:
    rm main.o
    rm Tutorial-HelloWorld
    # All Target
all:
    g++ $(PCAPPP_BUILD_FLAGS) $(PCAPPP_INCLUDES) -c -o main.o main.cpp
    g++ $(PCAPPP_LIBS_DIR) -o Tutorial-HelloWorld main.o $(PCAPPP_LIBS)

# Clean Target
clean:
    rm main.o
    rm Tutorial-HelloWorld

### COMMON ###

# includes
PCAPPP_INCLUDES := -I/opt/homebrew/Cellar/pcapplusplus/21.05/include/pcapplusplus


# libs
PCAPPP_LIBS := /opt/homebrew/opt/pcapplusplus/lib/libPcap++.a /opt/homebrew/opt/pcapplusplus/lib/libPacket++.a /opt/homebrew/opt/pcapplusplus/lib/libCommon++.a

# post build
PCAPPP_POST_BUILD :=

# build flags
PCAPPP_BUILD_FLAGS := -fPIC

ifdef PCAPPP_ENABLE_CPP_FEATURE_DETECTION
    PCAPPP_BUILD_FLAGS += -DPCAPPP_CPP_FEATURE_DETECTION -std=c++11
endif

ifndef CXXFLAGS
CXXFLAGS := -O2 -g -Wall
endif

PCAPPP_BUILD_FLAGS += $(CXXFLAGS)
### MAC OS X ###

# includes
PCAPPP_INCLUDES += -I$(MACOS_SDK_HOME)/usr/include/netinet

# libs
PCAPPP_LIBS += -lpcap -lpthread -framework SystemConfiguration -framework CoreFoundation
seladb
  • 852
  • 1
  • 13
  • 29
  • Hi, I'm the author of PcapPlusPlus, but unfortunately I'm not familiar with SwiftUI. However, this seems like a general question about binding C++ and Swift/Objective C code. Is there something special about PcapPlusPlus that makes it harder to bind as compared to other C++ libraries? – seladb Sep 28 '21 at 02:22
  • @seladb It can't be anything to do with pcpp itself, I'm just completely new to xcode & macOS development in general, hence I was seeking help translating the provided makefiles in pcapplusplus's example program, to settings I can add in the project settings in my xcode project. – publicstaticmain Sep 28 '21 at 04:42
  • Got it, thanks @publicstaticmain! – seladb Sep 28 '21 at 16:42
  • @seladb No problem!. Do you think you could provide a bit of assistance if you find the time? When I try to build in xcode, I get multiple errors reading "pcpp::PcaLiveDeviceList ...etc undefined symbol for architecture arm64." I've added the /Cellar/pcapplusplus/include to the header search paths properly, & I've included the /lib/ folder in the proj "ilink with libs" options, it seems to have issues with the linker flags I assume? I'm just not sure how to take the contents of the provided make files and add the linker flags properly to xcode. – publicstaticmain Sep 28 '21 at 18:38
  • 1
    If you're building for arm64 maybe you can try to build PcapPlusPlus from source using the `--arm64` flag. You can take a look here: https://pcapplusplus.github.io/docs/install/build-source/macos This flag was created for macOS with Apple Silicon, I'm not sure if you try to build it for macOS or iOS – seladb Sep 29 '21 at 08:42
  • @seladb thank you for the advice, got it compiling and working happily alongside swiftUI – publicstaticmain Oct 04 '21 at 17:08
  • this is great! so it was just about using the `--arm64` flag, or did you do more things to make it work? – seladb Oct 06 '21 at 07:35
  • @seladb It was a mix of adding the flags that appear when I run the make file of the standalone project, & also adding the "corefoundation" and "system configuration " libraries in the "libraries to include" section of my project settings. I kinda assumed that if the compilier knew to include it for the swiftUI src code of the project, it would know to do so when compiling C++ & obj C – publicstaticmain Oct 08 '21 at 17:12
  • I'm not sure I understand... can you elaborate on the steps that made PcapPlusPlus compile for SwiftUI? I think it might be useful for more people – seladb Oct 11 '21 at 05:10
  • @seladb The first step after making the project is to go to your build settings, and under "header search paths" add the pcapplusplus include dir. Then, under "other linker flags" I added -L/usr/local/lib -lpcap, -lpthread and $(inherited). Then under the build phases tab of the proj settings, in "Link Binary With libraries" I added the SystemConfig framework, corefoundation, then libCommon++.a, libPack++.a & libPcap++.a. I supposed much of this might be obvious, but I'm inexperience with xcode and real world C++ libs – publicstaticmain Oct 11 '21 at 15:35

1 Answers1

2

Here are the steps to build PcapPlusPlus in a SwiftUI project:

  1. Run PcapPlusPlus configuration script for arm64:
    ./configure-mac_os_x.sh --arm64
    
  2. Build PcapPlusPlus:
    make libs
    
  3. Go to the build settings, and under "header search paths" add the PcapPlusPlus include dir
  4. Under "other linker flags" add:
    -L/usr/local/lib -lpcap, -lpthread $(inherited)
    
  5. Under the build phases tab of the proj settings, in "Link Binary With libraries" add the SystemConfig framework, corefoundation, then libCommon++.a, libPacket++.a and libPcap++.a

@publicstaticmain please let me know if anything is missing or inaccurate.

seladb
  • 852
  • 1
  • 13
  • 29
  • That's correct, the only issue i'm having is the "PCAP_SLEEP()" macro isn't being found, & "platformspecificutils.h" doesn't seem to to be found whatsoever, but every other header file I include is found properly. – publicstaticmain Oct 16 '21 at 18:25
  • Which version of PcapPlusPlus are you using? In the latest release (v21.05) the `platformspecificutils.h` file was removed and so as the `PCAP_SLEEP()` macro, maybe you're using an older version? – seladb Oct 19 '21 at 07:15
  • Then there's no issue! I'm on the latest verison of pcap, I was just using tutorial for reference on how to capture packets asynchronously https://pcapplusplus.github.io/docs/tutorials/capture-packets – publicstaticmain Oct 19 '21 at 19:19
  • Good catch @publicstaticmain , thank for letting me know! I just fixed the tutorial and replaced `PCAP_SLEEP()` with the updated method `pcpp::multiPlatformSleep()` – seladb Oct 22 '21 at 04:37
  • I couldn't find the "build settings" option/folder – Fahad Naeem Dec 20 '22 at 05:38
  • @Fahad Naeem Maybe this can help? https://stackoverflow.com/questions/3428216/adding-system-header-search-path-to-xcode – seladb Dec 21 '22 at 08:59