Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5981831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:59:58+00:00 2026-05-22T21:59:58+00:00

I have written a simple program that use some classes and procol buffers. These

  • 0

I have written a simple program that use some classes and procol buffers. These classes are to make connecting and sending messages between computers easier. Compilation succeeded. However, linker says:

server.o: In function `main’:

server.cpp:(.text+0x24): undefined
reference to
`dataExchange::Server::Server(unsigned
int)’

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

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

The code:
server.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;
  }
}

connection.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();
  };

}

What is the reason the linker fails?


procedure of building:

g++ server.cpp -c; g++ protocol.pb.cc -c; g++ connection.cpp -c;
g++ server.o protocol.pb.o connection.o -lprotobuf

conection.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));
    }
  };

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:59:58+00:00Added an answer on May 22, 2026 at 9:59 pm

    Forgot to compile and/or link the "connection.cpp", most likely. I atleast hope you have such a file, which implements all the functions?


    I think you have some fundamental misunderstandings how the splitting in .h and .cpp works. In the .cpp, you do not simple redeclare the classes, this time with the functions defined inside of them. You need to do like this:

    // 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(){
      // ...
    }
    // ...
    }
    

    But this is only part of the problem, it seems you need to get a good C++ book. For example, you need to make any inheritence in the header that declares the class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some difficulty compiling a C++ program that I've written. This program
I am very new to JAVA. I have written simple program (in Linux -VIM
I have a program written in C, which includes 2 functions, one function is
I have a legacy database which contains simple data structures (no CODE refs thank
I have written a good bit of code in python and it works great.
I've recently been doing some DirectX 10 work and I'm looking to move to
I have been working through the CS106B course from Stanford, and while completing the
According to the Partition II metadata specification for the GenericParamConstraint table for the CLR,
This question Measure execution time... allows programmers to find good tools to evaluate execution
A raw computer basically just has memory with physical addresses starting at 0, right?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.