Search code examples
cpcapclion

PCAP does't work in CLion


For my assignment I need to run the C/C++ code with the libpcap. I'm using CLion 1.2. When I try to run the test code, I get the messages

undefined reference to 'pcap_open_offline'

undefined reference to 'pcap_next'

Here is the code

#include <stdio.h>
#include <pcap/pcap.h>

const u_char *packet;


int main() {


    char errbuf[PCAP_ERRBUF_SIZE];
    pcap_t *handle = pcap_open_offline("/home/alex/Downloads/priklad.pcap", errbuf);

    struct pcap_pkthdr packet_header;
    packet = pcap_next(handle, &packet_header);


    return 0;
}

I tried linking the library like discussed here, but it didn't help. I loaded the library with the command

sudo apt-get install libpcap-dev

Current CMakeList.txt:

cmake_minimum_required(VERSION 3.3)
project(PKS1)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

SET(GCC_COVERAGE_LINK_FLAGS    "-lpcap")

SET(CMAKE_MODULE_PATH "/home/alex/projects/ClionProjects/PKS1/")


include(FindPCAP)
find_package(PCAP)

set(SOURCE_FILES main.c)
add_executable(PKS1 ${SOURCE_FILES})
target_link_libraries(PKS1 ${usr/include})

My question is, what am I doing wrong? Where is the library? How do I find it and correctly link it without compiling every time with gcc?


Solution

  • You're never linking to libpca.

    Change your line:

    target_link_libraries(PKS1 ${usr/include})
    

    to

    target_link_libraries(PKS1 ${PCAP_LIBRARY})
    

    You might also want to change to:

    find_package(PCAP REQUIRED)
    

    in order to error out during the run of cmake instead of during the compile step if libpcap can't be found.