美文网首页
命令行编译qt程序遭遇 No such file or dire

命令行编译qt程序遭遇 No such file or dire

作者: book_02 | 来源:发表于2021-05-28 15:40 被阅读0次

1. 现象

对于一个qt工程,qt-creator运行没有问题。后面为了提高自动化效率,写一个脚本通过命令行编译qt程序,发现编译报错,报错信息如下:

 Cannot open include file: 'type_traits': No such file or directory

2. 原因分析

报错信息直接指明了原因:找不到头文件,说明要用的库的头文件路径不在INCLUDE变量里,所以编译时就找不到。

除了上面的报错,可能还会有如下的报错情况:

  1. 找不到需要的exe,说明PATH变量里没有程序的路径
  2. 找不到各种库文件,说明LIB变量里没有库文件的路径

那为什么qt-creator运行没有问题呢?
因为 qt-creator 获取了一些环境变量如下:

3. 解决方法

解决办法就是自行把需要用到的路径加到相应的变量里。

如下通过python脚本来编译qt程序,把路径加到相应的PATHINCLUDELIB环境变量里,这样编译的时候就能找到相应的文件

## set path  INCLUDE  LIB
sys_path = os.getenv('path')
path_add=sys_path+";D:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin\\amd64"
path_add=path_add+";C:\\Program Files (x86)\\Windows Kits\\10\\bin\\x64;"
path_add=path_add+";D:/programs/Qt/Qt5.14.2/5.14.2/msvc2015_64/bin;"
path_add=path_add+";D:/programs/Qt/Qt5.14.2/Tools/QtCreator/bin;"
os.environ['path']=path_add

include_path = os.getenv('INCLUDE')
include_path = "D:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\INCLUDE;"+str(include_path)+";"
include_path = include_path+"C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.14393.0\\winrt;"
include_path = include_path+"C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.14393.0\\ucrt;"
include_path = include_path+"C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.14393.0\\um;"
include_path = include_path+"C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.14393.0\\shared;"
os.environ['INCLUDE']=include_path

lib_path = os.getenv('LIB')
lib_path = "C:\\Program Files (x86)\\Windows Kits\\10\\lib\\10.0.14393.0\\ucrt\\x64;"+str(lib_path)+";"
lib_path = lib_path+"C:\\Program Files (x86)\\Windows Kits\\NETFXSDK\\4.6.1\\lib\\um\\x64;"
lib_path = lib_path+"C:\\Program Files (x86)\\Windows Kits\\10\\lib\\10.0.14393.0\\um\\x64;"
lib_path = lib_path+"D:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\LIB\\amd64;"
lib_path = lib_path+"D:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\ATLMFC\\LIB\\amd64;"
os.environ['LIB']=lib_path

脚本里的路径添加都是临时生效,脚本执行完就失效了,所以不用担心会破坏系统环境变量。

4. 完整的用python脚本编译qt程序的样例

#!/usr/bin/python
# -*- coding: UTF-8 -*- 

import os
import sys
import shutil

## ====================================================================
## functions

def goto_error():
    os.system("pause")
    sys.exit(1)

def goto_success():
    os.system("pause")
    sys.exit(0)

def print_red(content):    
    print("\033[0;31;40m"+content+"\033[0m")

def delete_folder(folder_name):
    if os.path.exists(folder_name):
        shutil.rmtree(folder_name)
        print('delete ' + folder_name)
    else:
        print(folder_name + ' does not exist, no need to delete')

def delete_file(file_name):
    if os.path.exists(file_name):
        os.remove(file_name)
        print('delete ' + file_name)
    else:
        print(file_name + ' does not exist, no need to delete')

## ====================================================================

## build
delete_folder('build_qt')
delete_file('./release/test.exe')
delete_file('./release/main.obj')

