Qter 发表于 2018-2-23 23:00:31

Qt Creator 工程创建

本帖最后由 Qter 于 2018-2-24 00:47 编辑

打开Qt Creator
1.创建解决方案
文件->新建文件或项目->其它项目->子目录项按向导(选择相关路径) ... 完成


即只生成一个 DoraemonSolution.pro文件内容为:TEMPLATE = subdirs

2.创建主界面程序
右键 DoraemonSolution 新建子项目->Application->Qt Widgets Application
根据向导填写项目名称为 Doraemon

3.添加静态库QCommLib

右键 DoraemonSolution 新建子项目->Library->C++库


依赖库默认 QtCore
注意最后一步 默认做为子项目加入,如下:



4.Doraemon中添加对静态库QCommLib的依赖


右键 Doraemon ->添加库-> 内部库




下一步后,将在Doraemon.pro文件添加如下代码
win32:CONFIG(release, debug|release): LIBS += -L$OUT_PWD/../QCommLib/release/ -lQCommLib
else:win32:CONFIG(debug, debug|release): LIBS += -L$OUT_PWD/../QCommLib/debug/ -lQCommLib
else:unix: LIBS += -L$OUT_PWD/../QCommLib/ -lQCommLib

INCLUDEPATH += $PWD/../QCommLib
DEPENDPATH += $PWD/../QCommLib

win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $OUT_PWD/../QCommLib/release/libQCommLib.a调用时直接包含相关库中的头文件(如:#include "qcommlib.h" ),就可以调用了


5.添加动态库QControlSo

右键 DoraemonSolution 新建子项目->Library->C++库




共享 库 多了文件 qcontrolso_global.h#ifndef QCONTROLSO_GLOBAL_H
#define QCONTROLSO_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(QCONTROLSO_LIBRARY)
#define QCONTROLSOSHARED_EXPORT Q_DECL_EXPORT
#else
#define QCONTROLSOSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // QCONTROLSO_GLOBAL_H
宏的相关定义,如下:#ifdef Q_OS_WIN
#    define Q_DECL_EXPORT   __declspec(dllexport)
#    define Q_DECL_IMPORT   __declspec(dllimport)
#elif defined(QT_VISIBILITY_AVAILABLE)
#    define Q_DECL_EXPORT   __attribute__((visibility("default")))
#    define Q_DECL_IMPORT   __attribute__((visibility("default")))
#    define Q_DECL_HIDDEN   __attribute__((visibility("hidden")))
#endif6.Doraemon中添加对共享库QControlSo的依赖
右键 Doraemon ->添加库-> 内部库

下一步后,将在Doraemon.pro文件添加如下代码win32:CONFIG(release, debug|release): LIBS += -L$OUT_PWD/../QControlSo/release/ -lQControlSo
else:win32:CONFIG(debug, debug|release): LIBS += -L$OUT_PWD/../QControlSo/debug/ -lQControlSo
else:unix: LIBS += -L$OUT_PWD/../QControlSo/ -lQControlSo

INCLUDEPATH += $PWD/../QControlSo
DEPENDPATH += $PWD/../QControlSo

Qter 发表于 2018-2-24 00:32:17

QT坑人一大亮点:如果你在构建过程中出现问题,你重新修改后确定没有错误的前提下,再运行可能还是相同的错误,那么你需要

清除----------重新执行qmake------重新构建
页: [1]
查看完整版本: Qt Creator 工程创建