【Qt C++】fatal error LNK1112: module machine type ‘x64‘ conflicts with target machine type ‘X86‘

【Qt C++】fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86',以及解决在VS Code中使用qmake编译项目的问题


使用Vscode + qmake + jom 编译Qt项目,发现在QtCreator里编译地好好的,但是vscode里设置的脚本死活报错:

Qt5Widgets.lib(Qt5Widgets.dll) : fatal error LNK1112: module machine
type ‘x64’ conflicts with target machine type ‘X86’ jom:
D:\Projects\2023.9.19
AfterService\AfterServiceSoft\build\Makefile.Release
[release\AfterServiceSoft.exe] Error 1112

我开始还以为是Task.json写错了,但是检查了很多遍都看不出问题在哪里。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "mkdir",
            "type": "shell",
            "options": {"cwd": "${workspaceFolder}"},
            "command": "mkdir -Force build"
            "problemMatcher": ["$msCompile"]
        },
        {
            "label": "qmake-release",
            "type": "shell",
            "options": {"cwd": "${workspaceFolder}/build"},
            "command": "qmake ${workspaceFolder}/${workspaceFolderBasename}.pro -spec win32-msvc \"CONFIG+=qtquickcompiler\""
            ],
            "problemMatcher": ["$msCompile"]
        },
        {
            "label": "make-release",
            "type": "shell",
            "options": {"cwd": "${workspaceFolder}/build"},
            "command": "jom -f  Makefile.Release"
            ],
            "dependsOn": ["qmake-release"],
            "problemMatcher": ["$msCompile"]
        },
        {
            "label": "run-release",
            "type": "process",
            "options": {"cwd": "${workspaceFolder}/build"},
            "command": "${workspaceFolderBasename}.exe",
            "dependsOn": ["make-release"]
        },
        {
            "label": "clean",
            "type": "shell",
            "options": {"cwd": "${workspaceFolder}/build"},
            "command": "jom clean"
            ],
            "problemMatcher": ["$msCompile"]
        }
    ]
}

大概花了2天时间,做了诸多尝试

尝试(1):修改qtConfigure.vcvarsallPath的值

查找别人的博客才知道得先运行“x64 Native Tools Command Prompt for VS 2022”配置x64的工具链编译环境,否则cl.exe默认都是以x86的库在链接程序。我检查了下VS Code的setting.json文件发现,qtConfigure.vcvarsallPath这个参数就是用来调整编译器的版本的。原先默认生成的是vs2015的32位编译器。
在这里插入图片描述
因此原以为改成2022的64位编译器就可以。
在这里插入图片描述
相当于Qt Creator里配置这玩意:
在这里插入图片描述
但是仍然没有解决

尝试(2):尝试主动调用vsvars64.bat

我在cmd窗口下打开vsvars64.bat,并在其中主动运行jom -f Makefile.Release,没有这个问题。也就是说,要在脚本运行且不退出的情况下才有效。因此我首先把vsvars64.bat所在路径加入到环境变量中,然后在Task.json中做如下修改。

"label": "make-release",
"type": "shell",
"options": {"cwd": "${workspaceFolder}/build"},
"command": "vsvars64.bat ;jom -f  Makefile.Release" //因为VSC里的命令行执行多条命令得把&&换成;

发现VsCode里确实有打印,64位环境的配置过程,紧接着执行了jom,但最终还是报LNK1112错误,相当于可能配置后没有生效?
在这里插入图片描述
没有解决

尝试(3):换写法实现尝试(2)

经过诸多尝试,参考Developer Command Prompt集成到VScodeVS CODE可以配置用vc++编译和调试吗?等文章,最终的写法如下

 "label": "make-release",
 "type": "shell",
 "options": {
     "cwd": "${workspaceFolder}/build"
 },
 "command": "cmd.exe /k 'D:\\Programs\\VS2022\\VC\\Auxiliary\\Build\\vcvars64.bat 1>nul 2>&1 && jom -f Makefile.Release && EXIT'" ,
 "problemMatcher": ["$msCompile"]

原来vsvars64是要通过cmd /k保存环境后才调用完后仍然有效,且在VSCode里命令最好用’'而不是""。
已经解决

最后贴一下Task.json的完整配置

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "mkdir",
            "type": "shell",
            "options": {"cwd": "${workspaceFolder}"},
            "command": "mkdir -Force build",
            "problemMatcher": ["$msCompile"]
        },
        {
            "label": "qmake-release",
            "type": "shell",
            "options": {"cwd": "${workspaceFolder}/build"},
            "command": "qmake '${workspaceFolder}/${workspaceFolderBasename}.pro' -spec win32-msvc \"CONFIG+=qtquickcompiler\"",
            "problemMatcher": ["$msCompile"]
        },
        {
            "label": "make-release",
            "type": "shell",
            "options": {"cwd": "${workspaceFolder}/build"},
            "command": "cmd.exe /k 'vcvars64.bat 1>nul 2>&1 && jom -f Makefile.Release && EXIT'",
            "dependsOn": ["qmake-release"],
            "problemMatcher": ["$msCompile"]
        },
        {
            "label": "run-release",
            "type": "process",
            "options": {"cwd": "${workspaceFolder}/build/release"},
            "command": "${workspaceFolderBasename}.exe",
            "dependsOn": ["make-release"],
            "problemMatcher": ["$msCompile"]
        },
        {
            "label": "clean",
            "type": "shell",
            "options": {"cwd": "${workspaceFolder}/build"},
            "command": "jom clean",
            "problemMatcher": ["$msCompile"]
        }
    ]
}
  • 24
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值