未定义的参考问题
Undefined reference question
我在c++中得到这个错误,我真的不知道如何得到它的底部:
g++ proxy.cpp -lboost_thread -lboost_filesystem -lboost_system
/tmp/ccUHa2s3。在函数' main'中:proxy.cpp:(.text+0x1d8):未定义对'
server::server(std::deque<boost::shared_ptr<boost::asio::io_service>, std::allocator<boost::shared_ptr<boost::asio::io_service> > > 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。0:在函数'
返回1退出状态server::handle_accept(boost::shared_ptr<connection>, boost::system::error_code const&)
'中:proxy-server.cpp:(.text+0x250):未定义的connection::start()
引用/tmp/ccl3DHn7。在函数'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&)
'的未定义引用
这意味着它找不到server
构造函数的实现。你有没有写过一个没有在上面显示的?
EDIT:好,所以你写了一个,但你没有把它传递给编译器。您需要在g++
行中添加proxy-server.cpp
。
EDIT 2:仅仅编译包含main
和包含头文件的文件是不够的。您需要提供所有的cpp文件到g++
,否则它将无法链接您的程序。
g++ proxy.cpp proxy-server.cpp proxy-conn.cpp -lboost_thread -lboost_filesystem -lboost_system
从错误中可以很清楚地看出,您已经声明了server
的构造函数,但是定义不可用(要么没有编译和链接,要么根本没有提供)
您需要在您的cpp
文件中定义它
- 在决定是通过参考还是通过价值时,尺寸真的是一个问题吗
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- C++ 而循环问题 Codecademy 参考 [已解决]
- 我有几个关于参考变量的问题
- MSYS2静态QT未定义的参考问题
- OPENCV筛选未定义的参考问题
- 有关参考和指针的问题
- Python C-API:PYDICT_GETITEM上的分割故障,可能的参考问题
- 参考类型静态数据成员的问题可能是编译器错误
- 通过参考问题
- 命名空间外部变量的未定义参考/多重定义,C 结构问题
- 关于通用参考和左值参考的问题
- 通过参考问题
- 矢量参考问题
- VS 2010构建问题:参考问题
- 课堂自我参考问题
- C++参考问题
- 关于指针和参考文献的两个简短问题
- 这个快速排序有什么问题,我从书中参考它
- OpenCV 2.3编译问题-未定义的参考- Ubuntu 11.10