Link Error with application [closed]

2019-07-04 07:05发布

I have this link error in after the building, but no idea to the provenience. I checked all the external dependencies in the properties of the linker, but nothing. For you the body of the headers and cpp:

file 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

file 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()
{

}

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

and the file IfacomAmqSender.h that is recall in the linkError:

#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

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

the errors are:

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

the command Line is:

/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 

This is the command line

Additional Dependencies

Input Linker properties

Here the project

2条回答
劳资没心,怎么记你
2楼-- · 2019-07-04 07:30

Typically such problems arise when you forgot to include source files into your project.

If you are using qmake .pro project file, make sure you have included IfacomAmqSender.cpp into it (as pointed out @loentar):

SOURCES += (previous source files) \
           IfacomAmqSender.cpp

Or just add the IfacomAmqSender.cpp to Visual Studio project (Project — Add Existing Item... or right click in Solution Explorer — Add — Existing Item...) if you are not using qmake.

Everything should compile and link well then.

查看更多
做自己的国王
3楼-- · 2019-07-04 07:47

Based on the recently added linker line, it seems that you are not linking against the library, actually. You would need to put this into your application's qmake project file:

LIBS += -L/path/to/your/library -lyourlibrary

If that does not work, please make sure you have a library version that you are trying to link against which contain the necessary symbols. I usually use "nm -a" o "objdump" on Linux, but there must be some equivalent version of those on Windows as well.

Edit: Since you seem to use Visual Studio as opposed to qmake, you need to ensure about two thing in your solution file:

1) The library built has the symbols. As for this, you need to have the symbols available, so make sure they are built into the library. You should have all the necessary files added to the project, but most importantly the source files (cpp).

2) You need to make sure when you build the application against the library that you have set up Visual Studio correctly in terms of library path and name. This setting can be done by the UI setting options.

Here are some screenshot for illustration:

enter image description here

enter image description here

查看更多
登录 后发表回答