Я новичок в 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