문제

I'm trying to link a c++ library (created in Qt) to a console application. In pure c++ I did link the library dll to the program with sucess. In Qt I get the LNK2019: unresolved external symbol.

Library

LibA

headers

*liba_global.h*

#ifndef LIBA_GLOBAL_H
#define LIBA_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(LIBA_LIBRARY)
#  define LIBASHARED_EXPORT Q_DECL_EXPORT
#else
#  define LIBASHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // LIBA_GLOBAL_H

*lib_example.h*

#ifndef LIB_EXAMPLE_H
#define LIB_EXAMPLE_H

#include "liba_global.h"
    //Interface as seen on http://www.tutorialspoint.com/cplusplus/cpp_interfaces.htm
    class LIBASHARED_EXPORT Lib_example
    {
    public:
        Lib_example(){}
        virtual ~Lib_example(){}
        virtual int getId()=0;
        virtual void setId(int id)=0;
    };

#endif // LIB_EXAMPLE_H

*test_a.h*

#ifndef TEST_A_H
#define TEST_A_H

#include "liba_global.h"
#include "lib_example.h"

class LIBASHARED_EXPORT Test_A:public Lib_example
{
public:
    Test_A();
    ~Test_A();
    int getId();
    void setId(int id);
private:
    int id;
};

#endif // TEST_A_H

source

*test_a.cpp*

#include "test_a.h"
int Test_A::getId()
{
    return id;
}

void Test_A::setId(int id)
{
    this->id = id;
}

Test_A::Test_A()
{
    this->id = 0;
}

Test_A::~Test_A()
{ }

pro

QT       -= gui

TARGET = LibA
TEMPLATE = lib

DEFINES += LIBA_LIBRARY

SOURCES += \
    test_a.cpp

HEADERS += lib_example.h\
        liba_global.h \
    test_a.h

unix:!symbian {
    maemo5 {
        target.path = /opt/usr/lib
    } else {
        target.path = /usr/lib
    }
    INSTALLS += target
}

project

main.cpp

#include <QCoreApplication>
#include <iostream>
#include "test_a.h"

using namespace std;
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Test_A test;
    test.setId(2);
    cout<<test.getId()<<endl;
    return a.exec();
}

pro

QT       += core

QT       -= gui

TARGET = ProjUsingLib_A
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../build-LibA-Desktop_Qt_5_1_1_MSVC2012_32bit-Debug/release/ -lLibA
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../build-LibA-Desktop_Qt_5_1_1_MSVC2012_32bit-Debug/debug/ -lLibA
else:unix: LIBS += -L$$PWD/../build-LibA-Desktop_Qt_5_1_1_MSVC2012_32bit-Debug/ -lLibA

INCLUDEPATH += $$PWD/../LibA
DEPENDPATH += $$PWD/../LibA

I wrote the code and linked using Qt Creator.

More details: Lib and Main compiled using MSVC2012_32bit_debug option.

The error:

main.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall Test_A::Test_A(void)" (??0Test_A@@QAE@XZ) referenced in function _main
main.obj:-1: error: LNK2019: unresolved external symbol "public: virtual __thiscall Test_A::~Test_A(void)" (??1Test_A@@UAE@XZ) referenced in function _main
main.obj:-1: error: LNK2019: unresolved external symbol "public: virtual int __thiscall Test_A::getId(void)" (?getId@Test_A@@UAEHXZ) referenced in function _main
main.obj:-1: error: LNK2019: unresolved external symbol "public: virtual void __thiscall Test_A::setId(int)" (?setId@Test_A@@UAEXH@Z) referenced in function _main

Thanks in advance :)

도움이 되었습니까?

해결책

You're not building/linking test_a.cpp. Add it to the SOURCES line of your makefile.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top