解决QT程序自动启动时找不到DLL的问题

最近写了一个自动启动的QT程序,将程序及DLL都放到了同一个目录下。
双击启动,当然没有问题。
但设置为自动启动的时候,就提示找不到需要的DLL了。

最简单的方法,当然就是将DLL放到System32下面或者将DLL放到其他PATH的路径下面。
但我自己的环境已经够复杂了,实在是不想有其他冲突了。

于是,用VS写了一个启动用的Loader。
这个Loader啥都不干,就是先设置PATH,然后将参数传递给QT的EXE,启动之。

QT使用Timer

void MainWindow::initTimer()
{
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(timerRecalc()));
    timer->start(1000*6);
}


void MainWindow::timerRecalc()
{
    for(int i=0;i<eventArray->size();i++)
    {
        NMyEvent myEvent = eventArray->at(i);
        ui->tableMain->setItem(i, 0, new QTableWidgetItem(myEvent.EventName));
        ui->tableMain->setItem(i, 1, new QTableWidgetItem(NMyUtils::formatDate(myEvent.TimeBegin)));
        ui->tableMain->setItem(i, 2, new QTableWidgetItem(NMyUtils::formatDate(myEvent.TimeDue)));
        myEvent.reCalcDaysLeft();
        ui->tableMain->setItem(i, 3, new QTableWidgetItem(myEvent.DaysLeft));
        ui->tableMain->setItem(i, 4, new QTableWidgetItem(myEvent.Comments));
    }
}

QT读取JSON

void MainWindow::readJson(QString fileName)
{
    QString szContent = NMyUtils::readFile(NMyUtils::getFileFullPath(fileName));

    QJsonDocument jDoc = QJsonDocument::fromJson(szContent.toUtf8());
    QJsonObject jObj = jDoc.object();
    QJsonArray jReminderItems = jObj.value(QString("Reminder")).toArray();

    foreach (const QJsonValue & item, jReminderItems)
    {
        QJsonValue eName = (item.toObject())["EventName"];
        QJsonValue tBegin = (item.toObject())["TimeBegin"];
        QJsonValue tDue = (item.toObject())["TimeDue"];
        QJsonValue comments = (item.toObject())["Comments"];
        const NMyEvent *myevent = NMyEvent::newEvent(eName.toString(),tBegin.toString(),tDue.toString(),comments.toString());

        eventArray->append(*myevent);
    }
}

QT实现自动启动(Windows)

QString NMyUtils::getExeFullPathWithArgs()
{
    QString exePath = QCoreApplication::applicationFilePath().replace("/","\\").replace("Reminder.exe","QTLoader.exe");
    QString argPath = "\""+exePath+"\" TrayIcon";
    qDebug()<<argPath;
    return argPath;
}

