在使用Qt开发过程中,我们有时需要使用Qt创建动态库,通常情况下创建的动态库没有版本号,为了方便追综版本,需要为动态库也添加版本号,这样方便代码的版本控制,那么如何操作了,只需要在Qt的工程文件中(.pro)添加这个字段即可VERSION = 5.5.0但这样创建的动态库会在生成的动态库名称上把主版本也加上去,如mydemo5.dll,那么如何创建mydemo.dll的动态库并带版本号呢,其实添加VERSION后会生成资源文件mydemo_resource.rc,在这个资源文件里改版本号即可,如果添加了这个资源文件RC_FILE += mydemo_resource.rc,这个时候就不用再添加VERSION这个字段了,具体操作代码如下:
按这个步骤,填写相关名字,生成动态库工程
添加代码,mydemo.pro工程文件
QT -= gui
TARGET = mydemo #动态库名称
TEMPLATE = lib
win32: CONFIG += c++11
else: macos:CONFIG += c++11 staticlib
DESTDIR = ../bin
#VERSION = 5.5.0 #生成mydemo_resource.rc文件,但这样生成的动态库会加上主版本号,即mydemo5.dll
DEFINES += MYDEMO_LIBRARY
#应用程序调用动态库方式 LIBS += -L$$DESTDIR -lmydemo
win32{ # windows 支持
#动态库的名称由mydemo_resource.rc文件内容控制
RC_FILE += mydemo_resource.rc
}
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
mydemo.cpp
HEADERS += \
mydemo_global.h \
mydemo.h
# Default rules for deployment.
unix {
target.path = /usr/lib
}
!isEmpty(target.path): INSTALLS += target
mydemo_resource.rc文件
#include <windows.h>
VS_VERSION_INFO VERSIONINFO
FILEVERSION 5,5,0,3518
PRODUCTVERSION 5,5,0,3518
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS VS_FF_DEBUG
#else
FILEFLAGS 0x0L
#endif
FILEOS VOS__WINDOWS32
FILETYPE VFT_DLL
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904b0"
BEGIN
VALUE "CompanyName", "\0"
VALUE "FileDescription", "\0"
VALUE "FileVersion", "5.5.0.3518\0"
VALUE "LegalCopyright", "\0"
VALUE "OriginalFilename", "mydemo.dll\0"
VALUE "ProductName", "mydemo\0"
VALUE "ProductVersion", "5.5.0.3518\0"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x0409, 1200
END
END
/* End of Version info */
mydemo_global.h文件
#ifndef MYDEMO_GLOBAL_H
#define MYDEMO_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(MYDEMO_LIBRARY)
# define MYDEMO_EXPORT Q_DECL_EXPORT
#else
# define MYDEMO_EXPORT Q_DECL_IMPORT
#endif
#endif // MYDEMO_GLOBAL_H
mydemo.h文件
#ifndef MYDEMO_H
#define MYDEMO_H
#include "mydemo_global.h"
#include <QObject>
#include <QDebug>
#include <QString>
class MYDEMO_EXPORT Mydemo
{
//Q_OBJECT
public:
Mydemo();
QString getBuildCpuArchitecture();
QString getCurrentCpuArchitecture();
QString getKernelType();
QString getKernelVersion();
QString getMachineHostName();
QString getPrettyProductName();
QString getProductType();
QString getProductVersion();
};
#endif // MYDEMO_H
mydemo.cpp文件
#include "mydemo.h"
#include <QSysInfo>
Mydemo::Mydemo()
{
}
QString Mydemo::getBuildCpuArchitecture()
{
return QSysInfo::buildCpuArchitecture();
}
QString Mydemo::getCurrentCpuArchitecture()
{
return QSysInfo::currentCpuArchitecture();
}
QString Mydemo::getKernelType()
{
return QSysInfo::kernelType();
}
QString Mydemo::getKernelVersion()
{
return QSysInfo::kernelVersion();
}
QString Mydemo::getMachineHostName()
{
return QSysInfo::machineHostName();
}
QString Mydemo::getPrettyProductName()
{
return QSysInfo::prettyProductName();
}
QString Mydemo::getProductType()
{
return QSysInfo::productType();
}
QString Mydemo::getProductVersion()
{
return QSysInfo::productVersion();
}
测试工程QtCallQtCustomDll.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
DESTDIR = ../bin
VERSION = 5.5.0
LIBS += -L$$DESTDIR -lmydemo
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
mainwindow.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void slotShowSystemInfo();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "../mydemo/mydemo.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton_showSysInfo, SIGNAL(clicked()), this, SLOT(slotShowSystemInfo()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::slotShowSystemInfo()
{
qDebug() << "slotShowSystemInfo=========================";
Mydemo mydemo;
QString buildCpuArchitecture = mydemo.getBuildCpuArchitecture();
QString currentCpuArchitecture = mydemo.getCurrentCpuArchitecture();
QString kernelType = mydemo.getKernelType();
QString kernelVersion = mydemo.getKernelVersion();
QString machineHostName = mydemo.getMachineHostName();
QString prettyProductName = mydemo.getPrettyProductName();
QString productType = mydemo.getProductType();
QString productVersion = mydemo.getProductVersion();
ui->lineEdit_BuildCpuArchitecture->setText(buildCpuArchitecture);
ui->lineEdit_CurrentCpuArchitecture->setText(currentCpuArchitecture);
ui->lineEdit_KernelType->setText(kernelType);
ui->lineEdit_KernelVersion->setText(kernelVersion);
ui->lineEdit_MachineHostName->setText(machineHostName);
ui->lineEdit_PrettyProductName->setText(prettyProductName);
ui->lineEdit_ProductType->setText(productType);
ui->lineEdit_ProductVersion->setText(productVersion);
}
main.cpp文件
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
测试结果: