1

我是 C++ 的新手,但我有很多编程方面的一般经验,所以我熟悉大多数低级概念。

无论如何,我想我会尝试编写一个 IRC 客户端,因为 IRC 似乎有一个相对简单的 TCP 协议。(很简单,可以通过 Telnet 访问和使用服务器,不实用,但可行)

所以我搜索了“C++ 套接字库”并遇到了dlib,这是一个包含一些套接字支持的通用库。

我查看了文档,找到了一个可以回应它收到的任何内容的示例,并尝试编译它。但是,我收到了大量关于函数“未定义引用”的错误。

我真的不知道现在该怎么办。谁能给我一些建议?

我在 Code::Blocks v10.05 和 GCC 4.4.1 中构建它(我会使用 4.6.1,但我还没有弄清楚如何让 Code::Blocks 以这种方式编译)

这是确切的示例:

// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*

    This is an example illustrating the use of the sockets and
    server components from the dlib C++ Library.

    This is a simple echo server.  It listens on port 1234 for incoming
    connections and just echos back any data it receives.

*/




#include "dlib/sockets.h"
#include "dlib/server.h"
#include "dlib/ref.h" // for ref()
#include <iostream>

using namespace dlib;
using namespace std;



class serv : public server::kernel_1a_c
{

    void on_connect  (
        connection& con
    )
    {
        char ch;
        while (con.read(&ch,1) > 0)
        {
            // we are just reading one char at a time and writing it back
            // to the connection.  If there is some problem writing the char
            // then we quit the loop.
            if (con.write(&ch,1) != 1)
                break;
        }
    }

};


void thread(serv& our_server)
{
    try
    {
        // Start the server.  start() blocks until the server is shutdown
        // by a call to clear()
        our_server.start();
    }
    catch (socket_error& e)
    {
        cout << "Socket error while starting server: " << e.what() << endl;
    }
    catch (exception& e)
    {
        cout << "Error while starting server: " << e.what() << endl;
    }
}


int main()
{
    try
    {
        serv our_server;

        // set up the server object we have made
        our_server.set_listening_port(1234);
        our_server.set_max_connections(1000);

        // create a thread that will start the server.   The ref() here allows us to pass
        // our_server into the threaded function by reference.
        thread_function t(thread, dlib::ref(our_server));

        cout << "Press enter to end this program" << endl;
        cin.get();
        // this will cause the server to shut down
        our_server.clear();
    }
    catch (exception& e)
    {
        cout << e.what() << endl;
    }
    catch (...)
    {
        cout << "Some error occurred" << endl;
    }
}

然后是输出:

-------------- Build: Release in test ---------------

