通过自定义 qmake 函数包含文件

Include files via custom qmake function

我是 qmake 的新手,我正在尝试项目结构。 我现在将我的项目结构化为

./src/
   logic/
   ui/
   controller/
   etc...
./inc/
   logic/
   ui/
   controller/
   etc...

我想创建一个相应地正确包含新 *.h 和 *.cpp 文件的函数,所以我做到了:

cont = "controller"
logic = "logic"
ui = "ui"

defineReplace(myFunction) {
    path = $
    name = $
    HEADERS *= ./inc/$${path}/$${name}.h
    SOURCES *= ./src/$${path}/$${name}.cpp
}

myFunction(cont,file1)

我预计结果就像我刚刚输入的那样:

HEADERS *= ./inc/controller/file1.h
SOURCES *= ./src/controller/file1.cpp

但我刚收到 myFunction is not a recognized test function

我做错了什么?

What am I doing wrong?

Qmake 区分了 "replace"-函数(即返回一个字符串,就像 make 中的变量替换;通常用于赋值的 rhs)和 "test"-函数(返回一个布尔值适用于条件运算符)。

myFunction(cont, file)是一个测试函数的调用; $$myFunction(cont, file) 是替换函数的调用。

另请注意,Qmake 文件基本上由赋值和条件组成。因此,myFunction(cont, file) 被解释为

myFunction(cont, file) {
    # nothing
} else {
    # nothing
}

另一个问题是 Qmake 中的函数使用自己的私有变量副本,因此您必须使用 export() 让您的更改在外部可见。因此,我们有:

# replace function example code
defineReplace(myFunction) {
    HEADERS *= ./inc/$/$.h
    SOURCES *= ./src/$/$.cpp
    export(HEADERS)
    export(SOURCES)
    # warning: conditional must expand to exactly one word
    #return()
    # warning: conditional must expand to exactly one word
    #return(foo bar)
    # ok: any word will do as we don't care for true/false evaluation
    return(baz)
}

# test function example code
defineTest(myFunction) {
    HEADERS *= ./inc/$/$.h
    SOURCES *= ./src/$/$.cpp
    export(HEADERS)
    export(SOURCES)
    # warning: unexpected return value
    #return(foo)
    # ok: returns true
    #return(true)
    # ok: also returns true
    #return()
    # ...or simply return true by default
}

# calling replace function
$$myFunction(cont, file)
# calling test function
myFunction(cont, file)