QT CreatorL 从另一个(父)目录添加源文件

QT CreatorL Adding source files from another (parent) directory

我的Qt工程结构类似这样:

目录结构:

  |
  |--- dir
  |      |
  |      | - a.c
  |      | - a.h
  |      | - test.pro
  |--- dir1
  |      | - b.c
  |      | - b.h

test.pro

SOURCES +=  a.c \
            ../dir1/*.c
HEADERS +=  a.h \
            ../dir1/*.h

当我尝试构建项目时出现错误:

:-1: error: No rule to make target `../dir1/*.c'

是否可以包含 .pro 文件之外的源文件?

并且还让它们显示在 Qt Creator 左侧的“项目”窗格中?

qmake(.pro 文件)中的通配符仅适用于当前项目目录中的文件。对于子文件夹,它不起作用。所以正确的解决方法是分别添加每个文件.

问题是在 Qt 错误跟踪器 QTCREATORBUG-8925 上提出的。作为新功能请求或由于多个问题,勾选已关闭:

Using wildcards in .pro files creates multiple problems, e.g. adding a additional file won't automatically compile it. Nor would deleting a file automatically remove it from the Makefile

但是,wiki 上列出了未记录的函数 Undocumented_QMake

files(glob) — Returns a list of files which match the specified glob pattern.

因此,如果可以接受上述使用 globbing 模式的问题,则可以将其用作

SOURCES += $$files(../dir1/*.c)