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

2021-05-28  本文已影响0人  book_02

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")
上一篇 下一篇

猜你喜欢

热点阅读