Compiling: main.cpp
Linking console executable: bin\test.exe
obj\main.o:main.cpp:(.text+0xe): undefined reference to `dlib::logger::~logger()'
obj\main.o:main.cpp:(.text+0x1d3): undefined reference to `dlib::logger::logger(char const*)'
obj\main.o:main.cpp:(.text+0x3cf): undefined reference to `dlib::threaded_object::threaded_object()'
obj\main.o:main.cpp:(.text+0x44a): undefined reference to `dlib::threaded_object::start()'
obj\main.o:main.cpp:(.text+0x4ef): undefined reference to `dlib::threaded_object::wait() const'
obj\main.o:main.cpp:(.text+0x523): undefined reference to `dlib::threaded_object::~threaded_object()'
obj\main.o:main.cpp:(.text+0x5e8): undefined reference to `dlib::threaded_object::~threaded_object()'
obj\main.o:main.cpp:(.text+0x701): undefined reference to `dlib::threaded_object::~threaded_object()'
obj\main.o:main.cpp:(.text$_ZN4serv10on_connectERN4dlib10connectionE[serv::on_connect(dlib::connection&)]+0x20): undefined reference to `dlib::connection::read(char*, long)'
obj\main.o:main.cpp:(.text$_ZN4serv10on_connectERN4dlib10connectionE[serv::on_connect(dlib::connection&)]+0x38): undefined reference to `dlib::connection::write(char const*, long)'
obj\main.o:main.cpp:(.text$_ZN4dlib15thread_functionD0Ev[dlib::thread_function::~thread_function()]+0x43): undefined reference to `dlib::threaded_object::wait() const'
obj\main.o:main.cpp:(.text$_ZN4dlib15thread_functionD0Ev[dlib::thread_function::~thread_function()]+0x70): undefined reference to `dlib::threaded_object::~threaded_object()'
obj\main.o:main.cpp:(.text$_ZN4dlib15thread_functionD0Ev[dlib::thread_function::~thread_function()]+0xbe): undefined reference to `dlib::threaded_object::~threaded_object()'
obj\main.o:main.cpp:(.text$_ZN4dlib15thread_functionD1Ev[dlib::thread_function::~thread_function()]+0x43): undefined reference to `dlib::threaded_object::wait() const'
obj\main.o:main.cpp:(.text$_ZN4dlib15thread_functionD1Ev[dlib::thread_function::~thread_function()]+0x70): undefined reference to `dlib::threaded_object::~threaded_object()'
obj\main.o:main.cpp:(.text$_ZN4dlib15thread_functionD1Ev[dlib::thread_function::~thread_function()]+0xb3): undefined reference to `dlib::threaded_object::~threaded_object()'
obj\main.o:main.cpp:(.text$_ZN4dlib15server_kernel_1INS_12set_kernel_1IPNS_10connectionENS_27binary_search_tree_kernel_1IS3_cNS_33memory_manager_stateless_kernel_1IcEESt4lessIS3_EEES6_EEE5clearEv[dlib::server_kernel_1<dlib::set_kernel_1<dlib::connection*, dlib::binary_search_tree_kernel_1<dlib::connection*, char, dlib::memory_manager_stateless_kernel_1<char>, std::less<dlib::connection*> >, dlib::memory_manager_stateless_kernel_1<char> > >::clear()]+0x10e): undefined reference to `dlib::connection::shutdown()'
obj\main.o:main.cpp:(.text$_ZN4dlib15server_kernel_1INS_12set_kernel_1IPNS_10connectionENS_27binary_search_tree_kernel_1IS3_cNS_33memory_manager_stateless_kernel_1IcEESt4lessIS3_EEES6_EEE18service_connectionEPv[dlib::server_kernel_1<dlib::set_kernel_1<dlib::connection*, dlib::binary_search_tree_kernel_1<dlib::connection*, char, dlib::memory_manager_stateless_kernel_1<char>, std::less<dlib::connection*> >, dlib::memory_manager_stateless_kernel_1<char> > >::service_connection(void*)]+0x9e): undefined reference to `dlib::close_gracefully(dlib::connection*, unsigned long)'
obj\main.o:main.cpp:(.text$_ZN4dlib15server_kernel_1INS_12set_kernel_1IPNS_10connectionENS_27binary_search_tree_kernel_1IS3_cNS_33memory_manager_stateless_kernel_1IcEESt4lessIS3_EEES6_EEE18service_connectionEPv[dlib::server_kernel_1<dlib::set_kernel_1<dlib::connection*, dlib::binary_search_tree_kernel_1<dlib::connection*, char, dlib::memory_manager_stateless_kernel_1<char>, std::less<dlib::connection*> >, dlib::memory_manager_stateless_kernel_1<char> > >::service_connection(void*)]+0x352): undefined reference to `dlib::logger::logger_stream::print_end_of_line()'
obj\main.o:main.cpp:(.text$_ZN4dlib15server_kernel_1INS_12set_kernel_1IPNS_10connectionENS_27binary_search_tree_kernel_1IS3_cNS_33memory_manager_stateless_kernel_1IcEESt4lessIS3_EEES6_EEE18service_connectionEPv[dlib::server_kernel_1<dlib::set_kernel_1<dlib::connection*, dlib::binary_search_tree_kernel_1<dlib::connection*, char, dlib::memory_manager_stateless_kernel_1<char>, std::less<dlib::connection*> >, dlib::memory_manager_stateless_kernel_1<char> > >::service_connection(void*)]+0x3c5): undefined reference to `dlib::logger::logger_stream::print_header_and_stuff()'
obj\main.o:main.cpp:(.text$_ZN4dlib15server_kernel_1INS_12set_kernel_1IPNS_10connectionENS_27binary_search_tree_kernel_1IS3_cNS_33memory_manager_stateless_kernel_1IcEESt4lessIS3_EEES6_EEE18service_connectionEPv[dlib::server_kernel_1<dlib::set_kernel_1<dlib::connection*, dlib::binary_search_tree_kernel_1<dlib::connection*, char, dlib::memory_manager_stateless_kernel_1<char>, std::less<dlib::connection*> >, dlib::memory_manager_stateless_kernel_1<char> > >::service_connection(void*)]+0x3f3): undefined reference to `dlib::logger::logger_stream::print_end_of_line()'
obj\main.o:main.cpp:(.text$_ZN4dlib15server_kernel_1INS_12set_kernel_1IPNS_10connectionENS_27binary_search_tree_kernel_1IS3_cNS_33memory_manager_stateless_kernel_1IcEESt4lessIS3_EEES6_EEE5startEv[dlib::server_kernel_1<dlib::set_kernel_1<dlib::connection*, dlib::binary_search_tree_kernel_1<dlib::connection*, char, dlib::memory_manager_stateless_kernel_1<char>, std::less<dlib::connection*> >, dlib::memory_manager_stateless_kernel_1<char> > >::start()]+0x58): undefined reference to `dlib::create_listener(dlib::listener*&, unsigned short, std::string const&)'
obj\main.o:main.cpp:(.text$_ZN4dlib15server_kernel_1INS_12set_kernel_1IPNS_10connectionENS_27binary_search_tree_kernel_1IS3_cNS_33memory_manager_stateless_kernel_1IcEESt4lessIS3_EEES6_EEE5startEv[dlib::server_kernel_1<dlib::set_kernel_1<dlib::connection*, dlib::binary_search_tree_kernel_1<dlib::connection*, char, dlib::memory_manager_stateless_kernel_1<char>, std::less<dlib::connection*> >, dlib::memory_manager_stateless_kernel_1<char> > >::start()]+0x11d): undefined reference to `dlib::listener::accept(dlib::connection*&, unsigned long)'
obj\main.o:main.cpp:(.text$_ZN4dlib15server_kernel_1INS_12set_kernel_1IPNS_10connectionENS_27binary_search_tree_kernel_1IS3_cNS_33memory_manager_stateless_kernel_1IcEESt4lessIS3_EEES6_EEE5startEv[dlib::server_kernel_1<dlib::set_kernel_1<dlib::connection*, dlib::binary_search_tree_kernel_1<dlib::connection*, char, dlib::memory_manager_stateless_kernel_1<char>, std::less<dlib::connection*> >, dlib::memory_manager_stateless_kernel_1<char> > >::start()]+0x244): undefined reference to `dlib::threads_kernel_shared::thread_pool()'
obj\main.o:main.cpp:(.text$_ZN4dlib15server_kernel_1INS_12set_kernel_1IPNS_10connectionENS_27binary_search_tree_kernel_1IS3_cNS_33memory_manager_stateless_kernel_1IcEESt4lessIS3_EEES6_EEE5startEv[dlib::server_kernel_1<dlib::set_kernel_1<dlib::connection*, dlib::binary_search_tree_kernel_1<dlib::connection*, char, dlib::memory_manager_stateless_kernel_1<char>, std::less<dlib::connection*> >, dlib::memory_manager_stateless_kernel_1<char> > >::start()]+0x25b): undefined reference to `dlib::threads_kernel_shared::threader::create_new_thread(void (*)(void*), void*)'
obj\main.o:main.cpp:(.text$_ZN4dlib15server_kernel_1INS_12set_kernel_1IPNS_10connectionENS_27binary_search_tree_kernel_1IS3_cNS_33memory_manager_stateless_kernel_1IcEESt4lessIS3_EEES6_EEE5startEv[dlib::server_kernel_1<dlib::set_kernel_1<dlib::connection*, dlib::binary_search_tree_kernel_1<dlib::connection*, char, dlib::memory_manager_stateless_kernel_1<char>, std::less<dlib::connection*> >, dlib::memory_manager_stateless_kernel_1<char> > >::start()]+0x4a3): undefined reference to `dlib::listener::~listener()'
obj\main.o:main.cpp:(.text$_ZN4dlib15server_kernel_1INS_12set_kernel_1IPNS_10connectionENS_27binary_search_tree_kernel_1IS3_cNS_33memory_manager_stateless_kernel_1IcEESt4lessIS3_EEES6_EEE5startEv[dlib::server_kernel_1<dlib::set_kernel_1<dlib::connection*, dlib::binary_search_tree_kernel_1<dlib::connection*, char, dlib::memory_manager_stateless_kernel_1<char>, std::less<dlib::connection*> >, dlib::memory_manager_stateless_kernel_1<char> > >::start()]+0x648): undefined reference to `dlib::connection::~connection()'
obj\main.o:main.cpp:(.text$_ZN4dlib15server_kernel_1INS_12set_kernel_1IPNS_10connectionENS_27binary_search_tree_kernel_1IS3_cNS_33memory_manager_stateless_kernel_1IcEESt4lessIS3_EEES6_EEE5startEv[dlib::server_kernel_1<dlib::set_kernel_1<dlib::connection*, dlib::binary_search_tree_kernel_1<dlib::connection*, char, dlib::memory_manager_stateless_kernel_1<char>, std::less<dlib::connection*> >, dlib::memory_manager_stateless_kernel_1<char> > >::start()]+0x949): undefined reference to `dlib::listener::~listener()'
obj\main.o:main.cpp:(.text$_ZN4dlib15server_kernel_1INS_12set_kernel_1IPNS_10connectionENS_27binary_search_tree_kernel_1IS3_cNS_33memory_manager_stateless_kernel_1IcEESt4lessIS3_EEES6_EEE5startEv[dlib::server_kernel_1<dlib::set_kernel_1<dlib::connection*, dlib::binary_search_tree_kernel_1<dlib::connection*, char, dlib::memory_manager_stateless_kernel_1<char>, std::less<dlib::connection*> >, dlib::memory_manager_stateless_kernel_1<char> > >::start()]+0x9bd): undefined reference to `dlib::connection::~connection()'
obj\main.o:main.cpp:(.text$_ZN4dlib15server_kernel_1INS_12set_kernel_1IPNS_10connectionENS_27binary_search_tree_kernel_1IS3_cNS_33memory_manager_stateless_kernel_1IcEESt4lessIS3_EEES6_EEE5startEv[dlib::server_kernel_1<dlib::set_kernel_1<dlib::connection*, dlib::binary_search_tree_kernel_1<dlib::connection*, char, dlib::memory_manager_stateless_kernel_1<char>, std::less<dlib::connection*> >, dlib::memory_manager_stateless_kernel_1<char> > >::start()]+0xdd0): undefined reference to `dlib::listener::~listener()'
obj\main.o:main.cpp:(.text$_ZN4dlib15server_kernel_1INS_12set_kernel_1IPNS_10connectionENS_27binary_search_tree_kernel_1IS3_cNS_33memory_manager_stateless_kernel_1IcEESt4lessIS3_EEES6_EEE5startEv[dlib::server_kernel_1<dlib::set_kernel_1<dlib::connection*, dlib::binary_search_tree_kernel_1<dlib::connection*, char, dlib::memory_manager_stateless_kernel_1<char>, std::less<dlib::connection*> >, dlib::memory_manager_stateless_kernel_1<char> > >::start()]+0xdf4): undefined reference to `dlib::connection::~connection()'
obj\main.o:main.cpp:(.text$_ZN4dlib15server_kernel_1INS_12set_kernel_1IPNS_10connectionENS_27binary_search_tree_kernel_1IS3_cNS_33memory_manager_stateless_kernel_1IcEESt4lessIS3_EEES6_EEE5startEv[dlib::server_kernel_1<dlib::set_kernel_1<dlib::connection*, dlib::binary_search_tree_kernel_1<dlib::connection*, char, dlib::memory_manager_stateless_kernel_1<char>, std::less<dlib::connection*> >, dlib::memory_manager_stateless_kernel_1<char> > >::start()]+0xf7c): undefined reference to `dlib::listener::~listener()'
obj\main.o:main.cpp:(.text$_ZN4dlib15server_kernel_1INS_12set_kernel_1IPNS_10connectionENS_27binary_search_tree_kernel_1IS3_cNS_33memory_manager_stateless_kernel_1IcEESt4lessIS3_EEES6_EEE5startEv[dlib::server_kernel_1<dlib::set_kernel_1<dlib::connection*, dlib::binary_search_tree_kernel_1<dlib::connection*, char, dlib::memory_manager_stateless_kernel_1<char>, std::less<dlib::connection*> >, dlib::memory_manager_stateless_kernel_1<char> > >::start()]+0xfa0): undefined reference to `dlib::connection::~connection()'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 5 seconds)
33 errors, 0 warnings
4

4 回答 4

4

看起来您缺少告诉 gcc 与 dlib 库链接的选项。我不知道要为 Code::Blocks 做的具体设置是什么,但一般来说,使用 gcc 你需要像这样使用 -l 选项:

gcc <other compiler options> -ldlib

更新

好的,我对此进行了更好的研究,看起来 dlib 实际上并没有为您构建库。外部库包工作的常见方式是提供一个生成文件,该文件为您构建需要链接的 *.a 或 *.so 文件。但是 dlib 不提供此功能,并且需要您将其特殊添加source.cpp到您的构建设置中。同样,我不知道如何在代码块中执行此操作,但是在编译和链接该套接字示例时,以下内容对我有用:

g++ -o dlib_socket -I ../dlib-17.42/ -lpthread -lX11 dlib_socket.cpp ../dlib-17.42/dlib/all/source.cpp

在上面的示例中,我将 dlib 提取到父目录中(因此是 ../dlib-17.42/dlib)

于 2011-09-05T03:04:21.457 回答
1

你读过 dlib 的如何编译页面吗?听起来您忘记将 dlib/all/source.cpp 添加到您的项目中。添加它应该会使那些链接器错误消失。此外,由于您使用的是 Code::Blocks,因此您需要告诉它与以下 Windows 库链接:gdi32、comctl32、user32、ws2_32 和 imm32。

还值得指出的是,有一个免费版本的Visual Studio 2010,您可以使用它来编译这些 C++ 程序。如果你在 Windows 上编译,我肯定会推荐使用这个编译器而不是 Code::Blocks。

在我的系统上(Windows XP 在具有 3.5GB RAM 的虚拟机 VM 中)Code::Blocks 使用上述方法编译没有问题。但是,如果它拒绝编译 dlib/all/source.cpp,您应该将 dlib/all/source.cpp 中列出的文件单独添加到您的项目中。包含 source.cpp 的唯一原因是为了方便,实际上不需要它。

于 2011-09-05T03:47:04.887 回答
0

首先也是最重要的 http://beej.us/guide/bgnet/告诉你关于套接字编程你需要知道的一切。在您了解基础知识之前跳入库会适得其反。大多数套接字库都是为您处理数据结构的简单包装器。

这是我多年前为 Windows 写的一门课。

#pragma once
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
class cMySocket {
public:
    cMySocket(void);
    ~cMySocket(void);

    int startup_server(int type, unsigned __int16 port, const char* ip );
    void shutdown_server();
    operator SOCKET();
    cMySocket & operator=(const SOCKET &s);

    bool           running;
    SOCKET         m_socket;
    sockaddr_in    service;
    sockaddr_in    client_addr;
};
cMySocket::cMySocket(void){
    m_socket = SOCKET_ERROR;
    running = false;
}

cMySocket::~cMySocket(void){
    if( m_socket != SOCKET_ERROR )
        closesocket(m_socket);
}

int cMySocket::startup_server(int type, unsigned __int16 port, const char* ip ){
    unsigned long iMode = 1;

    m_socket = socket(AF_INET, type, 0);
    if (m_socket == INVALID_SOCKET){
        throw "Error at socket()\n";
    }

    if( ioctlsocket(m_socket, FIONBIO, &iMode) == SOCKET_ERROR){
        closesocket(m_socket);
        throw "Error at ioctlsocket()\n";
    }

    memset( &service, 0, sizeof( service ));
    service.sin_family = AF_INET;
    service.sin_addr.s_addr = inet_addr(ip);
    service.sin_port = htons(port);

    if( bind(m_socket, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR ){
        closesocket(m_socket);
        throw "Error at bind()\n";
    }
    iMode = 1;
    if ( ioctlsocket( m_socket, FIONBIO, &iMode ) )
    {
        closesocket(m_socket);
        throw "Error at iostlcosket()\n";
    }
    if( type == SOCK_STREAM ){
        if( listen( m_socket, 10) == SOCKET_ERROR ){
            closesocket(m_socket);
            throw "Error at listen()\n";
        }
    }
    running = true;
    return 1;
}

void cMySocket::shutdown_server(){
    closesocket( m_socket );
    m_socket = SOCKET_ERROR;
    running = false;
}

cMySocket::operator SOCKET () { 
    return m_socket; 
}

cMySocket & cMySocket::operator=(const SOCKET &s){  
    m_socket = s;
    return *this;
}

你仍然必须在你的 on 上使用 connect、accept、send、recv 功能,以及错误检查。除非您使用库提供的某种隐含协议,否则我建议您自己编写套接字实现。

于 2011-09-05T03:13:10.233 回答
-1

最好不要使用 C::B。试试 NetBeans,因为它会生成 make 文件。

右键单击项目并转到构建选项。在那里你会找到链接器。从那里添加库。

http://www.learncpp.com/cpp-tutorial/a3-using-libraries-with-codeblocks/

于 2011-09-05T03:08:26.257 回答