本文长期连接
Windows编译静态版Qt5.9.2过程记录,转载说明出处
最近在学习Qt,因为想写个小工具,从官网下载的肯定不行,Qt官网下载的都是动态库。好好的写完代码后,发现exe不能在外面运行(那还有个毛用?)网上找了下资料,说是要编译静态版本的。于是开始了持续好几天的静态版本探索。
首先是下载Qt的源码。
qt-everywhere-opensource-src-5.9.2.zip
如图:
下载好后如图:
解压之后如图:
Qt编译需要ActivePerl 下载一个
ActivePerl下载地址
我下载的是5.28版本的。如图
下载好的文件如图:
安装一下。
安装过程会自动添加的环境变量。基本上是一路next就行。试一下成功没有,如图:。
perl -v
很显然成功了。
然后装个python,这个我估计基本上大部分程序或者敲代码的哥哥姐姐都装了,添加到环境变量,这个过程不赘述。试一下成功没有。
我的是因为之前要装cocos,所以版本比较低。
接下来还要装个Ruby,
下载后如图:
安装后也要头癣添加到环境变量。默认是勾选了的。所以基本上是一路Next.
接下来是编译环境选择
在Windows上,有两个预构建环境可供选择:一个是MinGW,另一个是Microsoft Visual Studio(MSVC)。这两个环境不兼容,无法混合。你必须选择一个。
这两者的区别如下:
当你的项目使用MinGW编译的使用,想要用一个MSVC编译生成的库时就会有问题。使用MinGW编译项目的时候,所使用的Lib也要是MinGW编译的。如果你只是开发Window平台的软件时,最好用Qt MSVC组合,这样可以使用大量的第三方lib,还有很多的构建指令,毕竟window上MSVC才是王道。
我选择MSVC,打开安装VS时自带安装的MSCV:VS 2017的开发人员命令提示符
好,开始进入编译阶段。
首先是修改源码也就是D:\QT\qt-everywhere-opensource-src-5.9.2\qtbase\mkspecs\common\msvc-desktop.conf这个文件,看上面的截图就知道,我的源码是放在D:\QT里面的。如图。
这里原来是
改的内容就是把 -MD改成-MT,和-MDd改成-MTd
这点官网其实有说的。
保存文件后,需要修改用刚才打开的vs开发人员命令符工具cd到安装目录。如图:
接下来需要执行
configure -static -prefix "D:\QT\QtStatic" -confirm-license -opensource -debug-and-release -platform win32-msvc -nomake examples -nomake tests -plugin-sql-sqlite -plugin-sql-odbc -qt-zlib -qt-libpng -qt-libjpeg -opengl desktop -mp
其中-prefix "D:\QT\QtStatic"是表示最终会编译到该目录。
大意如下:
如果你是vs2017,那么会报个错误,错误忘记截图了。原因是vs2017编译的一个bug。你需要做的是替换一个.h头文件内容,文件位置如下:
目录是:D:\QT\qt-everywhere-opensource-src-5.9.2\qtbase\src\corelib\tools 你看到了吧,正好是Qt源码目录。
文件修改后的内容是如下,你直接替换就行。不同版本的Qt也一样。
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QALGORITHMS_H
#define QALGORITHMS_H
#include <QtCore/qglobal.h>
#if defined(Q_CC_MSVC) && _MSC_VER > 1500
#include <intrin.h>
#endif
QT_BEGIN_NAMESPACE
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
/*
Warning: The contents of QAlgorithmsPrivate is not a part of the public Qt API
and may be changed from version to version or even be completely removed.
*/
namespace QAlgorithmsPrivate {
#if QT_DEPRECATED_SINCE(5, 2)
template <typename RandomAccessIterator, typename T, typename LessThan>
QT_DEPRECATED_X("Use std::sort") Q_OUTOFLINE_TEMPLATE void qSortHelper(RandomAccessIterator start, RandomAccessIterator end, const T &t, LessThan lessThan);
template <typename RandomAccessIterator, typename T>
QT_DEPRECATED_X("Use std::sort") inline void qSortHelper(RandomAccessIterator begin, RandomAccessIterator end, const T &dummy);
template <typename RandomAccessIterator, typename T, typename LessThan>
QT_DEPRECATED_X("Use std::stable_sort") Q_OUTOFLINE_TEMPLATE void qStableSortHelper(RandomAccessIterator start, RandomAccessIterator end, const T &t, LessThan lessThan);
template <typename RandomAccessIterator, typename T>
QT_DEPRECATED_X("Use std::stable_sort") inline void qStableSortHelper(RandomAccessIterator, RandomAccessIterator, const T &);
template <typename RandomAccessIterator, typename T, typename LessThan>
QT_DEPRECATED_X("Use std::lower_bound") Q_OUTOFLINE_TEMPLATE RandomAccessIterator qLowerBoundHelper(RandomAccessIterator begin, RandomAccessIterator end, const T &value, LessThan lessThan);
template <typename RandomAccessIterator, typename T, typename LessThan>
QT_DEPRECATED_X("Use std::upper_bound") Q_OUTOFLINE_TEMPLATE RandomAccessIterator qUpperBoundHelper(RandomAccessIterator begin, RandomAccessIterator end, const T &value, LessThan lessThan);
template <typename RandomAccessIterator, typename T, typename LessThan>
QT_DEPRECATED_X("Use std::binary_search") Q_OUTOFLINE_TEMPLATE RandomAccessIterator qBinaryFindHelper(RandomAccessIterator begin, RandomAccessIterator end, const T &value, LessThan lessThan);
#endif // QT_DEPRECATED_SINCE(5, 2)
}
#if QT_DEPRECATED_SINCE(5, 2)
template <typename InputIterator, typename OutputIterator>
QT_DEPRECATED_X("Use std::copy") inline OutputIterator qCopy(InputIterator begin, InputIterator end, OutputIterator dest)
{
while (begin != end)
*dest++ = *begin++;
return dest;
}
template <typename BiIterator1, typename BiIterator2>
QT_DEPRECATED_X("Use std::copy_backward") inline BiIterator2 qCopyBackward(BiIterator1 begin, BiIterator1 end, BiIterator2 dest)
{
while (begin != end)
*--dest = *--end;
return dest;
}
template <typename InputIterator1, typename InputIterator2>
QT_DEPRECATED_X("Use std::equal") inline bool qEqual(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
{
for (; first1 != last1; ++first1, ++first2)
if (!(*first1 == *first2))
return false;
return true;
}
template <typename ForwardIterator, typename T>
QT_DEPRECATED_X("Use std::fill") inline void qFill(ForwardIterator first, ForwardIterator last, const T &val)
{
for (; first != last; ++first)
*first = val;
}
template <typename Container, typename T>
QT_DEPRECATED_X("Use std::fill") inline void qFill(Container &container, const T &val)
{
qFill(container.begin(), container.end(), val);
}
template <typename InputIterator, typename T>
QT_DEPRECATED_X("Use std::find") inline InputIterator qFind(InputIterator first, InputIterator last, const T &val)
{
while (first != last && !(*first == val))
++first;
return first;
}
template <typename Container, typename T>
QT_DEPRECATED_X("Use std::find") inline typename Container::const_iterator qFind(const Container &container, const T &val)
{
return qFind(container.constBegin(), container.constEnd(), val);
}
template <typename InputIterator, typename T, typename Size>
QT_DEPRECATED_X("Use std::count") inline void qCount(InputIterator first, InputIterator last, const T &value, Size &n)
{
for (; first != last; ++first)
if (*first == value)
++n;
}
template <typename Container, typename T, typename Size>
QT_DEPRECATED_X("Use std::count") inline void qCount(const Container &container, const T &value, Size &n)
{
qCount(container.constBegin(), container.constEnd(), value, n);
}
#ifdef Q_QDOC
typedef void* LessThan;
template <typename T> LessThan qLess();
template <typename T> LessThan qGreater();
#else
template <typename T>
class QT_DEPRECATED_X("Use std::less") qLess
{
public:
inline bool operator()(const T &t1, const T &t2) const
{
return (t1 < t2);
}
};
template <typename T>
class QT_DEPRECATED_X("Use std::greater") qGreater
{
public:
inline bool operator()