未定义的参考问题

发布于 2024-11-30 12:42:48 字数 3660 浏览 1 评论 0 原文

我在 C++ 中遇到此错误,我真的不知道如何深入了解它:

g++ proxy.cpp -lboost_thread -lboost_filesystem -lboost_system

/tmp/ccUHa2s3.o:在函数“main”中: proxy.cpp:(.text+0x1d8): 对 `server::server(std::deque, std::allocator>>const> int)' collect2:ld返回1退出状态

我有以下源代码(我复制了http://alexott.net/common/asio-proxy-async/proxy-conn.cpp.html):

//proxy.cpp:
#include "proxy-server.hpp"

int main(int argc, char** argv) {
    try {
        int thread_num=2;
        if(argc > 1)
            thread_num=boost::lexical_cast<int>(argv[1]);
        ios_deque io_services;
        std::deque<ba::io_service::work> io_service_work;

        boost::thread_group thr_grp;

        for (int i = 0; i < thread_num; ++i) {
            io_service_ptr ios(new ba::io_service);
            io_services.push_back(ios);
            io_service_work.push_back(ba::io_service::work(*ios));
            thr_grp.create_thread(boost::bind(&ba::io_service::run, ios));
        }
        server server(io_services);   //apparently there's some error here?
        thr_grp.join_all();
    } catch (std::exception& e) {
        std::cerr << e.what() << std::endl;
    }


    return 0;
}



//proxy-server.hpp:
#ifndef _PROXY_SERVER_H
#define _PROXY_SERVER_H 1

#include "common.h"
#include "proxy-conn.hpp"

#include <deque>

typedef std::deque<io_service_ptr> ios_deque;

class server {
public:
    server(const ios_deque& io_services, int port=10001);

private:
    void start_accept();
    void handle_accept(connection::pointer new_connection, const bs::error_code& error);

    ios_deque io_services_;
    ba::ip::tcp::acceptor acceptor_;
};


#endif /* _PROXY-SERVER_H */


//proxy-server.cpp:
#include "proxy-server.hpp"

server::server(const ios_deque& io_services, int port)
    : io_services_(io_services),
      acceptor_(*io_services.front(), ba::ip::tcp::endpoint(ba::ip::tcp::v4(), port)) {
    start_accept();
}

void server::start_accept() {
    // Round robin.
    io_services_.push_back(io_services_.front());
    io_services_.pop_front();
    connection::pointer new_connection = connection::create(*io_services_.front());

    acceptor_.async_accept(new_connection->socket(),
                           boost::bind(&server::handle_accept, this, new_connection,
                                       ba::placeholders::error));
}

void server::handle_accept(connection::pointer new_connection, const bs::error_code& error) {
    if (!error) {
        new_connection->start();
        start_accept();
    }
}

有人可以为我指明如何修复此错误的正确方向吗?


编辑

我现在收到以下错误:

g++ proxy.cpp proxy-server.cpp -lboost_thread -lboost_filesystem -lboost_system

/tmp/ccl3DHn7.o:在函数“server::handle_accept(boost::shared_ptr, boost::system::error_code const&)”中: proxy-server.cpp:(.text+0x250): 对 `connection::start()' 的未定义引用 /tmp/ccl3DHn7.o:在函数 `connection::create(boost::asio::io_service&)' 中: proxy-server.cpp:(.text._ZN10connection6createERN5boost4asio10io_serviceE[connection::create(boost::asio::io_service&)]+0x29):对 `connection::connection(boost::asio::io_service&) 的未定义引用;)' collect2:ld返回1退出状态`

I'm getting this error in C++ and I really don't know how to get to the bottom of it:

g++ proxy.cpp -lboost_thread -lboost_filesystem -lboost_system

/tmp/ccUHa2s3.o: In function `main':
proxy.cpp:(.text+0x1d8): undefined reference to `server::server(std::deque<boost::shared_ptr<boost::asio::io_service>, std::allocator<boost::shared_ptr<boost::asio::io_service> > > const&, int)'
collect2: ld returned 1 exit status

