You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何强制qmake为QMAKE_POST_LINK生成的未预先存在文件创建INSTALLS对应的安装与卸载规则?

没问题,这事儿有好几种靠谱的解决办法!qmake默认会检查安装目标里的文件是否存在,才会生成对应的install/uninstall规则,但咱们可以绕开这个检查,强制生成规则。下面给你几个实用方案:

方案1:给安装目标加个no_check_exist配置项

这是最直接的方法,qmake的安装目标支持no_check_exist这个配置开关,加上它之后就会跳过文件存在性检查,直接生成规则。你只需要修改test.pro里的insdoc部分:

unix {
    insdoc.path = /tmp
    insdoc.files += test.txt
    # 加上这一行,强制不检查文件是否存在
    insdoc.CONFIG += no_check_exist
}
!isEmpty(insdoc.path)
INSTALLS += insdoc

这样就算test.txt在qmake运行时还没生成,它也会像inspro那样,乖乖生成install_insdoc和uninstall_insdoc规则。

方案2:手动定义install/uninstall目标(更灵活)

如果你需要更精细的控制,比如自定义安装时的操作逻辑,可以直接手动创建目标,再关联到qmake的流程里:

unix {
    insdoc.path = /tmp
    insdoc.files += test.txt

    # 手动写install_insdoc目标的命令
    install_insdoc.target = install_insdoc
    install_insdoc.commands = @test -d $$INSTALL_ROOT$$insdoc.path || mkdir -p $$INSTALL_ROOT$$insdoc.path; $$QINSTALL test.txt $$INSTALL_ROOT$$insdoc.path/test.txt
    install_insdoc.depends = first

    # 手动写uninstall_insdoc目标的命令
    uninstall_insdoc.target = uninstall_insdoc
    uninstall_insdoc.commands = -$$DEL_FILE -r $$INSTALL_ROOT$$insdoc.path/test.txt; -$$DEL_DIR $$INSTALL_ROOT$$insdoc.path/

    # 把自定义目标加入qmake的额外目标列表
    QMAKE_EXTRA_TARGETS += install_insdoc uninstall_insdoc

    # 让默认的install/uninstall命令能触发这些目标,加入INSTALLS
    INSTALLS += insdoc
}

这种方法的好处是你可以完全掌控安装卸载的每一步,比如加一些额外的判断或者清理操作。

方案3:用占位文件骗qmake(简单但有兼容性注意)

还有个取巧的办法:在qmake执行的时候先创建一个空的test.txt占位,这样qmake就会认为文件存在并生成规则,之后在QMAKE_POST_LINK阶段再用实际生成的文件覆盖这个占位:

# qmake执行时先创建空的test.txt占位(unix下用touch)
system(touch test.txt)

unix {
    insdoc.path = /tmp
    insdoc.files += test.txt
}
!isEmpty(insdoc.path)
INSTALLS += insdoc

# 后续在QMAKE_POST_LINK里生成实际的test.txt内容
QMAKE_POST_LINK += echo "actual content" > test.txt;

不过要注意,这个方法的touch是Unix系命令,Windows下得换成type nul > test.txt,如果是跨平台项目,优先选前两个方案更稳妥。


内容的提问来源于stack exchange,提问作者sofname

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 13:28:16