gpt4 book ai didi

libtins 库的 C++ undefined reference

转载 作者:行者123 更新时间:2023-11-30 01:44:09 26 4
gpt4 key购买 nike

我安装了 <a href="http://libtins.github.io/download/" rel="noreferrer noopener nofollow">libtins</a> C++ 包,按照描述构建它并添加 #include <tins/tins.h>到示例文件的标题。我跑了 ldconfig但是构建总是会抛出一个 undefined reference 错误:

CMakeFiles/lts.dir/main.cpp.o: In function `main':
/home/patrick/ClionProjects/lts/main.cpp:6: undefined reference to `Tins::EthernetII::EthernetII(Tins::HWAddress<6ul, unsigned char> const&, Tins::HWAddress<6ul, unsigned char> const&)'
/home/patrick/ClionProjects/lts/main.cpp:7: undefined reference to `Tins::IPv4Address::IPv4Address(char const*)'
/home/patrick/ClionProjects/lts/main.cpp:7: undefined reference to `Tins::IPv4Address::IPv4Address(char const*)'
/home/patrick/ClionProjects/lts/main.cpp:7: undefined reference to `Tins::IP::IP(Tins::IPv4Address, Tins::IPv4Address)'
/home/patrick/ClionProjects/lts/main.cpp:8: undefined reference to `Tins::TCP::TCP(unsigned short, unsigned short)'
/home/patrick/ClionProjects/lts/main.cpp:10: undefined reference to `Tins::PDU::inner_pdu(Tins::PDU*)'
/home/patrick/ClionProjects/lts/main.cpp:12: undefined reference to `Tins::PDU::inner_pdu(Tins::PDU*)'
CMakeFiles/lts.dir/main.cpp.o: In function `Tins::EthernetII::~EthernetII()':
/usr/local/include/tins/ethernetII.h:46: undefined reference to `vtable for Tins::EthernetII'
/usr/local/include/tins/ethernetII.h:46: undefined reference to `Tins::PDU::~PDU()'
collect2: error: ld returned 1 exit status
make[3]: *** [lts] Error 1

我正在使用 CLion作为 IDE 和以下 cmake 文件:

cmake_minimum_required(VERSION 3.5)
project(lts)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -ltins")

set(SOURCE_FILES main.cpp)

add_executable(lts ${SOURCE_FILES})

知道可能是什么问题吗?

最佳答案

您的问题中有几件事需要解决。首先,让 CMake 为您找到 libtins 库的位置,然后使用 target_link_libraries() 将其添加到您的 lts 可执行文件中。在您问题的 CMake 示例中,您在编译器标志变量 (CMAKE_CXX_FLAGS) 中指定了库选项。另一件事是让 CMake 处理为 C++11 设置适当的编译器标志,而不是手动将它们添加到 CMAKE_CXX_FLAGS(这只会处理编译器部分而不处理链接器)。以下内容应该以稳健、平台独立的方式执行您想要的操作:

cmake_minimum_required(VERSION 3.5)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(lts)

find_library(TINS_LIBRARY tins)

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

target_link_libraries(lts "${TINS_LIBRARY}")

我建议您阅读 find_library() 的 CMake 文档和 target_link_libraries()了解这些命令的作用。对于 C++11 的东西,我推荐 this blog article比 CMake 文档给你的解释更连贯(披露:我写了这篇文章)。

关于libtins 库的 C++ undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36670466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com