天天看點

QT程式交叉編譯

qte終于移植成功,但是在交叉編譯自己寫的程式是總是提示有錯誤,錯誤如下:In file included from main.cpp:3:

./database.h:3:23: QSqlDatabase: No such file or directory

./database.h:4:20: QSqlQuery: No such file or directory

In file included from main.cpp:3:

./database.h: In function `bool createConnection()':

./database.h:8: error: `QSqlDatabase' was not declared in this scope

./database.h:8: error: expected `;' before "db"

./database.h:9: error: `db' was not declared in this scope

./database.h:8: warning: unused variable 'QSqlDatabase'

make: *** [main.o] 錯誤

提示沒有這兩個檔案或目錄?明明在我的include裡面怎麼會沒有?我想盡一切辦法,又是該邊境變量,又是删除編譯檔案重新編譯,最後還是不行,我一遍一遍的檢視include目錄,最後發現錯誤提示的兩個檔案在include/QtSql/下面是以我就把程式的包含檔案目錄該了一下原來為:#include<QSqlDatabase>,#include<QSqlQuery>改過後為:#include<QtSql/QSqlDatabase>,#include<QtSql/QSqlQuery>就是在原來的基礎上增加一個目錄字首,這樣問題就解決了,這個編譯通過,但是下面又出現了錯誤,錯誤如下:songlist.cpp:6:20: QSqlQuery: No such file or directory

songlist.cpp:12: error: `songlist' has not been declared

songlist.cpp:12: error: ISO C++ forbids declaration of `songlist' with no type

songlist.cpp: In function `int songlist(QWidget*)':

songlist.cpp:12: error: only constructors take base initializers

songlist.cpp:16: error: `ui' was not declared in this scope

songlist.cpp:16: error: invalid use of `this' in non-member function

songlist.cpp:20: error: `model' was not declared in this scope

songlist.cpp:20: error: `QSqlTableModel' has not been declared

songlist.cpp:20: error: invalid use of `this' in non-member function

songlist.cpp:31: error: `model2' was not declared in this scope

songlist.cpp:31: error: `QSqlTableModel' has not been declared

songlist.cpp:31: error: invalid use of `this' in non-member function

songlist.cpp: At global scope:

songlist.cpp:46: error: expected constructor, destructor, or type conversion before '::' token

songlist.cpp:51: error: `songlist' is not a class or namespace

songlist.cpp: In function `void changeEvent(QEvent*)':

songlist.cpp:53: error: cannot call member function `virtual void QWidget::changeEvent(QEvent*)' without object

songlist.cpp:56: error: `ui' was not declared in this scope

songlist.cpp:56: error: invalid use of `this' in non-member function

songlist.cpp:56: warning: unused variable 'ui'

songlist.cpp:63: error: `songlist' is not a class or namespace

songlist.cpp: In function `void on_pushButton_clicked()':

songlist.cpp:65: error: `ui' was not declared in this scope

songlist.cpp:70: error: `model' was not declared in this scope

songlist.cpp:80: error: `model2' was not declared in this scope

songlist.cpp:65: warning: unused variable 'ui'

songlist.cpp:87: error: `songlist' is not a class or namespace

songlist.cpp: In function `void on_pushButton_4_clicked()':

songlist.cpp:89: error: `ui' was not declared in this scope

songlist.cpp:90: error: `model2' was not declared in this scope

songlist.cpp:92: error: `Dir' was not declared in this scope

songlist.cpp:93: error: `prefix' was not declared in this scope

songlist.cpp:94: error: `PATH' was not declared in this scope

songlist.cpp:97: error: `play1' was not declared in this scope

songlist.cpp:97: warning: unused variable 'play1'

songlist.cpp:103: error: `songlist' is not a class or namespace

songlist.cpp: In function `void on_pushButton_3_clicked()':

songlist.cpp:105: error: `ui' was not declared in this scope

songlist.cpp:106: error: `model2' was not declared in this scope

songlist.cpp:105: warning: unused variable 'ui'

songlist.cpp:110: error: `songlist' is not a class or namespace

songlist.cpp: In function `void on_pushButton_2_clicked()':

songlist.cpp:115: error: invalid use of `this' in non-member function

database.h: At global scope:

database.h:7: warning: 'bool createConnection()' defined but not used

make: *** [songlist.o] 錯誤

交叉編譯真是讓人頭大啊!從錯誤開始的地方找,添加頭目錄,發現問題還是沒有解決,于是就檢視songlist.h發現裡面什麼東西都沒了,還提示任何的改動都可能造成檔案内容丢失,于是就重建立立了一個工程,繼續交叉編譯,前面的問題都解決了 ,但是又出現了新的錯誤,錯誤如下:main.o: In function `main':

main.cpp:(.text+0x210): undefined reference to `QSqlQuery::~QSqlQuery()'

main.cpp:(.text+0x220): undefined reference to `QSqlDatabase::~QSqlDatabase()'

main.cpp:(.text+0x794): undefined reference to `QSqlDatabase::addDatabase(QString const&, QString const&)'

main.cpp:(.text+0x858): undefined reference to `QSqlDatabase::~QSqlDatabase()'

main.cpp:(.text+0xbac): undefined reference to `QSqlDatabase::setDatabaseName(QString const&)'

