[SOLVED] Dynamic Build Works; Static Build Fails
-
I am sorry if this is an obvious issue that I'm missing, but I can't seem to figure it out...
My project builds and runs perfectly in a dynamic (ie DLL-dependant) installation of Qt 4.8.1, but I would like to deploy it statically. I followed a guide (http://www.formortals.com/how-to-statically-link-qt-4/) on another computer to be sure of not messing up my development installation, using the MinGW compiler on Windows 7, and the configure command:
@configure -release -nomake examples -nomake demos -no-exceptions -no-stl -no-rtti -no-qt3support -no-scripttools -no-openssl -no-opengl -no-webkit -no-phonon -no-style-motif -no-style-cde -no-style-cleanlooks -no-style-plastique -platform win32-g++ -static -qt-sql-sqlite -script@
If I build my dynamic code as-is, no errors are produced; of course, some functionality is missing, as no plugins have been loaded.Continuing the guide, I added the following to the very top of my main.cpp file:
@#include <QtPlugin>
Q_IMPORT_PLUGIN(qsqlite)
Q_IMPORT_PLUGIN(qsvg)
Q_IMPORT_PLUGIN(qtaccessiblewidgets)@and the following to my .pro file:
@CONFIG += static
static {
CONFIG += static
QTPLUGIN += qsqlite qsvg qtaccessiblewidgets
DEFINES += static
message("Static build.")
}win32 {
QMAKE_LFLAGS += -static-libgcc -static-libstdc++
}@(the win32 section is included as I have previously been able to build the project statically, but required these lines to remove some pesky DLL dependencies. It is after changing some code in studywindow.cpp that I could no longer build the project)
As-is, QtCreator displays a red line under both Q_IMPORT_PLUGIN(svg) and my first other #include in main.cpp, claiming "expected a declaration". Similar red lines with the same message appear in the blank line immediately after Q_OBJECT in all of my class header files, save studywindow.h. If I try to build this, having followed the above procedure that worked in the past, I get the following output:
@In file included from ........\Qt\include/QtCore/qglobal.h:1:0,
from ........\Qt\include/QtCore/../../src/corelib/global/qnamespace.h:45,
from ........\Qt\include/QtCore/qnamespace.h:1,
from ........\Qt\include/QtCore/../../src/corelib/kernel/qobjectdefs.h:45,
from ........\Qt\include/QtCore/qobjectdefs.h:1,
from ........\Qt\include/QtGui/../../src/gui/kernel/qwindowdefs.h:45,
from ........\Qt\include/QtGui/qwindowdefs.h:1,
from ........\Qt\include/QtGui/../../src/gui/kernel/qwidget.h:45,
from ........\Qt\include/QtGui/qwidget.h:1,
from ........\Qt\include\QtGui/../../src/gui/widgets/qmainwindow.h:45,
from ........\Qt\include\QtGui/qmainwindow.h:1,
from ........\Qt\include\QtGui/QMainWindow:1,
from studywindow.h:4,
from studywindow.cpp:1:
........\Qt\include/QtCore/../../src/corelib/global/qglobal.h:1588:5: error: expected unqualified-id before numeric constant
........\Qt\include/QtCore/../../src/corelib/global/qglobal.h:1589:5: error: expected unqualified-id before numeric constant
........\Qt\include/QtCore/../../src/corelib/global/qglobal.h:1993:18: error: expected unqualified-id before numeric constant
........\Qt\include/QtCore/../../src/corelib/global/qglobal.h:1998:18: error: expected unqualified-id before numeric constant
........\Qt\include/QtCore/../../src/corelib/global/qglobal.h:2006:18: error: expected unqualified-id before numeric constant
........\Qt\include/QtCore/../../src/corelib/global/qglobal.h:2014:18: error: expected unqualified-id before numeric constant
........\Qt\include/QtCore/../../src/corelib/global/qglobal.h:2024:1: error: expected unqualified-id before numeric constant
In file included from c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/new:42:0,
from ........\Qt\include/QtCore/../../src/corelib/tools/qlist.h:58,
from ........\Qt\include/QtCore/qlist.h:1,
from ........\Qt\include/QtCore/../../src/corelib/kernel/qobject.h:50,
from ........\Qt\include/QtCore/qobject.h:1,
from ........\Qt\include/QtGui/../../src/gui/kernel/qwidget.h:46,
from ........\Qt\include/QtGui/qwidget.h:1,
from ........\Qt\include\QtGui/../../src/gui/widgets/qmainwindow.h:45,
from ........\Qt\include\QtGui/qmainwindow.h:1,
from ........\Qt\include\QtGui/QMainWindow:1,
from studywindow.h:4,
from studywindow.cpp:1:
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/exception:37:37: error: expected declaration before end of line
mingw32-make[1]: *** [release/studywindow.o] Error 1
mingw32-make: *** [release] Error 2@I noted that part of the include chain in these errors was QMainWindow, included from my class header file, studywindow.h, and changing the order of #includes changes the error to the first #include in the file that links back to qglobal.h.
Commenting out all of the extra code for the static build removes the errors. If I uncomment the extra .pro code, leaving the Q_IMPORT_PLUGIN block in main.cpp commented out, I can build the application and run it normally, albeit still without essential plugins loaded. If I uncomment the Q_IMPORT_PLUGIN block, and re-comment the extra .pro code, the only errors I get are in the vein of
@undefined reference to `qt_plugin_instance_qsqlite()'@for each of the plugins I have loaded, which makes sense, seeing as there are no plugins being loaded by the .pro file. Bizzarely, if I then uncomment everything, the only error I get is a linker error:
@cannot find -lqsqlite@ -
[continued; I exceeded the character limit]
I am completely at a loss as to why I get the 8 above errors with both parts uncommented to start with, and get a completely different error after some jiggery-pokery with commenting and un-commenting; I am confident that I followed exactly the same process as I did before when I successfully built the project statically. Online searches suggested that re-defining #defines used in header files could cause such errors, but, having skimmed qglobal.h, I am confident I haven't re-used any variable names.
I would be extremely appreciative of any help to solve this problem; I'm stumped.
Thank you in advance.main.cpp
@#include <QtPlugin>
Q_IMPORT_PLUGIN(qsqlite)
Q_IMPORT_PLUGIN(qsvg)
Q_IMPORT_PLUGIN(qtaccessiblewidgets)#include <QApplication>
#include "homewindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);HomeWindow *window = new HomeWindow(); window->show(); return app.exec();
}
@.pro
@OTHER_FILES +=
...HEADERS +=
...SOURCES +=
...FORMS +=
...QT +=
sql
network
svg
scriptCONFIG += static
static {
CONFIG += static
QTPLUGIN += qsqlite qsvg qtaccessiblewidgets
DEFINES += static
message("Static build.")
}win32 {
QMAKE_LFLAGS += -static-libgcc -static-libstdc++
}@studywindow.h
@#ifndef STUDYWINDOW_H
#define STUDYWINDOW_H#include <QMainWindow>
#include "dbfieldenums.h"
#include <QSqlRelationalTableModel>
#include <QSqlRecord>
#include <QTime>#include <QNetworkAccessManager>
#include <QNetworkReply>#include <QSvgWidget>
namespace Ui {
class StudyWindow;
}class StudyWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit StudyWindow(QSqlRelationalTableModel *newItems, QSqlRelationalTableModel *reviewItems, QSqlRelationalTableModel *pNativeGenders, QSqlRelationalTableModel *pTargetGenders, quint16 userReactionInterval, quint16 userCharacterInterval, English pEnglish,
QWidget *pCaller = 0, QWidget *parent = 0);
~StudyWindow();protected:
...private:
...private slots:
...signals:
...
};#endif // STUDYWINDOW_H
@EDIT:
I've since been able to fix my problem, which was actually two:The compiler errors were because my .pro file contained the line "DEFINES += static", which should have been "DEFINES += STATIC".
The linker error was because my configure command (-qt-sql-sqlite) had built the qsqlite library into Qt - to use it as a plugin, I should have used -plugin-sql-sqlite.
I think the seemingly random ever-changing nature of my errors was because I re-built the project in QtCreator without cleaning the build first.
I hope this helps anybody else in a similar situation.