C++ - 链接器错误

C++ - Linker error

本文关键字:错误 链接 C++      更新时间:2023-10-16

我是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

在 main.cpp 文件中有一个 gloox_connectionlistener.cpp 文件的包含语句。您不应该对 cpp 文件使用包含,而是单独编译文件。将 include 语句更改为使用".h"文件,然后进行类似于下面的编译。

gcc -o myprogram main.cpp gloox_connectionlistener.cpp

这是唯一的代码吗?

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

相关文章: