无法在Qt中设置库依赖

Unable to set library dependency in Qt

本文关键字:设置 依赖 Qt      更新时间:2023-10-16

我有一个项目在Qt,与一个类定义的形式

Project MyP  --- file p.h
#ifndef P_H
#define P_H
#include <QImage>
class PP
{
public:
   static QImage P1(const QImage& a);
}
#endif
Project MyP  --- file p.cpp
#include "p.h"
QImage PP::P1(const QImage& a)
{
   QImage b;
   return b;
}
I created a test project for the class above
project test, file test.h
#ifndef TEST_H
#define TEST_H
#include <QtTest/QtTest>
class Test : public QObject
{
    Q_OBJECT
private slots:
    void TestSomething();
};
#endif 
#include "test.h"
#include "p.h"
void Test::TestSomething()
{
    QImage sourceImage = QImage("source.png");
    QImage newImage = PP::P1(sourceImage);
    int result = 1*2;
    QVERIFY(result == 2);
}
QTEST_MAIN(Test)

在c++中做同样的事情,但我可以很容易地添加一个lib依赖)…但在Qt我得到一个错误

error: undefined reference to `_imp___ZN6PPP1ENS_12P1MethodERK6QImage'

我试图添加库依赖,就像我在这里看到的

http://qt-project.org/wiki/How_to_create_a_library_with_Qt_and_use_it_in_an_application

我添加了MyP头文件

#if defined MYP_LIB
#define MYP_COMMON_DLLSPEC  Q_DECL_EXPORT
#else
#define MYP_COMMON_DLLSPEC Q_DECL_IMPORT
#endif

并将类声明更改为

class MYP_COMMON_DLLSPEC Dither
{
....
}

我添加了pro文件(为了方便,我将发布整个文件)

TEMPLATE = lib
VERSION = 1.0.0.0
CONFIG += staticlib debug
debug   {
    DESTDIR = bin/debug
}
release {
    DESTDIR = bin/release
}
HEADERS += 
    pp.h
SOURCES += 
    pp.cpp 
DEFINES += MYP_LIB

然后在测试项目中,我在概要文件中添加了

DEPENDPATH += . ../myp
INCLUDEPATH += ../myp
win32:LIBS += ../myp/bin/debug/libmyp.a
LIBS+=  -L../myp/bin/debug -lmyp

我一直无法创建一个库文件,或一个dll文件,并已无法让项目一起工作。注意:它们都在同一个溶液中。

如何从单独的项目中正确运行测试?

您必须告诉链接器(分别)从DLL导入和导出符号,因此您必须声明您的类,如:

     Project MyP  --- file p.h
        #ifndef P_H
        #define P_H
       #if defined(MyP_LIBRARY)
       #  define MyPSHARED_EXPORT Q_DECL_EXPORT
       #else
       #  define MyPSHARED_EXPORT Q_DECL_IMPORT
       #endif
        #include <QImage>
        MyPSHARED_EXPORT class PP
        {
        public:
...

并添加

DEFINES += MyP_LIBRARY

到您的MyP。箴文件

更多信息见http://qt project.org/doc/qt - 4.8 -/- sharedlibrary.html