动态库
写.pro文件:
# 不使用qt库
CONFIG -= qt
# 项目类型是类库,在Windows下生dll/lib,在linux下生So/a;
TEMPLATE = lib
# 定义DLLDEMO_LIBRARY宏;
DEFINES += DLLDEMO_LIBRARY
# 使用C++11;
CONFIG += c++11
# 项目中包含的.cpp文件
SOURCES += \
dlldemo.cpp
# 项目中包含的.h文件
HEADERS += \
dllDemo_global.h \
dlldemo.h
# 使用linux库,条件编译,unix条件
# Default rules for deployment.
unix {
target.path = /usr/lib
}
# 使用Windows库;
win32 {
}
!isEmpty(target.path): INSTALLS += target
dllDemo_global.h内容:
#if defined(_MSC_VER) || defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
# define Q_DECL_EXPORT __declspec(dllexport)
# define Q_DECL_IMPORT __declspec(dllimport)
#else
# define Q_DECL_EXPORT __attribute__((visibility("default")))
# define Q_DECL_IMPORT __attribute__((visibility("default")))
#endif
#if defined(DLLDEMO_LIBRARY)
# define DLLDEMO_EXPORT Q_DECL_EXPORT
#else
# define DLLDEMO_EXPORT Q_DECL_IMPORT
#endif
或者
#pragma once
#ifdef WIN32
# if defined DLLDEMO_LIBRARY
# define DLLDEMO_EXPORT __declspec(dllexport)
# else
# define DLLDEMO_EXPORT __declspec(dllimport)
# endif
#else
# define DLLDEMO_EXPORT
dlldemo.h内容:
#include "dllDemo_global.h"
class DLLDEMO_EXPORT DllDemo
{
public:
DllDemo();
};
静态库
1.使用Qt编写静态库与动态库大部分是相同的,唯一的不同是多了一条语句:CONFIG += staticlib;
表示自己是一个生产静态库的项目;
CONFIG -= qt
TEMPLATE = lib
CONFIG += staticlib
CONFIG += c++11
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
dll_static_print.cpp
HEADERS += \
dll_static_print.h
# Default rules for deployment.
unix {
target.path = $$[QT_INSTALL_PLUGINS]/generic
}
!isEmpty(target.path): INSTALLS += target
文中代码所在链接:Qt构建动态库静态库.rar