main.cpp:(.text+0xc48): undefined reference to `QSqlDatabase::open()'

main.cpp:(.text+0xcf0): undefined reference to `QSqlQuery::~QSqlQuery()'

main.cpp:(.text+0xd00): undefined reference to `QSqlDatabase::~QSqlDatabase()'

main.cpp:(.text+0xd68): undefined reference to `QSqlDatabase::QSqlDatabase()'

main.cpp:(.text+0xd80): undefined reference to `QSqlQuery::QSqlQuery(QString const&, QSqlDatabase)'

main.cpp:(.text+0xd90): undefined reference to `QSqlDatabase::~QSqlDatabase()'

main.cpp:(.text+0xe98): undefined reference to `QSqlQuery::exec(QString const&)'

main.cpp:(.text+0xef8): undefined reference to `QSqlDatabase::defaultConnection'

main.cpp:(.text+0xf90): undefined reference to `QSqlQuery::exec(QString const&)'

main.cpp:(.text+0x1038): undefined reference to `QSqlQuery::exec(QString const&)'

main.cpp:(.text+0x10e0): undefined reference to `QSqlQuery::exec(QString const&)'

main.cpp:(.text+0x1188): undefined reference to `QSqlQuery::exec(QString const&)'

main.cpp:(.text+0x1230): undefined reference to `QSqlQuery::exec(QString const&)'

main.o:main.cpp:(.text+0x12d8): more undefined references to `QSqlQuery::exec(QString const&)' follow

songlist.o: In function `songlist::songlist(QWidget*)':

songlist.cpp:(.text+0x1ac): undefined reference to `QSqlQuery::~QSqlQuery()'

songlist.cpp:(.text+0x47c): undefined reference to `QSqlDatabase::~QSqlDatabase()'

songlist.cpp:(.text+0x5b0): undefined reference to `QSqlDatabase::QSqlDatabase()'

songlist.cpp:(.text+0x5d8): undefined reference to `QSqlTableModel::QSqlTableModel(QObject*, QSqlDatabase)'

songlist.cpp:(.text+0x5f4): undefined reference to `QSqlDatabase::~QSqlDatabase()'

songlist.cpp:(.text+0x7c0): undefined reference to `QSqlDatabase::QSqlDatabase()'

songlist.cpp:(.text+0x7e8): undefined reference to `QSqlTableModel::QSqlTableModel(QObject*, QSqlDatabase)'

songlist.cpp:(.text+0x804): undefined reference to `QSqlDatabase::~QSqlDatabase()'

songlist.cpp:(.text+0x8cc): undefined reference to `QSqlQueryModel::query() const'

songlist.cpp:(.text+0x9d0): undefined reference to `QSqlQuery::~QSqlQuery()'

songlist.cpp:(.text+0xbb8): undefined reference to `QSqlQuery::~QSqlQuery()'

songlist.cpp:(.text+0xe88): undefined reference to `QSqlDatabase::~QSqlDatabase()'

songlist.cpp:(.text+0xfbc): undefined reference to `QSqlDatabase::QSqlDatabase()'

songlist.cpp:(.text+0xfe4): undefined reference to `QSqlTableModel::QSqlTableModel(QObject*, QSqlDatabase)'

songlist.cpp:(.text+0x1000): undefined reference to `QSqlDatabase::~QSqlDatabase()'

songlist.cpp:(.text+0x11cc): undefined reference to `QSqlDatabase::QSqlDatabase()'

songlist.cpp:(.text+0x11f4): undefined reference to `QSqlTableModel::QSqlTableModel(QObject*, QSqlDatabase)'

songlist.cpp:(.text+0x1210): undefined reference to `QSqlDatabase::~QSqlDatabase()'

songlist.cpp:(.text+0x12d8): undefined reference to `QSqlQueryModel::query() const'

songlist.cpp:(.text+0x13dc): undefined reference to `QSqlQuery::~QSqlQuery()'

songlist.o: In function `songlist::on_pushButton_clicked()':

songlist.cpp:(.text+0x2f38): undefined reference to `QSqlTableModel::submitAll()'

songlist.o: In function `songlist::on_pushButton_4_clicked()':

songlist.cpp:(.text+0x43d0): undefined reference to `QSqlTableModel::submitAll()'

songlist.o: In function `songlist::on_pushButton_3_clicked()':

songlist.cpp:(.text+0x47b8): undefined reference to `QSqlTableModel::submitAll()'

collect2: ld returned 1 exit status

make: *** [SKTV] 錯誤

這樣錯誤在交叉編譯QTE時,出現了一個類似的,但是我重新解壓再編譯時卻沒有了 ,我在網上也沒找到類似的問題,是以我隻好再重新編譯了,相同的錯誤還是出現,先靜下心來想想,一夜沒有睡好!

終于解決了,問題終于解決了。問題原因:由于交叉編譯是使用qte的qmake -project手動建立的工程檔案,無法自動判斷是否包含資料庫,是以我們如果使用了資料庫既要手動在工程檔案的最後添加一句話,QT +=sql這樣問題就迎刃而解了,困擾了我好幾天的錯誤,也是最關鍵的錯誤,如果無法解決,整個程式将無法編譯和移植!啊,松了一口氣!