gpt4 book ai didi

c++ - 神秘的链接器错误

转载 作者:行者123 更新时间:2023-11-30 01:29:15 25 4
gpt4 key购买 nike

我编写了一个使用一些类和 Protocol Buffer 的简单程序。这些类是为了使计算机之间的连接和发送消息更容易。编译成功。但是,链接器说:

server.o: In function `main':

server.cpp:(.text+0x24): undefinedreference to`dataExchange::Server::Server(unsignedint)'

server.cpp:(.text+0x39):undefined reference to`dataExchange::Server::accept()'

server.cpp:(.text+0x51): undefinedreference to`dataExchange::Connection::receive()'collect2: ld returned 1 exit status

代码:服务器.cpp:

#include <iostream>

#include "connection.h"
#include "protocol.pb.h"

using namespace std;
using namespace msg;
using namespace dataExchange;

int main() {
Server server(1234);
while(1) {
Connection con = server.accept();
Annoucement ann = con.receive();
cout << ann.typ() << endl;
}
}

连接.h:

namespace dataExchange {

class SocketMaintenance {
protected:
sockaddr_in addr;
int s;
public:
SocketMaintenance(const unsigned int) throw();
SocketMaintenance(const sockaddr_in, const int) throw();
SocketMaintenance(const SocketMaintenance&) throw();
~SocketMaintenance();
void write(const char*, const int);
};

class Connection {
FileOutputStream* raw_output;
FileInputStream* raw_input;
char buffer[1024];
public:
Connection(const char*, const unsigned int);// throw(WrongAddress);
Connection(const SocketMaintenance&);//throw(ConnectFailed);
void send(const Message&) throw();
Annoucement receive() throw();
};

class Server {
public:
Server(const unsigned int);// throw(BindFailed,ListenFailed);
Connection accept() throw();
};

}

链接器失败的原因是什么?


构建过程:

g++ server.cpp -c; g++ 协议(protocol).pb.cc -c; g++连接.cpp-c;g++ server.o protocol.pb.o connection.o -lprotobuf

连接.cpp

using namespace std;
using namespace msg;
using namespace google::protobuf;
using namespace google::protobuf::io;

namespace dataExchange {

class SocketMaintenance {
protected:
sockaddr_in addr;
int s;
public:
SocketMaintenance(const unsigned int port) {
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
s = socket(AF_INET, SOCK_STREAM, 0);
}
SocketMaintenance(const sockaddr_in Addr, const int Socket) {
addr = Addr;
s = Socket;
}
SocketMaintenance(const SocketMaintenance& source) {
addr = source.addr;
s = source.s;
}
~SocketMaintenance() {
close(s);
}
void write(const char* buffer, const int n) {
::write(s, buffer, n);
}
};

class Connection : public SocketMaintenance {
FileOutputStream* raw_output;
FileInputStream* raw_input;
char buffer[1024];
public:
Connection(const char* IP, const unsigned int port) : SocketMaintenance(port) {
if(inet_pton(AF_INET, IP, &addr.sin_addr)<=0) {
throw WrongAddress();
}
if(connect(s, (sockaddr*)&addr, sizeof(addr))<0) {
throw ConnectFailed();
}
raw_output = new FileOutputStream(s);
raw_input = new FileInputStream(s);
}
Connection(const SocketMaintenance& source) : SocketMaintenance(source) {}
~Connection() {
delete raw_output;
}
void send(const Message& msg) throw(EmptyMessage) {
CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
int n = msg.ByteSize();
if(n<=0) throw EmptyMessage();
coded_output->WriteVarint32(n);
delete coded_output;
raw_output->Flush();
msg.SerializeToArray(buffer, n);
SocketMaintenance::write(buffer, n);
}
Annoucement receive() throw() {
CodedInputStream* coded_input = new CodedInputStream(raw_input);
google::protobuf::uint32 n;
coded_input->ReadVarint32(&n);
char *b;
int m;
coded_input->GetDirectBufferPointer((const void**)&b, &m);
Annoucement ann;
ann.ParseFromArray(b, n);
return ann;
}

};

class Server : public SocketMaintenance {
public:
Server(const unsigned int port) : SocketMaintenance(port) {
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(s, (struct sockaddr *)&addr, sizeof(addr))<0) {
throw BindFailed();
}
if(listen(s, 5)<0) {
throw ListenFailed();
}
}
Connection accept() throw() {
sockaddr_in client;
socklen_t client_len;
int client_socket = ::accept(s, (sockaddr*)&client, &client_len);
return Connection(SocketMaintenance(client, client_socket));
}
};

}

最佳答案

很可能忘记编译和/或链接 “connection.cpp”。我至少希望你有这样一个文件,它实现了所有的功能?


我认为您对 .h 和 .cpp 的拆分方式存在一些根本性的误解。在 .cpp 中,您不需要简单地重新声明类,这次是使用在其中定义的函数。你需要这样做:

// connection.cpp
namespace dataExchange{
// define the constructor of Server
Server::Server(const unsigned int port)
: SocketMaintenance(port)
{
// ....
}

// define the accept function of Server
Connection accept() throw(){
// ...
}
// ...
}

但这只是问题的一部分,看来你需要得到a good C++ book .例如,您需要在声明类的 header 中进行任何继承。

关于c++ - 神秘的链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6254242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com