0

我是 C++ 新手,我真的无法弄清楚为什么这个短代码会导致链接器错误。就每个节目长度的挫败感而言,这对我来说排名很高。

该程序最初基于一个 gloox(一个用于 XMPP 聊天的 C++ 库),该程序仅将程序写成一个单独的文件。我正在尝试将其拆分为多个文件,以使其具有正确的样式。这些是程序中仅有的 3 个文件(到目前为止):

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__) */

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

错误:

ld: 6 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1
4

2 回答 2

3

在 main.cpp 文件中有一个包含 gloox_connectionlistener.cpp 文件的语句。您不应该对 cpp 文件使用包含,您应该单独编译该文件。将包含语句更改为使用“.h”文件,然后编译类似于以下内容。

gcc -o myprogram main.cpp gloox_connectionlistener.cpp

于 2013-12-30T05:39:20.540 回答
1

这是唯一的代码吗?

“重复符号”的链接器错误通常意味着您的程序中潜伏着重复的全局变量或函数。通常这是由于在多个源单元中包含包含这些全局变量/函数的标头引起的。

于 2013-12-30T05:25:06.510 回答