C++ - 链接器错误
C++ - Linker error
我是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
这是唯一的代码吗?
"重复符号"的链接器错误通常意味着程序中潜伏着重复的全局变量或函数。 通常,这是由于在多个源单元中包含包含这些全局变量/函数的标头引起的。
相关文章:
- Netbeans 10:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- CMake 错误:链接器命令失败,退出代码为 1 和 cpp.o 文件
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 如何修复 clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- Android NDK.Build命令失败.未定义的引用.clang++:错误:链接器命令失败,退出代码为1
- 使用cmake错误链接boost日志
- 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用):在 Macbook 上
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- clang:错误:链接器命令失败,C++代码中的退出代码为 1(使用 -v 查看调用)
- 静态库中的 g++ 错误链接函数
- 未定义的参考错误链接天然函数Android
- C++编译错误:ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v
- QT Q_PROPERTY错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- OSX MOJAVE -LD:架构X86_64 clang找不到符号:错误:链接器命令失败,出口代码1
- C 错误链接器命令失败了出口代码1(使用-V查看调用)
- LD:架构x86_64 clang找不到符号:错误:链接器命令失败,出口代码1(使用-v to See
- 聚输出错误 - 链接列表
- Xcode链接器错误:链接器命令失败,退出代码为1(使用-v查看调用)
- 尝试构建C DLIB示例无法与数百个未定义的参考错误链接