我想在linux上的Clion项目中使用pcap。 我安装了libpcap-dev:
sudo apt-get install libpcap-dev
然后我尝试编译包含pcap函数的任何文件,如:
#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文件:
cmake_minimum_required(VERSION 3.7)
project(mypr)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(mypr ${SOURCE_FILES})
答案 0 :(得分:5)
您应该在您的CMakeLists.txt中包含并使用FindPCAP.cmake文件。这是一个:https://github.com/bro/cmake/blob/master/FindPCAP.cmake
将FindPCAP.cmake放入项目的源目录并尝试将CMakeLists.txt更改为:
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})
答案 1 :(得分:0)
我认为您也可以使用CMAKE_MODULE_PATH
(假设FindPCAP.cmake
在项目的源目录中):
cmake_minimum_required(VERSION 3.7)
project(mypr)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/")
find_package(PCAP REQUIRED)
set(SOURCE_FILES main.cpp)
add_executable(mypr ${SOURCE_FILES})
target_link_libraries(mypr ${PCAP_LIBRARY})