Search code examples
c++swiftxcodepcapplusplus

Issue linking and compiling C++ lib (Pcapplusplus) in SwiftUI project


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

Solution

  • 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.