#
# Project to compile QCustomPlot as shared library (.so/.dll) from the amalgamated sources
#
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
greaterThan(QT_MAJOR_VERSION, 4): CONFIG += c++11
lessThan(QT_MAJOR_VERSION, 5): QMAKE_CXXFLAGS += -std=c++11
DEFINES += QCUSTOMPLOT_COMPILE_LIBRARY
TEMPLATE = lib
CONFIG += debug_and_release build_all
static {
CONFIG += static
} else {
CONFIG += shared
}
VERSION = 2.1.1
TARGET = qcustomplot
CONFIG(debug, debug|release) {
TARGET = $$join(TARGET,,,d) # if compiling in debug mode, append a "d" to the library name
QMAKE_TARGET_PRODUCT = "QCustomPlot (debug mode)"
QMAKE_TARGET_DESCRIPTION = "Plotting library for Qt (debug mode)"
} else {
QMAKE_TARGET_PRODUCT = "QCustomPlot"
QMAKE_TARGET_DESCRIPTION = "Plotting library for Qt"
}
QMAKE_TARGET_COMPANY = "www.qcustomplot.com"
QMAKE_TARGET_COPYRIGHT = "Copyright (C) by Emanuel Eichhammer"
SOURCES += ../../qcustomplot.cpp
HEADERS += ../../qcustomplot.h
QT += core gui
- 指定项目依赖的Qt模块。这里添加了
core
和gui
模块,因为QCustomPlot
需要基本的Qt
核心功能和图形用户界面功能。
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
- 如果Qt的主版本号大于4(即Qt5或更高版本),则添加
widgets
和printsupport
模块。因为QCustomPlot使用了Qt的Widgets模块(用于绘图控件)和PrintSupport模块(用于打印支持)。
greaterThan(QT_MAJOR_VERSION, 4): CONFIG += c++11
lessThan(QT_MAJOR_VERSION, 5): QMAKE_CXXFLAGS += -std=c++11
- 如果Qt版本大于4(即Qt5及以上),则配置项目使用C++11标准。
- 如果Qt版本小于5(即Qt4),则通过编译器标志
-std=c++11
启用C++11支持。注意:这要求编译器支持C++11。-
DEFINES += QCUSTOMPLOT_COMPILE_LIBRARY
- 定义一个预处理器宏
QCUSTOMPLOT_COMPILE_LIBRARY
。这个宏在QCustomPlot
的源代码中用于控制导出符号(如使用QCUSTOMPLOT_EXPORT
宏),以便将类导出为库的公共API。
TEMPLATE = lib
设置项目模板为lib
,表示我们要构建一个库(而不是应用程序或其他类型)。
6 .
CONFIG += debug_and_release build_all
debug_and_release
:同时生成调试(debug)和发布(release)版本的库。build_all
:确保构建所有配置(即使当前被设置为只构建一种配置)。
static {
CONFIG += static
} else {
CONFIG += shared
}
- 这是一个条件判断:如果外部传递了
static
参数(例如在qmake命令行中指定CONFIG+=static
),则配置为构建静态库(static
);否则,构建共享库(shared
)。
VERSION = 2.1.1
- 设置库的版本号为2.1.1。这个版本号通常用于生成库文件的文件名后缀(例如
libqcustomplot.so.2.1.1
)以及内部版本信息。
TARGET = qcustomplot
- 设置目标库的名称为
qcustomplot
。在Linux上,会生成libqcustomplot.so
;在Windows上,会生成qcustomplot.dll
。
CONFIG(debug, debug|release) {
TARGET = $$join(TARGET,,,d) # if compiling in debug mode, append a "d" to the library name
QMAKE_TARGET_PRODUCT = "QCustomPlot (debug mode)"
QMAKE_TARGET_DESCRIPTION = "Plotting library for Qt (debug mode)"
} else {
QMAKE_TARGET_PRODUCT = "QCustomPlot"
QMAKE_TARGET_DESCRIPTION = "Plotting library for Qt"
}
- 这段代码根据当前构建配置(debug或release)进行设置:
- 如果是debug配置,将目标名称(
TARGET
)后面追加一个字母d
(例如qcustomplotd
),这样调试库和发布库可以共存。同时设置目标产品的名称和描述为带有“debug mode”的字符串。 - 如果是release配置,则使用普通的目标名称,并设置普通的产品名称和描述。
QMAKE_TARGET_PRODUCT
和QMAKE_TARGET_DESCRIPTION
用于在Windows中设置资源文件的属性(如文件描述),在Linux中通常没有作用。
QMAKE_TARGET_COMPANY = "www.qcustomplot.com"
QMAKE_TARGET_COPYRIGHT = "Copyright (C) by Emanuel Eichhammer"
- 设置目标库的公司名称和版权信息。同样,在Windows中这些信息会嵌入到生成的DLL文件中。
SOURCES += ../../qcustomplot.cpp
HEADERS += ../../qcustomplot.h
- 指定项目的源文件和头文件。这里使用了QCustomPlot的合并源文件(amalgamated sources),即所有代码都在一个
.cpp
和一个.h
文件中。
配置总结
问题:
- 为什么生成的dll文件名是qcustomplot2.dll
因为设置了version
VERSION = 2.1.1
TARGET = qcustomplot
qmake会自动将主版本号(2)附加到库文件名上,这是Qt的默认行为。
qmake处理版本号时:
Windows系统:添加主版本号后缀
–TARGET + 主版本号 + .dll → qcustomplot2.dll
– Linux系统:创建带完整版本号的符号链接
libqcustomplot.so → libqcustomplot.so.2 → libqcustomplot.so.2.1.1