c++ libtin构建错误

c++ libtins build error

本文关键字:错误 构建 libtin c++      更新时间:2023-10-16

我正在尝试http://libtins.github.io上的litbins构建包示例。

我已经按照安装说明并且能够很好地安装#include <tins/tins.h>。现在的问题似乎是在实际引用它的函数。当尝试构建这个类时:

#include <tins/tins.h>
#include <cassert>
#include <iostream>
#include <string>
using namespace Tins;
int main() {
    NetworkInterface iface = NetworkInterface::default_interface();
    NetworkInterface::Info info = iface.addresses();
    EthernetII eth("77:22:33:11:ad:ad", info.hw_addr);
    eth /= IP("localhost", info.ip_addr);
    eth /= TCP(13, 15);
    eth /= RawPDU("I'm a payload!");
    PacketSender sender;
    sender.send(eth, iface);
}

我得到这个错误:

cd '/home/oisin/NetBeansProjects/TCPPacket'
/usr/bin/gmake -f Makefile CONF=Debug
"/usr/bin/gmake" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
gmake[1]: Entering directory '/home/oisin/NetBeansProjects/TCPPacket'
"/usr/bin/gmake"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux/tcppacket
gmake[2]: Entering directory '/home/oisin/NetBeansProjects/TCPPacket'
mkdir -p build/Debug/GNU-Linux
rm -f "build/Debug/GNU-Linux/main.o.d"
g++    -c -g -std=c++11 -MMD -MP -MF "build/Debug/GNU-Linux/main.o.d" -o build/Debug/GNU-Linux/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux
g++     -o dist/Debug/GNU-Linux/tcppacket build/Debug/GNU-Linux/main.o 
build/Debug/GNU-Linux/main.o: In function `main':
/home/oisin/NetBeansProjects/TCPPacket/main.cpp:26: undefined reference to `Tins::NetworkInterface::default_interface()'
/home/oisin/NetBeansProjects/TCPPacket/main.cpp:28: undefined reference to `Tins::NetworkInterface::addresses() const'
/home/oisin/NetBeansProjects/TCPPacket/main.cpp:30: undefined reference to `Tins::EthernetII::EthernetII(Tins::HWAddress<6ul, unsigned char> const&, Tins::HWAddress<6ul, unsigned char> const&)'
/home/oisin/NetBeansProjects/TCPPacket/main.cpp:32: undefined reference to `Tins::IPv4Address::IPv4Address(char const*)'
/home/oisin/NetBeansProjects/TCPPacket/main.cpp:32: undefined reference to `Tins::IP::IP(Tins::IPv4Address, Tins::IPv4Address)'
/home/oisin/NetBeansProjects/TCPPacket/main.cpp:34: undefined reference to `Tins::TCP::TCP(unsigned short, unsigned short)'
/home/oisin/NetBeansProjects/TCPPacket/main.cpp:36: undefined reference to `Tins::RawPDU::RawPDU(std::string const&)'
/home/oisin/NetBeansProjects/TCPPacket/main.cpp:38: undefined reference to `Tins::PacketSender::DEFAULT_TIMEOUT'
/home/oisin/NetBeansProjects/TCPPacket/main.cpp:38: undefined reference to `Tins::NetworkInterface::NetworkInterface()'
/home/oisin/NetBeansProjects/TCPPacket/main.cpp:38: undefined reference to `Tins::PacketSender::PacketSender(Tins::NetworkInterface const&, unsigned int, unsigned int)'
/home/oisin/NetBeansProjects/TCPPacket/main.cpp:40: undefined reference to `Tins::PacketSender::send(Tins::PDU&, Tins::NetworkInterface const&)'
/home/oisin/NetBeansProjects/TCPPacket/main.cpp:38: undefined reference to `Tins::PacketSender::~PacketSender()'
/home/oisin/NetBeansProjects/TCPPacket/main.cpp:38: undefined reference to `Tins::PacketSender::~PacketSender()'
build/Debug/GNU-Linux/main.o: In function `Tins::EthernetII& Tins::operator/=<Tins::EthernetII>(Tins::EthernetII&, Tins::PDU const&)':
/usr/local/include/tins/pdu.h:537: undefined reference to `Tins::PDU::inner_pdu(Tins::PDU*)'
build/Debug/GNU-Linux/main.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()'
build/Debug/GNU-Linux/main.o: In function `Tins::IP::~IP()':
/usr/local/include/tins/ip.h:64: undefined reference to `vtable for Tins::IP'
/usr/local/include/tins/ip.h:64: undefined reference to `Tins::PDU::~PDU()'
build/Debug/GNU-Linux/main.o: In function `Tins::TCP::~TCP()':
/usr/local/include/tins/tcp.h:79: undefined reference to `vtable for Tins::TCP'
/usr/local/include/tins/tcp.h:79: undefined reference to `Tins::PDU::~PDU()'
build/Debug/GNU-Linux/main.o: In function `Tins::RawPDU::~RawPDU()':
/usr/local/include/tins/rawpdu.h:64: undefined reference to `vtable for Tins::RawPDU'
/usr/local/include/tins/rawpdu.h:64: undefined reference to `Tins::PDU::~PDU()'
collect2: error: ld returned 1 exit status
nbproject/Makefile-Debug.mk:62: recipe for target 'dist/Debug/GNU-Linux/tcppacket' failed
gmake[2]: *** [dist/Debug/GNU-Linux/tcppacket] Error 1
gmake[2]: Leaving directory '/home/oisin/NetBeansProjects/TCPPacket'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
gmake[1]: *** [.build-conf] Error 2
gmake[1]: Leaving directory '/home/oisin/NetBeansProjects/TCPPacket'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
gmake: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)

Netbeans正在使用c++11,我已经启用了-DLIBTINS_ENABLE_CXX11=1的c++11支持,我已经运行了ldconfig随着重启虚拟机,相同的错误。

我已经尝试将类与g++ main.cpp -o main -ltins连接以获得这些错误-

In file included from /usr/local/include/tins/tins.h:60:0,
                 from main.cpp:14:
/usr/local/include/tins/crypto.h:297:13: error: ‘function’ in namespace ‘std’ does not name a type
     typedef std::function<void(const std::string&,
             ^
/usr/local/include/tins/crypto.h:308:13: error: ‘function’ in namespace ‘std’ does not name a type
     typedef std::function<void(const std::string&,
             ^
/usr/local/include/tins/crypto.h:401:44: error: ‘handshake_captured_callback_type’ does not name a type
     void handshake_captured_callback(const handshake_captured_callback_type& callback);
                                            ^
/usr/local/include/tins/crypto.h:412:34: error: ‘ap_found_callback_type’ does not name a type
     void ap_found_callback(const ap_found_callback_type& callback);
                                  ^
/usr/local/include/tins/crypto.h:445:9: error: ‘handshake_captured_callback_type’ does not name a type
         handshake_captured_callback_type handshake_captured_callback_;
         ^
/usr/local/include/tins/crypto.h:446:9: error: ‘ap_found_callback_type’ does not name a type
         ap_found_callback_type ap_found_callback_;
         ^

帮助感激。

编辑

从下面dvnguyen的回答,链接我的main.cppg++ app.cpp -o app -ltins -std=c++11解决了上述问题,但现在我留下了以下错误-

cd '/home/oisin/NetBeansProjects/TCPPacket'
/usr/bin/gmake -f Makefile CONF=Debug
"/usr/bin/gmake" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
gmake[1]: Entering directory '/home/oisin/NetBeansProjects/TCPPacket'
"/usr/bin/gmake"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux/tcppacket
gmake[2]: Entering directory '/home/oisin/NetBeansProjects/TCPPacket'
mkdir -p build/Debug/GNU-Linux
rm -f "build/Debug/GNU-Linux/main.o.d"
g++    -c -g -std=c++11 -MMD -MP -MF "build/Debug/GNU-Linux/main.o.d" -o build/Debug/GNU-Linux/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux
g++     -o dist/Debug/GNU-Linux/tcppacket build/Debug/GNU-Linux/main.o -lmain
/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../x86_64-suse-linux/bin/ld: cannot find -lmain
collect2: error: ld returned 1 exit status
nbproject/Makefile-Debug.mk:62: recipe for target 'dist/Debug/GNU-Linux/tcppacket' failed
gmake[2]: *** [dist/Debug/GNU-Linux/tcppacket] Error 1
gmake[2]: Leaving directory '/home/oisin/NetBeansProjects/TCPPacket'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
gmake[1]: *** [.build-conf] Error 2
gmake[1]: Leaving directory '/home/oisin/NetBeansProjects/TCPPacket'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
gmake: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 2s)

我真的不知道我错过了什么。

从错误日志中的undefined reference消息,我可以告诉你没有添加/链接库到你的项目。我不知道你的Netbean项目设置,但如果你使用gcc,你可以在这里看到一个例子:

g++ app.cpp -o app -ltins -std=c++11