I have the following source code (which I copied off http://alexott.net/common/asio-proxy-async/proxy-conn.cpp.html):

//proxy.cpp:
#include "proxy-server.hpp"

int main(int argc, char** argv) {
    try {
        int thread_num=2;
        if(argc > 1)
            thread_num=boost::lexical_cast<int>(argv[1]);
        ios_deque io_services;
        std::deque<ba::io_service::work> io_service_work;

        boost::thread_group thr_grp;

        for (int i = 0; i < thread_num; ++i) {
            io_service_ptr ios(new ba::io_service);
            io_services.push_back(ios);
            io_service_work.push_back(ba::io_service::work(*ios));
            thr_grp.create_thread(boost::bind(&ba::io_service::run, ios));
        }
        server server(io_services);   //apparently there's some error here?
        thr_grp.join_all();
    } catch (std::exception& e) {
        std::cerr << e.what() << std::endl;
    }


    return 0;
}



//proxy-server.hpp:
#ifndef _PROXY_SERVER_H
#define _PROXY_SERVER_H 1

#include "common.h"
#include "proxy-conn.hpp"

#include <deque>

typedef std::deque<io_service_ptr> ios_deque;

class server {
public:
    server(const ios_deque& io_services, int port=10001);

private:
    void start_accept();
    void handle_accept(connection::pointer new_connection, const bs::error_code& error);

    ios_deque io_services_;
    ba::ip::tcp::acceptor acceptor_;
};


#endif /* _PROXY-SERVER_H */


//proxy-server.cpp:
#include "proxy-server.hpp"

server::server(const ios_deque& io_services, int port)
    : io_services_(io_services),
      acceptor_(*io_services.front(), ba::ip::tcp::endpoint(ba::ip::tcp::v4(), port)) {
    start_accept();
}

void server::start_accept() {
    // Round robin.
    io_services_.push_back(io_services_.front());
    io_services_.pop_front();
    connection::pointer new_connection = connection::create(*io_services_.front());

    acceptor_.async_accept(new_connection->socket(),
                           boost::bind(&server::handle_accept, this, new_connection,
                                       ba::placeholders::error));
}

void server::handle_accept(connection::pointer new_connection, const bs::error_code& error) {
    if (!error) {
        new_connection->start();
        start_accept();
    }
}

Can someone please point me in the right direction as to how to fix this error?


Edit

I now get the following error:

g++ proxy.cpp proxy-server.cpp -lboost_thread -lboost_filesystem -lboost_system

/tmp/ccl3DHn7.o: In function `server::handle_accept(boost::shared_ptr<connection>, boost::system::error_code const&)':
proxy-server.cpp:(.text+0x250): undefined reference to `connection::start()'
/tmp/ccl3DHn7.o: In function `connection::create(boost::asio::io_service&)':
proxy-server.cpp:(.text._ZN10connection6createERN5boost4asio10io_serviceE[connection::create(boost::asio::io_service&)]+0x29): undefined reference to `connection::connection(boost::asio::io_service&)'
collect2: ld returned 1 exit status`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如歌彻婉言 2024-12-07 12:42:48

这意味着它找不到 server 构造函数的实现。你是否写过一篇上面没有显示的内容?

编辑:好的,你已经写了一个,但你没有将它传递给编译器。您的 g++ 行中需要有 proxy-server.cpp

编辑2:仅仅编译包含main的文件并包含头文件是不够的。您需要向g++提供所有cpp文件,否则它将无法链接您的程序。

g++ proxy.cpp proxy-server.cpp proxy-conn.cpp -lboost_thread -lboost_filesystem -lboost_system

It means it cannot find the implementation of the server constructor. Have you written one which you do not show above?

EDIT: Ok, so you've written one, but you're not passing it to the compiler. You need to have proxy-server.cpp in your g++ line.

EDIT 2: It is not enough to just compile the file containing main and include the header files. You need to provide all the cpp files to g++, or it will not be able to link your program.

g++ proxy.cpp proxy-server.cpp proxy-conn.cpp -lboost_thread -lboost_filesystem -lboost_system
土豪 2024-12-07 12:42:48

从错误中可以清楚地看出,您已经声明了 server 的构造函数,但定义不可用(要么未编译和链接,要么根本未提供)

您需要在您的cpp 文件。

It's pretty clear from the error, you've declared the constructor for server, but the definition is not available (either not compiled and linked in, or not provided at all)

You need to define it in your cpp file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文