## set path  INCLUDE  LIB
sys_path = os.getenv('path')
path_add=sys_path+";D:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin\\amd64"
path_add=path_add+";C:\\Program Files (x86)\\Windows Kits\\10\\bin\\x64;"
path_add=path_add+";D:/programs/Qt/Qt5.14.2/5.14.2/msvc2015_64/bin;"
path_add=path_add+";D:/programs/Qt/Qt5.14.2/Tools/QtCreator/bin;"
os.environ['path']=path_add

include_path = os.getenv('INCLUDE')
include_path = "D:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\INCLUDE;"+str(include_path)+";"
include_path = include_path+"C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.14393.0\\winrt;"
include_path = include_path+"C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.14393.0\\ucrt;"
include_path = include_path+"C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.14393.0\\um;"
include_path = include_path+"C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.14393.0\\shared;"
os.environ['INCLUDE']=include_path

lib_path = os.getenv('LIB')
lib_path = "C:\\Program Files (x86)\\Windows Kits\\10\\lib\\10.0.14393.0\\ucrt\\x64;"+str(lib_path)+";"
lib_path = lib_path+"C:\\Program Files (x86)\\Windows Kits\\NETFXSDK\\4.6.1\\lib\\um\\x64;"
lib_path = lib_path+"C:\\Program Files (x86)\\Windows Kits\\10\\lib\\10.0.14393.0\\um\\x64;"
lib_path = lib_path+"D:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\LIB\\amd64;"
lib_path = lib_path+"D:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\ATLMFC\\LIB\\amd64;"
os.environ['LIB']=lib_path

## create build_qt directory
os.system('mkdir build_qt')
os.chdir('build_qt')

## qmake and jom
rlt = os.system('qmake.exe ../test.pro -spec win32-msvc')
if (0 != rlt):
    print_red("***** error ***** : script00_build.py failed")
    goto_error()
    
rlt = os.system('jom.exe qmake_all')
if (0 != rlt):
    print_red("***** error ***** : script00_build.py failed")
    goto_error()


rlt = os.system('jom.exe -f Makefile.Release')
if (0 != rlt):
    print_red("***** error ***** : script00_build.py failed")
    goto_error()

os.system("pause")

相关文章

  • 命令行编译qt程序遭遇 No such file or dire

    1. 现象 对于一个qt工程,qt-creator运行没有问题。后面为了提高自动化效率,写一个脚本通过命令行编译q...

  • linux和windows中命令行编译qt程序步骤

    linux和windows中命令行编译qt程序步骤 linux平台命令行编译 1.检查一下qmake版本(可省略)...

  • 第一个Qt程序-HellWorld!

    Qt提供了集成开发环境Qt Createor,但是一开始我并不想使用它,而是选择在命令行窗口编译Qt程序,Qt提供...

  • Qt程序导出

    对于编译好的qt程序可以使用下面的方法导出: 打开Qt 5.8 for Desktop (MinGW 5.3.0 ...

  • "windwos.h":No such file or dire

    笔者在qt项目中遇到编译错误"windwos.h":No such file or directory,通过百度,...

  • qt5应用程序如何交叉编译

    常见问题: 交叉编译Qt程序的原理没理清 Qt的库和Qt的qmake、rcc、moc等工具没有设置好 Qt的版本宿...

  • 2018-09-26

    Qt如何打包一个Qt程序 由于实际需求,我们写好程序好一般是在Qt内部编译运行的,当需要移植到其他电脑时很有可能用...

  • 阿里云centOS安装uwsgi之后测试失败的原因

    问题一: 「报错:open("./python_plugin.so"): No such file or dire...

  • Qt4

    本节目标: 组合Qt功能创建简单GUI应用程序 认识布局、信号和槽的概念 从Hello Qt开始 Qt的编译运行过...

  • GDB常用命令

    1、file载入调试程序,同时加载符号表 2、core-file载入core dump程序映像,gdb命令行参数形...

网友评论

      本文标题:命令行编译qt程序遭遇 No such file or dire

      本文链接:https://www.haomeiwen.com/subject/ygussltx.html