Qt笔记-生成静态库和动态库

动态库

写.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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑山老妖的笔记本

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值