我在建筑物后有这个链接错误,但不知道是否有证据。我检查了链接器属性中的所有外部依赖项,但没有。对你来说,标题的主体和cpp:
文件mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <activemq/library/ActiveMQCPP.h>
#include <decaf/lang/Thread.h>
#include <decaf/lang/Runnable.h>
#include <decaf/util/concurrent/CountDownLatch.h>
#include <decaf/lang/Integer.h>
#include <decaf/lang/Long.h>
#include <decaf/lang/System.h>
#include <activemq/core/ActiveMQConnectionFactory.h>
#include <activemq/util/Config.h>
#include <cms/Connection.h>
#include <cms/Session.h>
#include <cms/TextMessage.h>
#include <cms/BytesMessage.h>
#include <cms/MapMessage.h>
#include <cms/ExceptionListener.h>
#include <cms/MessageListener.h>
#include <include/IfacomAmqSender.h>
#include <include/IfacomAmqReceiver.h>
using namespace activemq::core;
using namespace decaf::util::concurrent;
using namespace decaf::util;
using namespace decaf::lang;
using namespace cms;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void onMessage(const Message*);
void connectionSender();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
文件mainwindow.cpp
#include "include/IfacomAmqSender.h"
#include "include/IfacomAmqReceiver.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <activemq/library/ActiveMQCPP.h>
#include <decaf/lang/Thread.h>
#include <decaf/lang/Runnable.h>
#include <decaf/util/concurrent/CountDownLatch.h>
#include <decaf/lang/Integer.h>
#include <decaf/lang/Long.h>
#include <decaf/lang/System.h>
#include <activemq/core/ActiveMQConnectionFactory.h>
#include <activemq/util/Config.h>
#include <cms/Connection.h>
#include <cms/Session.h>
#include <cms/TextMessage.h>
#include <cms/BytesMessage.h>
#include <cms/MapMessage.h>
#include <cms/ExceptionListener.h>
#include <cms/MessageListener.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
/*********** Definition m_IfacomMessageBroker ******************/
std::string brokerURI = "failover://(tcp://localhost:61613?wireFormat=stomp)"; // localhost;
// brokerURI = "failover://(tcp://localhost:61616)"; // localhost
// Queue name
std::string destName = "IFACOM-CMS";
// Queue or Topic
bool useTopics = false; // true=Topic, false=Queue
// SESSION_TRANSACTED or AUTO_ACKNOWLEDGE
bool sessionTransacted = false; // if true, commit all messages
// Message parametres
int numMess=1; int waitMillis=1000;
// Definition of object m_IfacomMessageBroker
//IfacomAmqSender m_IfacomMessageBroker(brokerURI,numMess, useTopics, sessionTransacted, destName,waitMillis);
IfacomAmqSender* m_IfacomMessageBroker = new IfacomAmqSender(brokerURI,numMess, useTopics, sessionTransacted, destName,waitMillis);
void MainWindow::connectionSender()
{
activemq::library::ActiveMQCPP::initializeLibrary();
// ***** Initialisation **************************************************************
//m_IfacomMessageBroker.initConnection();
m_IfacomMessageBroker->initConnection();
m_IfacomMessageBroker->getSession()->createTextMessage();
//IfacomAmqReceiver IfacomAmqReceiverBroker(brokerURI,10, useTopics, sessionTransacted, destName,2000);
//IfacomAmqReceiverBroker.initConnection();
//IfacomAmqReceiverBroker.getConsumer()->setMessageListener(this);
}
void MainWindow::on_pushButton_clicked()
{
connectionSender();
}
void MainWindow::on_pushButton_2_clicked()
{
}
文件main.cpp
#include "mainwindow.h"
#include <include\IfacomAmqSender.h>
#include <include\IfacomAmqReceiver.h>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication prog(argc, argv);
MainWindow w;
w.show();
return prog.exec();
}
和linkError中调用的文件IfacomAmqSender.h:
#ifndef _IfacomAmqSender_h
#define _IfacomAmqSender_h
#include <activemq/library/ActiveMQCPP.h>
#include <decaf/lang/Thread.h>
#include <decaf/lang/Runnable.h>
#include <decaf/util/concurrent/CountDownLatch.h>
#include <decaf/lang/Integer.h>
#include <decaf/lang/Long.h>
#include <decaf/lang/System.h>
#include <activemq/core/ActiveMQConnectionFactory.h>
#include <activemq/util/Config.h>
#include <cms/Connection.h>
#include <cms/Session.h>
#include <cms/TextMessage.h>
#include <cms/BytesMessage.h>
#include <cms/MapMessage.h>
#include <cms/ExceptionListener.h>
#include <cms/MessageListener.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <memory>
using namespace activemq::core;
using namespace decaf::util::concurrent;
using namespace decaf::util;
using namespace decaf::lang;
using namespace cms;
class IfacomAmqSender : public ExceptionListener{
private:
CountDownLatch m_latch;
CountDownLatch m_doneLatch;
Connection* m_connection;
Session* m_session;
Destination* m_destination;
MessageConsumer* m_consumer;
MessageProducer* m_producer;
std::auto_ptr<TextMessage> m_message;
long m_waitMillis;
bool m_useTopic;
bool m_sessionTransacted;
std::string m_brokerURI;
std::string m_destName;
DeliveryMode m_message_delivery_mode;
int m_message_priority;
IfacomAmqSender(const IfacomAmqSender&);
IfacomAmqSender& operator=(const IfacomAmqSender&);
public:
IfacomAmqSender (const std::string&, int, bool,bool,const std::string&,int);
//explicit IfacomAmqSender(const std::string&, int, bool, bool, const std::string&, int);
//IfacomAmqSender(const std::string&, bool, bool, const std::string&);
~IfacomAmqSender();
void close();
void waitUntilReady();
void cleanup();
// KH
void createConnection();
void createSession();
void createDestination();
void createProducer();
void initConnection();
virtual void sendMessage(std::string);
// Send a ActiveMQ Message
virtual void sendMessage(std::auto_ptr<TextMessage>);
//--------------------------------------------------
// If something bad happens you see it here as this class is also been
// registered as an ExceptionListener with the connection.
virtual void onException(const CMSException&) ;
// Message Priority (0:Lowest - 9:Highest)
void setPriority(int);
int getPriority();
// Message Delivery Mode
void setDeliveryMode(DeliveryMode);
DeliveryMode getDeliveryMode();
Session* getSession();
};
#endif
文件IfacomAmqSender.cpp:
#include <activemq/library/ActiveMQCPP.h>
#include <decaf/lang/Thread.h>
#include <decaf/lang/Runnable.h>
#include <decaf/util/concurrent/CountDownLatch.h>
#include <decaf/lang/Integer.h>
#include <decaf/lang/Long.h>
#include <decaf/lang/System.h>
#include <activemq/core/ActiveMQConnectionFactory.h>
#include <activemq/util/Config.h>
#include <cms/Connection.h>
#include <cms/Session.h>
#include <cms/TextMessage.h>
#include <cms/BytesMessage.h>
#include <cms/MapMessage.h>
#include <cms/ExceptionListener.h>
#include <cms/MessageListener.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <memory>
#include "IfacomAmqSender.h"
using namespace activemq::core;
using namespace decaf::util::concurrent;
using namespace decaf::util;
using namespace decaf::lang;
using namespace cms;
using namespace std;
IfacomAmqSender::IfacomAmqSender(const std::string& brokerURI, int numMessages, bool useTopic = false, bool sessionTransacted = false, const std::string& destName = "IFACOM-CMS", int waitMillis = 1000) :
m_latch(1),
m_doneLatch(numMessages),
m_connection(NULL),
m_session(NULL),
m_destination(NULL),
m_consumer(NULL),
m_waitMillis(waitMillis),
m_useTopic(useTopic),
m_sessionTransacted(sessionTransacted),
m_destName(destName),
m_brokerURI(brokerURI) {
}
/*IfacomAmqSender::IfacomAmqSender(const std::string& brokerURI, bool useTopic = false, bool sessionTransacted = false, const std::string& destName = "IFACOM-CMS") :
m_latch(1),
m_doneLatch(1),
m_connection(NULL),
m_session(NULL),
m_destination(NULL),
m_consumer(NULL),
m_waitMillis(1000),
m_useTopic(useTopic),
m_sessionTransacted(sessionTransacted),
m_destName(destName),
m_brokerURI(brokerURI) {
}*/
IfacomAmqSender::~IfacomAmqSender() {
cleanup();
}
void IfacomAmqSender::close() {
this->cleanup();
}
void IfacomAmqSender::waitUntilReady() {
m_latch.await();
}
//------ Init connexion ---------------
void IfacomAmqSender::createConnection()
{
// Create a ConnectionFactory
auto_ptr<ConnectionFactory> connectionFactory(ConnectionFactory::createCMSConnectionFactory(m_brokerURI));
// Create a Connection
m_connection = connectionFactory->createConnection();
m_connection->start();
m_connection->setExceptionListener(this);
}
void IfacomAmqSender::createSession()
{
// Create a Session
if (this->m_sessionTransacted == true) {
m_session = m_connection->createSession(Session::SESSION_TRANSACTED);
} else {
m_session = m_connection->createSession(Session::AUTO_ACKNOWLEDGE);
}
}
void IfacomAmqSender::createDestination()
{
// Create the destination (Topic or Queue)
if (m_useTopic) {
m_destination = m_session->createTopic(m_destName);
} else {
m_destination = m_session->createQueue(m_destName);
}
}
void IfacomAmqSender::createProducer()
{
m_producer = m_session->createProducer(m_destination);
m_producer->setDeliveryMode(DeliveryMode::NON_PERSISTENT);
}
void IfacomAmqSender::initConnection() {
try {
createConnection();
// Create the session
createSession();
// Create the destination (Topic or Queue)
createDestination();
// Create a MessageProducer from the Session to the Topic or Queue
createProducer();
m_producer->setDeliveryMode(DeliveryMode::NON_PERSISTENT);
// Indicate we are ready for messages.
m_latch.countDown();
// Wait while asynchronous messages come in.
m_doneLatch.await(m_waitMillis);
} catch (CMSException& e) {
// Indicate we are ready for messages.
//latch.countDown();
e.printStackTrace();
}
}
void IfacomAmqSender::sendMessage(string text) {
try {
std::auto_ptr<TextMessage> message(m_session->createTextMessage(text));
// to set a property
////message->setIntProperty("Integer", ix);
m_producer->send(message.get());
message->setCMSTimestamp(System::currentTimeMillis());
} catch (CMSException& e) {
e.printStackTrace();
}
}
// Send a ActiveMQ Message
void IfacomAmqSender::sendMessage(std::auto_ptr<TextMessage> amq_message) {
try {
amq_message->setCMSTimestamp(System::currentTimeMillis());
m_producer->send(amq_message.get());
} catch (CMSException& e) {
e.printStackTrace();
}
}
//--------------------------------------------------
// If something bad happens you see it here as this class is also been
// registered as an ExceptionListener with the connection.
void IfacomAmqSender::onException(const CMSException& ex AMQCPP_UNUSED) {
printf("CMS Exception occurred. Shutting down client.\n");
ex.printStackTrace();
exit(1);
}
// Message Priority (0:Lowest - 9:Highest)
void IfacomAmqSender::setPriority(int priority){m_message_priority = priority;}
int IfacomAmqSender::getPriority(){return m_message_priority;}
// Message Delivery Mode
void IfacomAmqSender::setDeliveryMode(DeliveryMode delivery_mode){m_message_delivery_mode = delivery_mode;}
DeliveryMode IfacomAmqSender::getDeliveryMode(){return m_message_delivery_mode;}
Session* IfacomAmqSender::getSession()
{
return m_session;
}
void IfacomAmqSender::cleanup() {
if (m_connection != NULL) {
try {
m_connection->close();
} catch (cms::CMSException& ex) {
ex.printStackTrace();
}
}
// Destroy resources.
try {
delete m_destination;
m_destination = NULL;
delete m_consumer;
m_consumer = NULL;
delete m_session;
m_session = NULL;
delete m_connection;
m_connection = NULL;
} catch (CMSException& e) {
e.printStackTrace();
}
}
错误是:
Error 2 error LNK2019: unresolved external symbol "public: __thiscall IfacomAmqSender::IfacomAmqSender(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int,bool,bool,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int)" (??0IfacomAmqSender@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H_N10H@Z) referenced in function "void __cdecl `dynamic initializer for 'm_IfacomMessageBroker''(void)" (??__Em_IfacomMessageBroker@@YAXXZ) C:\Users\Marco\Desktop\Activemq\ReleaseVersions\GUI-CMS-SENDER\mainwindow.obj GUI-CMS-SENDER
Error 3 error LNK2019: unresolved external symbol "public: void __thiscall IfacomAmqSender::initConnection(void)" (?initConnection@IfacomAmqSender@@QAEXXZ) referenced in function "public: void __thiscall MainWindow::connectionSender(void)" (?connectionSender@MainWindow@@QAEXXZ) C:\Users\Marco\Desktop\Activemq\ReleaseVersions\GUI-CMS-SENDER\mainwindow.obj GUI-CMS-SENDER
Error 4 error LNK2019: unresolved external symbol "public: class cms::Session * __thiscall IfacomAmqSender::getSession(void)" (?getSession@IfacomAmqSender@@QAEPAVSession@cms@@XZ) referenced in function "public: void __thiscall MainWindow::connectionSender(void)" (?connectionSender@MainWindow@@QAEXXZ) C:\Users\Marco\Desktop\Activemq\ReleaseVersions\GUI-CMS-SENDER\mainwindow.obj GUI-CMS-SENDER
命令行是:
/OUT:"debug\\GUI-CMS-SENDER.exe" /MANIFEST /NXCOMPAT /PDB:"debug\GUI-CMS-SENDER.pdb" /DYNAMICBASE "qtmaind.lib" "C:\Qt\Qt5.1.1\5.1.1\msvc2012\\lib\Qt5Widgetsd.lib" "C:\Qt\Qt5.1.1\5.1.1\msvc2012\\lib\Qt5Guid.lib" "C:\Qt\Qt5.1.1\5.1.1\msvc2012\\lib\Qt5Cored.lib" "libEGLd.lib" "libGLESv2d.lib" "gdi32.lib" "user32.lib" "Qt5Cored.lib" "Qt5Guid.lib" "libapr-1.lib" "libactivemq-cppd.lib" "ws2_32.lib" "kernel32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /DEBUG /MACHINE:X86 /SAFESEH /PGD:"debug\GUI-CMS-SENDER.pgd" /SUBSYSTEM:WINDOWS /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"Win32\Debug\GUI-CMS-SENDER.exe.intermediate.manifest" /ERRORREPORT:PROMPT /NOLOGO /LIBPATH:"lib" /LIBPATH:"C:\Qt\Qt5.1.1\5.1.1\msvc2012\\lib" /LIBPATH:"\lib" /LIBPATH:"C:\Users\Marco\Desktop\Activemq\ReleaseVersions\GUI-CMS-SENDER\include" /TLBID:1
答案 0 :(得分:2)
基于最近添加的链接器行,实际上您似乎没有链接到库。您需要将它放入应用程序的qmake项目文件中:
LIBS += -L/path/to/your/library -lyourlibrary
如果这不起作用,请确保您有一个试图链接的库版本,其中包含必要的符号。我通常在Linux上使用“nm -a”o“objdump”,但在Windows上也必须有一些等效的版本。
编辑:由于您似乎使用Visual Studio而不是qmake
,因此您需要在解决方案文件中确保两件事:
1)构建的库有符号。至于此,您需要提供符号,因此请确保它们已内置到库中。您应该将所有必需的文件添加到项目中,但最重要的是源文件(cpp)。
2)您需要确保何时根据库路径和名称正确设置Visual Studio的库构建应用程序。此设置可以通过UI设置选项完成。
以下是一些插图截图:
答案 1 :(得分:1)
当您忘记将源文件包含到项目中时,通常会出现此类问题。
如果您使用的是qmake .pro
项目文件,请确保已将IfacomAmqSender.cpp
包含在其中(如@loentar指出):
SOURCES += (previous source files) \
IfacomAmqSender.cpp
如果您不使用qmake,只需将IfacomAmqSender.cpp
添加到Visual Studio项目(项目 - 添加现有项目......或右键单击解决方案资源管理器 - 添加 - 现有项目...)。
然后一切都应该编译和链接。