cmake: undefined reference to any pcap functions

南楼画角 提交于 2019-12-13 14:36:04

问题


I want to use pcap in my Clion project on linux. I installed libpcap-dev:

sudo apt-get install libpcap-dev

But then I try to compile any file, containing pcap functions like:

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

int main(int argc, char *argv[])
{
    char *dev, errbuf[PCAP_ERRBUF_SIZE];

    dev = pcap_lookupdev(errbuf);
    if (dev == NULL) {
        fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
        return(2);
    }
    printf("Device: %s\n", dev);
    return(0);
}

I have cmake errors:

CMakeFiles/main.cpp.o: In function `main':
/main.cpp:8: undefined reference to `pcap_lookupdev'

I have not used cmake before. Cmake file:

cmake_minimum_required(VERSION 3.7)
project(mypr)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)
add_executable(mypr ${SOURCE_FILES})

回答1:


You should include and use a FindPCAP.cmake file to your CMakeLists.txt. Here is one: https://github.com/bro/cmake/blob/master/FindPCAP.cmake

Put FindPCAP.cmake in your project's source directory and try changing your CMakeLists.txt to:

cmake_minimum_required(VERSION 3.7)
project(mypr)

set(CMAKE_CXX_STANDARD 11)

include(FindPCAP.cmake)

set(SOURCE_FILES main.cpp)
add_executable(mypr ${SOURCE_FILES})
target_link_libraries(mypr ${PCAP_LIBRARY})


来源:https://stackoverflow.com/questions/43322488/cmake-undefined-reference-to-any-pcap-functions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!