cmake linking against static libraries - do you have to tell cmake where to look?

折月煮酒 提交于 2020-06-26 09:44:48

问题


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)

回答1:


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.




回答2:


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/")



回答3:


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.



来源:https://stackoverflow.com/questions/48165990/cmake-linking-against-static-libraries-do-you-have-to-tell-cmake-where-to-look

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