bool NMyUtils::isAutoStart()
{
    QSettings reg("HKEY_LOCAL_MACHINE\\SOFTWARE\\MICROSOFT\\WINDOWS\\CURRENTVERSION\\RUN", QSettings::NativeFormat);
    QString val = reg.value("NReminder").toString();
    if(val.length()>0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void NMyUtils::setAutoStart()
{
    QSettings reg("HKEY_LOCAL_MACHINE\\SOFTWARE\\MICROSOFT\\WINDOWS\\CURRENTVERSION\\RUN", QSettings::NativeFormat);
    reg.setValue("NReminder",NMyUtils::getExeFullPathWithArgs());
}

void NMyUtils::removeAutoStart()
{
    QSettings reg("HKEY_LOCAL_MACHINE\\SOFTWARE\\MICROSOFT\\WINDOWS\\CURRENTVERSION\\RUN", QSettings::NativeFormat);
    reg.remove("NReminder");
}

QT实现TrayIcon

void MainWindow::initTrayIcon()
{
    trayIcon=new QSystemTrayIcon(this);
    appIcon =new QIcon(NMyUtils::getFileFullPath("Reminder.png"));
    trayIcon->setIcon(*appIcon);
    this->setWindowIcon(*appIcon);

    autoStartAction = new QAction(tr("AutoStart"),this);
    autoStartAction->setCheckable(true);
    if(NMyUtils::isAutoStart())
    {
        autoStartAction->setChecked(true);
    }
    connect(autoStartAction, SIGNAL(triggered()), this, SLOT(autoStart()));
    showHideAction = new QAction(tr("Show"),this);
    showHideAction->setCheckable(true);
    showHideAction->setChecked(true);
    connect(showHideAction, SIGNAL(triggered()), this, SLOT(showHide()));
    quitAction = new QAction(tr("Quit"),this);
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

    trayIconMenu = new QMenu(this);
    trayIconMenu->addAction(autoStartAction);
    trayIconMenu->addAction(showHideAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);
    trayIcon->setContextMenu(trayIconMenu);

    trayIcon->setToolTip(tr("Reminder"));
    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
    trayIcon->show();
}

VS2010编译QT5失败

最近整理了自己电脑上的源码,重新下载并编译了QT5的最新版本。
一开始还都顺利,后来就报错啦:

        cd qml\ && ( if not exist Makefile D:\Build\QT5\QT5.4.1_VC2010\qtbase\bi
n\qmake D:\Build\QT5\QT5.4.1_VC2010\qtdeclarative\src\qml\qml.pro -o Makefile )
&& "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\nmake.exe" -f Mak
efile

Microsoft (R) Program Maintenance Utility Version 10.00.40219.01
Copyright (C) Microsoft Corporation.  All rights reserved.

        "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\nmake.exe" -
f Makefile.Debug

Microsoft (R) Program Maintenance Utility Version 10.00.40219.01
Copyright (C) Microsoft Corporation.  All rights reserved.

        echo 2 /* CREATEPROCESS_MANIFEST_RESOURCE_ID */ 24 /* RT_MANIFEST */ "D:
\\Build\\QT5\\QT5.4.1_VC2010\\qtbase\\lib\\Qt5Qmld.dll.embed.manifest">D:\Build\
QT5\QT5.4.1_VC2010\qtbase\lib\Qt5Qmld.dll_manifest.rc
        if not exist D:\Build\QT5\QT5.4.1_VC2010\qtbase\lib\Qt5Qmld.dll if exist
 D:\Build\QT5\QT5.4.1_VC2010\qtbase\lib\Qt5Qmld.dll.embed.manifest del D:\Build\
QT5\QT5.4.1_VC2010\qtbase\lib\Qt5Qmld.dll.embed.manifest
        if exist D:\Build\QT5\QT5.4.1_VC2010\qtbase\lib\Qt5Qmld.dll.embed.manife
st copy /Y D:\Build\QT5\QT5.4.1_VC2010\qtbase\lib\Qt5Qmld.dll.embed.manifest D:\
Build\QT5\QT5.4.1_VC2010\qtbase\lib\Qt5Qmld.dll_manifest.bak
        link /NOLOGO /DYNAMICBASE /NXCOMPAT /BASE:0x66000000 /DEBUG /DLL /SUBSYS
TEM:WINDOWS /VERSION:5.41 /MANIFEST /MANIFESTFILE:D:\Build\QT5\QT5.4.1_VC2010\qt
base\lib\Qt5Qmld.dll.embed.manifest /OUT:D:\Build\QT5\QT5.4.1_VC2010\qtbase\lib\
Qt5Qmld.dll @C:\Users\Hansen\AppData\Local\Temp\nm86A5.tmp
   Creating library D:\Build\QT5\QT5.4.1_VC2010\qtbase\lib\Qt5Qmld.lib and objec
t D:\Build\QT5\QT5.4.1_VC2010\qtbase\lib\Qt5Qmld.exp
YarrInterpreter.obj : error LNK2019: unresolved external symbol "struct JSC::Yar
r::CharacterClass * __cdecl JSC::Yarr::newlineCreate(void)" (?newlineCreate@Yarr
@JSC@@YAPAUCharacterClass@12@XZ) referenced in function "public: struct JSC::Yar
r::CharacterClass * __thiscall JSC::Yarr::YarrPattern::newlineCharacterClass(voi
d)" (?newlineCharacterClass@YarrPattern@Yarr@JSC@@QAEPAUCharacterClass@23@XZ)
YarrJIT.obj : error LNK2001: unresolved external symbol "struct JSC::Yarr::Chara
cterClass * __cdecl JSC::Yarr::newlineCreate(void)" (?newlineCreate@Yarr@JSC@@YA
PAUCharacterClass@12@XZ)
YarrPattern.obj : error LNK2001: unresolved external symbol "struct JSC::Yarr::C
haracterClass * __cdecl JSC::Yarr::newlineCreate(void)" (?newlineCreate@Yarr@JSC
@@YAPAUCharacterClass@12@XZ)
YarrInterpreter.obj : error LNK2019: unresolved external symbol "struct JSC::Yar
r::CharacterClass * __cdecl JSC::Yarr::wordcharCreate(void)" (?wordcharCreate@Ya
rr@JSC@@YAPAUCharacterClass@12@XZ) referenced in function "public: struct JSC::Y
arr::CharacterClass * __thiscall JSC::Yarr::YarrPattern::wordcharCharacterClass(
void)" (?wordcharCharacterClass@YarrPattern@Yarr@JSC@@QAEPAUCharacterClass@23@XZ
)
YarrJIT.obj : error LNK2001: unresolved external symbol "struct JSC::Yarr::Chara
cterClass * __cdecl JSC::Yarr::wordcharCreate(void)" (?wordcharCreate@Yarr@JSC@@
YAPAUCharacterClass@12@XZ)
YarrPattern.obj : error LNK2001: unresolved external symbol "struct JSC::Yarr::C
haracterClass * __cdecl JSC::Yarr::wordcharCreate(void)" (?wordcharCreate@Yarr@J
SC@@YAPAUCharacterClass@12@XZ)
YarrPattern.obj : error LNK2019: unresolved external symbol "struct JSC::Yarr::C
haracterClass * __cdecl JSC::Yarr::digitsCreate(void)" (?digitsCreate@Yarr@JSC@@
YAPAUCharacterClass@12@XZ) referenced in function "public: struct JSC::Yarr::Cha
racterClass * __thiscall JSC::Yarr::YarrPattern::digitsCharacterClass(void)" (?d
igitsCharacterClass@YarrPattern@Yarr@JSC@@QAEPAUCharacterClass@23@XZ)
YarrPattern.obj : error LNK2019: unresolved external symbol "struct JSC::Yarr::C
haracterClass * __cdecl JSC::Yarr::spacesCreate(void)" (?spacesCreate@Yarr@JSC@@
YAPAUCharacterClass@12@XZ) referenced in function "public: struct JSC::Yarr::Cha
racterClass * __thiscall JSC::Yarr::YarrPattern::spacesCharacterClass(void)" (?s
pacesCharacterClass@YarrPattern@Yarr@JSC@@QAEPAUCharacterClass@23@XZ)
YarrPattern.obj : error LNK2019: unresolved external symbol "struct JSC::Yarr::C
haracterClass * __cdecl JSC::Yarr::nondigitsCreate(void)" (?nondigitsCreate@Yarr
@JSC@@YAPAUCharacterClass@12@XZ) referenced in function "public: struct JSC::Yar
r::CharacterClass * __thiscall JSC::Yarr::YarrPattern::nondigitsCharacterClass(v
oid)" (?nondigitsCharacterClass@YarrPattern@Yarr@JSC@@QAEPAUCharacterClass@23@XZ
)
YarrPattern.obj : error LNK2019: unresolved external symbol "struct JSC::Yarr::C
haracterClass * __cdecl JSC::Yarr::nonspacesCreate(void)" (?nonspacesCreate@Yarr
@JSC@@YAPAUCharacterClass@12@XZ) referenced in function "public: struct JSC::Yar
r::CharacterClass * __thiscall JSC::Yarr::YarrPattern::nonspacesCharacterClass(v
oid)" (?nonspacesCharacterClass@YarrPattern@Yarr@JSC@@QAEPAUCharacterClass@23@XZ
)
YarrPattern.obj : error LNK2019: unresolved external symbol "struct JSC::Yarr::C
haracterClass * __cdecl JSC::Yarr::nonwordcharCreate(void)" (?nonwordcharCreate@
Yarr@JSC@@YAPAUCharacterClass@12@XZ) referenced in function "public: struct JSC:
:Yarr::CharacterClass * __thiscall JSC::Yarr::YarrPattern::nonwordcharCharacterC
lass(void)" (?nonwordcharCharacterClass@YarrPattern@Yarr@JSC@@QAEPAUCharacterCla
ss@23@XZ)
D:\Build\QT5\QT5.4.1_VC2010\qtbase\lib\Qt5Qmld.dll : fatal error LNK1120: 7 unre
solved externals
NMAKE : fatal error U1077: '"c:\Program Files (x86)\Microsoft Visual Studio 10.0
\VC\BIN\link.EXE"' : return code '0x460'
Stop.
NMAKE : fatal error U1077: '"c:\Program Files (x86)\Microsoft Visual Studio 10.0
\VC\BIN\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: 'cd' : return code '0x2'
Stop.
NMAKE : fatal error U1077: 'cd' : return code '0x2'
Stop.
NMAKE : fatal error U1077: 'cd' : return code '0x2'
Stop.

后来发现,是一开始的时候python没有正确的添加到路径之中,导致一个文件生成失败,从而引起的这个错误。
其解决方法如下:

1、查看文件%QT_HOME%\qtdeclarative\src\qmlRegExpJitTables.h,大小应该为0,删除该文件
2、确保可以执行python命令
3、重新nmake

此外,我的QT5编译环境配置文件如下:

@@set mkspecs=win32-msvc2010

@call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"

@set PYTHON_HOME=C:\Languages\Python\Python27
@set PERL_HOME=C:\Languages\Perl5
@set RUBY_HOME=C:\Languages\Ruby\Ruby200x86
@set GNU_HOME=D:\Build\QT5\QT5.4.1_VC2010\gnuwin32
@set PATH=%PATH%;%PYTHON_HOME%;%PERL_HOME%\bin;%RUBY_HOME%\bin;%GNU_HOME%\bin

@set DXSDK_DIR=C:\ProgramerTools\DirectX\Microsoft DirectX 9.0 SDK (Summer 2004)

@color 02
@title QT5+VS2010

@cmd