3

I've installed the c++ library pcapplusplus on my linux machine and the .a files have been put in /usr/local/lib. I am now trying to link my project with it in cmake using target_link_libraries(${PROJECT_NAME} libCommon++.a libPacket++.a libPcap++.a). However, it can't find Packet.h which is part of libPacket++.a. What am I doing wrong here? Do I have to tell cmake where to look?

cmake_minimum_required(VERSION 2.8.9)
project(networksniffer)
# The version number.
set (networksniffer_VERSION_MAJOR 1)
set (networksniffer_VERSION_MINOR 0)

set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(PROJECT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/src)

# The following folder will be included
include_directories("/usr/local/include/pcapplusplus")

#add_executable(networksniffer ${SOURCES})
add_executable(networksniffer ${PROJECT_SOURCE_DIR}/networksniffer.cpp)

target_link_libraries(${PROJECT_NAME} libCommon++.a libPacket++.a libPcap++.a)
seladb
  • 852
  • 1
  • 13
  • 29
netleap tom
  • 143
  • 4
  • 10

3 Answers3

6

Packet.h is not part of libPacket++.a.

libPacket++.a is a library, Packet.h is a header file.

CMake can't know that both relate to each other unless you tell it so.

I suggest using find_package to properly locate both (https://cmake.org/cmake/help/v3.0/command/find_package.html).

Use target_include_directories then to include the path to the header file.

wolff
  • 426
  • 4
  • 11
  • thanks I've updated my cmakelists.txt file in my original post. Adding `include_directories("/usr/local/include/pcapplusplus")` resolved the issue of find `Packet.h`. However, now the project builds and I am getting `undefined reference to `pcpp::compute_checksum()` which I read is a linking error. – netleap tom Jan 09 '18 at 11:14
  • 1
    And of course these days you would prefer to use [`target_include_directories`](https://cmake.org/cmake/help/v3.10/command/target_include_directories.html) over the plain old `include_directories`. – ComicSansMS Jan 09 '18 at 11:43
  • @netleaptom this is a different issue. Please post a new question for this. – wolff Jan 09 '18 at 11:55
1

You need to add header search directory path where compiler can find header file in your case Packet.h

You can try command locate Packet.h to find out path on your system. Then you can add that path with include_directories() function. i.e. if you found Packet.h in /usr/local/include/ then you should update like below.

include_directories("${PROJECT_SOURCE_DIR}"/include "/usr/local/include/")
Manthan Tilva
  • 3,135
  • 2
  • 17
  • 41
1

The error is because it is not able to find the header file. I see that you havent mentioned any include folders. The file Packet.h must be in /usr/local/include. You can either include this. OR you can add something like this

find_package(pcapplusplus REQUIRED)
include_directories(${PCAPPLUSPLUS_INCLUDE_DIRS})

Take care of varible names yourself.

Naveen KH
  • 153
  • 2
  • 11
  • I tried it, however I believe .cmake is missing. The following error was shown: `Could not find a package configuration file provided by "pcapplusplus" with any of the following names: pcapplusplusConfig.cmake pcapplusplus-config.cmake` – nav Jun 10 '20 at 00:27