【问题标题】:Qbs custom module not workingQbs自定义模块不起作用
【发布时间】:2025-12-29 03:20:09
【问题描述】:

我想制作一个模块来使用 QtRO repc 编译器从 .rep 文件生成 .h 文件。

我对模块进行了编码,但是当我尝试将其加载到应用程序产品中时,它不会加载并禁用该产品。

模块位于 C:\Users\User\qt\qbs

Qbs 模块副本.qbs:

import qbs

Module {
    property bool source: true
    FileTagger {
        patterns: "*.rep"
        fileTags: ["rep"]
    }
    Rule {
       inputs: ["rep"]
       Artifact {
           fileTags: ["txt_output"]
       }
       prepare: {
           var cmd = new Command();
           cmd.program = "repc.exe";
           if source {
               cmd.arguments = ["-i", "rep", "-o", "source", input.filePath];
           } else {
               cmd.arguments = ["-i", "rep", "-o", "replica", input.filePath];
           }
           console.log("repc on : ", input.filePath);
           return [cmd];
       }
    }
}

product.qbs:

import qbs

Application {
    name: "ServiceExposer"
    Depends { name: "cpp" }
    Depends { name: "Qt.core" }
    Depends { name: "Qt.remoteobjects" }
    Depends { name: "replica" }
    files: [
        "main.cpp",
        "service_exposer.rep"
    ]
}

project.qbs:

import qbs

Project {
    references: ["ServiceExposer/ServiceExposer.qbs"]
    qbsSearchPaths: "C:\Users\User\qt\qbs"
}

我不知道我在哪里犯了错误。

提前感谢您的帮助。

【问题讨论】:

    标签: qt qbs


    【解决方案1】:
    1. 如果是头文件,为什么要给它“cpp”标签?不应该是“hpp”吗?
    2. 您将文件放入源目录的原因是什么?您是否打算将其添加到您的存储库中?通常,构建工件(无论是二进制文件还是人类可读文件)都应位于构建目录中,以免“污染”源代码树。
    3. 您没有提到该模块现在对您不起作用的原因,因此很难诊断问题。您应该提及您预期会发生什么以及发生了什么(给出具体的错误消息,如果有的话)。

    【讨论】:

    • @Chritian Kandeler - 我不知道 hpp 标签存在,我把它放在源目录中,这样我就可以从其他类中引用它。工作版本在我对同一篇文章的回答中。当它不起作用时,qtcreator 只需禁用该项目,不要告诉我我的模块有什么问题。
    • @Chritian Kandeler - 我不想把它放在新帖子中,什么是多路复用规则?这是什么意思 ?当qbs告诉你规则有冲突并且规则不是多路复用时是什么意思。
    • 我确实修复了它,这是 moc 正在查找具有 .h 扩展名的文件,这些文件是相同的(一个由构建目录中的 repc 生成,而我在源目录中复制的一个),我不得不修改我的模块以删除构建目录中的那个。
    【解决方案2】:

    在对文档和源代码进行了更多挖掘之后,我设法使其工作,我与您分享工作模块。

    如果您的项目中有任何.rep文件(QtRO(远程对象)模块远程对象定义)导入此模块,它将调用repc编译器并编译它们并将生成的.h文件放在您的源目录中.

    还没有完成,我没有找到一种方法来操作产品项的文件属性以自动将 .h 添加到它。

    import qbs
    import qbs.FileInfo
    
    Module {
        FileTagger {
            patterns: ["*.rep"]
            fileTags: ["repc-rep"]
        }
        Rule {
            inputs: ["repc-rep"]
            Artifact {
                filePath: repc_" + FileInfo.baseName(input.fileName) + "_source.h"
                fileTags: ["cpp"]
            }
            prepare: {
                var cmd = new Command();
                cmd.description = "repc " + input.fileName;
                cmd.program = "repc.exe"
                cmd.arguments = ["-i", "rep", "-o", "source", input.filePath, output.filePath];
                var cmd2 = new JavaScriptCommand();
                cmd2.silent = true;
                cmd2.sourceCode = function() {
                     File.copy(output.filePath, FileInfo.path(input.filePath) + "/" + output.fileName);
                }
                return [cmd, cmd2];
            }
        }
    }
    

    为了让这个模块工作,repc.exe 必须在你的路径中。

    欢迎提出任何建议。

    【讨论】: