简体   繁体   English

在Qt中创建C ++库

[英]Creating c++ library in Qt

I'm new in linux and Qt and I'm trying to create c++ library in Qt. 我是linux和Qt的新手,正在尝试在Qt中创建c ++库。 I have library called imaglib with several classes in it. 我有一个名为imaglib的库,其中包含几个类。 I have done this so far: 到目前为止,我已经做到了:

imaglib_global.h: imaglib_global.h:

#ifndef IMAGLIB_GLOBAL_H
#define IMAGLIB_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(IMAGLIB_LIBRARY)
#  define IMAGLIBSHARED_EXPORT Q_DECL_EXPORT
#else
#  define IMAGLIBSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // IMAGLIB_GLOBAL_H

imaglib.h: imaglib.h:

#ifndef IMAGLIB_H
#define IMAGLIB_H

#include "imaglib_global.h"
#include "imagClass.h"

class IMAGLIBSHARED_EXPORT IMaglib
{

public:
    IMaglib();
};

#endif // IMAGLIB_H

imagClass.h: imagClass.h:

#include "imaglib_global.h"

class IMAGLIBSHARED_EXPORT imagClass
{

public:
    imagClass();

    void ImagOperation();

    // Other data goes here
};

// there are several imag classes

When I build this it works and creates, in a debug folder .o files and .so, .so.1, .so.1.0 and .so.1.0.0 but when I try to include this in a application it doesn't work. 当我构建它时,它可以工作并在调试文件夹中创建.o文件和.so,.so.1,.so.1.0和.so.1.0.0,但是当我尝试将其包含在应用程序中时,它不会工作。 I've tried to copy these files in a folder called imagLib in my project and added this code in application.pro file: 我试图将这些文件复制到项目中名为imagLib的文件夹中,并将此代码添加到application.pro文件中:

TARGET = MyApp
TEMPLATE = app

DEPENDPATH += imagLib
INCLUDEPATH += imagLib
LIBS+=  -L imaglib/debug -llibiMaglib 
// It creates libiMaglib.so file so I thought I should use this one but it gives me error on compilation "cannot find -llibiiMaglib"

After that I've tried to build library from terminal: 之后,我尝试从终端构建库:

ar crv imaglibrary.so imaglib.o imagClass.o

but it gives me same error when I try to build application with imaglibrary.so and it gives me the same error. 但是当我尝试使用imaglibrary.so构建应用程序时,它给了我同样的错误,并且给了我同样的错误。 How to fix this? 如何解决这个问题?

EDIT 编辑

I have also tried to create .lib file in terminal and include it in application.pro file like .so file shown above. 我也尝试在终端中创建.lib文件,并将其包含在application.pro文件中,如上面显示的.so文件。 It works but it gives me different error: 它有效,但给了我不同的错误:

"undefined reference to imagClass::ImagOperation()" when I try to create object and call it from code. 当我尝试创建对象并从代码中调用它时,“对imagClass :: ImagOperation()的未定义引用”。

If the name of the built library is "libimaglib", then you should use the -l switch like this 如果构建的库的名称是“ libimaglib”,则应使用-l开关,如下所示

-limaglib

You could think of it as -l replaces "lib". 您可以将其视为-l替换“ lib”。 Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM