我是C ++的新手,但我在编程方面有很多一般经验,所以我熟悉大多数低级概念。
无论如何,我以为我会尝试编写一个IRC客户端,因为IRC似乎有一个相对简单的TCP协议。 (这很容易,您可以通过Telnet访问和使用服务器,这是不切实际的,但可行的)
所以我用Google搜索了“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
答案 0 :(得分:4)
看起来你错过了告诉gcc与dlib库链接的选项。我不知道Code :: Blocks的具体设置是什么,但通常使用gcc你需要像这样使用-l选项:
gcc <other compiler options> -ldlib
好的,我对此有了更好的了解,看起来dlib实际上并没有为你构建一个库。外部库包的常用方法是提供一个makefile,它构建您需要链接的* .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)
答案 1 :(得分:1)
你读过dlib的How to compile页吗?听起来你忘了将dlib / all / source.cpp添加到你的项目中。添加它应该会使这些链接器错误消失。此外,由于您使用Code :: Blocks,您需要告诉它链接这些Windows库:gdi32,comctl32,user32,ws2_32和imm32。
还值得指出的是,有Visual Studio 2010的免费版本可用于编译这些C ++程序。如果你在windows上编译我肯定会建议使用这个编译器而不是Code :: Blocks。
在我的系统上(具有3.5GB RAM的虚拟盒VM中的Windows XP)Code :: Blocks使用上述方法编译没有问题。但是,如果它拒绝编译dlib / all / source.cpp,则应该将dlib / all / source.cpp中列出的文件单独添加到项目中。包含source.cpp的唯一原因是为了方便,实际上并不需要。
答案 2 :(得分:0)
最好不要使用C :: B。尝试NetBeans,因为它将生成make文件。
右键单击该项目并转到构建选项。在那里你会找到链接器。从那里添加库。
http://www.learncpp.com/cpp-tutorial/a3-using-libraries-with-codeblocks/
答案 3 :(得分:0)
首先和formost http://beej.us/guide/bgnet/告诉您有关套接字编程的所有信息。 在了解基础知识之前跳进库中会对套接字产生反作用。 大多数套接字libarys都是简单的包装器,可以为你处理数据结构。
这是我多年前为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函数,以及错误检查。 除非您使用由您的图书馆提供的某种受限制的协议,否则我建议您自己编写套接字修改。