Question

I'm new to C++, and I really can't figure out why this short code is causing a linker error. In terms of frustration per program length, this ranks pretty high for me.

The program was originally based off a gloox (a C++ library for XMPP chat) that wrote the program atrociously as only a single file. I'm trying to split this up in multiple files so it's in proper style. These are the only 3 files to the program (so far):

Gloox_ConnectionListener.cpp:

#include "Gloox_ConnectionListener.h"

void Gloox_ConnectionListener::onConnect() {
    std::cout << "Connected" << std::endl;
}
void Gloox_ConnectionListener::onDisconnect(gloox::ConnectionError e) {
    std::cout << "Disconnected`" << e << std::endl;
}
bool Gloox_ConnectionListener::onTLSConnect(const gloox::CertInfo& info) {
    std::cout << "TLS/SSL secured" << std::endl;
    return true;
}

Gloox_ConnectionListener.h:

#ifndef __bot__Gloox_ConnectionListener__
#define __bot__Gloox_ConnectionListener__

#include <iostream>
#include <gloox/connectionlistener.h>

class Gloox_ConnectionListener : public gloox::ConnectionListener {
public:
    void onConnect();
    void onDisconnect(gloox::ConnectionError e);
    bool onTLSConnect(const gloox::CertInfo& info);
};

#endif /* defined(__bot__Gloox_ConnectionListener__) */

main.cpp:

//A basic gloox tutorial by Anders Schau Knatten
//Read more at http://blog.knatten.org/2012/03/23/basic-gloox-tutorial/
//To compile on Linux: g++ -o bot bot.cpp -lgloox -lpthread

#include <iostream>
#include <string>
#include <cstdarg>

#include <gloox/client.h>
#include <gloox/message.h>
#include <gloox/messagehandler.h>

#include <cryptopp/modes.h>
#include <cryptopp/aes.h>
#include <cryptopp/filters.h>

#include "Gloox_ConnectionListener.cpp"
using namespace std;
using namespace gloox;

/*ostream& operator<<(ostream& os, Message::MessageType type) {
    switch (type) {
        case Message::Chat:
            os << "Chat";
            break;
        case Message::Error:
            os << "Error";
            break;
        case Message::Groupchat:
            os << "Groupchat";
            break;
        case Message::Headline:
            os << "Headline";
            break;
        case Message::Normal:
            os << "Normal";
            break;
        case Message::Invalid:
            os << "Invalid";
            break;
        default:
            os << "unknown type";
            break;
    }
}*/



ostream& operator<<(ostream& os, const Message& stanza) {
    os << "type:'" << stanza.subtype() <<  "' from:'" << stanza.from().full() << "' body:'" << stanza.body() << "'";
    return os;
}

class Bot : public MessageHandler {
public:
    Bot(string username, string password) {

        if (username == "`" && password == "`") {
            username = "example@gmail.com";
            password = "example";
        }
        JID jid(username);
        client = new Client(jid, password);
        connListener = new Gloox_ConnectionListener();
        client->registerMessageHandler(this);
        client->registerConnectionListener(connListener);
        client->connect(true);
    }

    ~Bot() {
        delete client;
        delete connListener;
    }

    void handleMessage(const Message& stanza, MessageSession* session = 0) {
        if (stanza.body() != "") {
            cout << stanza.from().bare() << ": " << stanza.body() << endl;
            Message msg(Message::Chat, stanza.from(), "echo: " + stanza.body());
            client->send(msg);
        }
    }

private:
    Client* client;
    Gloox_ConnectionListener* connListener;
};

int main() {
    cout << "Jabber ID:";
    string username;
    cin >> username;

    cout << "Password:";
    string password;
    cin >> password;

    Bot b(username,password);
}

Error:

ld: 6 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1
Was it helpful?

Solution

You have an include statement for your gloox_connectionlistener.cpp file in the main.cpp file. You shouldn't use includes for cpp files, you compile the file separately. Change the include statement to use the ".h" file instead and then compile similar to below.

gcc -o myprogram main.cpp gloox_connectionlistener.cpp

OTHER TIPS

Is this the only code?

A linker error of "duplicate symbol" usually means that you have duplicate global variables or functions lurking in your program. Usually this is caused by including a header that contains these global variables/functions in multiple source units.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top