簡體   English   中英

C ++-鏈接器錯誤

[英]C++ - Linker error

我是C ++的新手,我真的不知道為什么這段簡短的代碼會導致鏈接器錯誤。 就每個程序長度的挫敗感而言,這對我來說排名很高。

該程序最初是基於gloox(一個用於XMPP聊天的C ++庫)而創建的,該gloox僅以單個文件的形式編寫了該程序。 我正在嘗試將其拆分為多個文件,以使其具有適當的樣式。 這些是程序的唯一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__) */

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

錯誤:

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

您在main.cpp文件中有一個gloox_connectionlistener.cpp文件的include語句。 您不應該將include用於cpp文件,而是分別編譯文件。 更改include語句以改為使用“ .h”文件,然后進行類似於以下的編譯。

gcc -o myprogram main.cpp gloox_connectionlistener.cpp

這是唯一的代碼嗎?

鏈接器錯誤“重復符號”通常意味着您的程序中潛伏着重復的全局變量或函數。 通常,這是由於在多個源單元中包含包含這些全局變量/函數的標頭引起的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM