cmake:未定义对任何pcap函数的引用 [英] cmake: undefined reference to any pcap functions

查看:337
本文介绍了cmake:未定义对任何pcap函数的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Linux上的Clion项目中使用pcap。
我安装了libpcap-dev:

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

sudo apt-get install libpcap-dev

但是随后,我尝试编译包含pcap函数的任何文件:

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);
}

我有cmake错误:

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

我以前没有用过cmake。 Cmake文件:

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})


推荐答案

您应将FindPCAP.cmake文件包括并使用到CMakeLists.txt中。这是一个: https://github.com/bro/cmake/blob/master /FindPCAP.cmake

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

将FindPCAP.cmake放在项目的源目录中,然后尝试将CMakeLists.txt更改为:

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})

这篇关于cmake:未定义对任何pcap函数的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