[原创]Real210 交叉编译sqlite到ARM支持Qt开发

原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: [原创]Real210 交叉编译sqlite到ARM支持Qt开发

环境:
编译链:arm-linux-gcc version 4.4.1
busybox版本:busybox-1.15.1
tslib版本:tslib-1.4
sqlite版本:sqlite-autoconf-3070900
QTE版本:qt-everywhere-opensource-src-4.7.3
Linux发行版:Fedora Core release 6 (Zod)

1:下载源码,解压并配置

./configure --host=arm-linux --prefix=/usr/local/sqlite-arm/

2:编译以及安装

make; make install

3:拷贝/usr/local/下的sqlite-arm目录到开发板的某个路径,比如本人的是:

cp -r sqlite-arm/ /usr/src/minifs/opt/QtEmbedded-4.7.3-arm/

注:/usr/src/minifs/为开发板的根目录,本人采用nfs挂载方式
4:对开发板上文件系统增加sqlite的环境变量:

#库文件路径/opt/QtEmbedded-4.7.3-arm/sqlite-arm/lib
export LD_LIBRARY_PATH=/opt/tslib/lib:/opt/QtEmbedded-4.7.3-arm/lib:/opt/QtEmbedded-4.7.3-arm/sqlite-arm/lib

#sqlite可执行程序路径/opt/QtEmbedded-4.7.3-arm/sqlite-arm/bin
export PATH=/opt:/opt/injector-gui2:/opt/QtEmbedded-4.7.3-arm/sqlite-arm/bin:$PATH

5:启动开发板,执行:sqlite命令,可以看到:

[real@/]# sqlite3 
SQLite version 3.7.9 2011-11-01 00:52:41
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>

测试Sqlite,一个可以直接通过C代码,一个可以通过开发板上Qt程序来测试.

下面为C代码测试:

===============================================================

6:交叉编译完sqlite3,进入安装目录sqlite3_arm的lib目录底下,内容如下
libsqlite3.a libsqlite3.so libsqlite3.so.0.8.6 libsqlite3.la libsqlite3.so.0 pkgconfig

7:把libsqlite3.a, libsqlite3.so拷贝到自己的应用程序目录下testdb,同时拷贝sqlite3_arm的include目录底下的sqlite3.h到testdb目录下

8:编写testdb.c文件,程序内容如下:

#include <stdlib.h> 
#include <stdio.h> 
#include "sqlite3.h"

int main(int argc, char **argv) 
{ 
sqlite3 *db; 
char *zErrMsg = 0; 
int rc;

rc = sqlite3_open("ypfdb", &db); 

if(rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db); 
} else {
printf("You Have Already Successfully!\n");
} 
sqlite3_close(db); 

return 0; 
}

9:编译执行程序
编译程序:
1):如果是动态编译,那么执行如下命令:

arm-linux-gcc testdb.c -L./ -lsqlite3 -o testdb

2)如果是静态编译,那么执行如下命令:

arm-linux-gcc testdb.c -L./ -static -lsqlite3 -lpthread -ldl -o testdb

下面为Qt代码测试:

===============================================================

10:如果在Qt中使用的话,那么pro文件需要加上如下编译参数:

LIBS +=-L./ -lsqlite3

代码如下:

#include <QtGui/QApplication>
#include "mainwindow.h"
#include <stdlib.h>
#include <stdio.h>
#include "sqlite3.h"


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;

    rc = sqlite3_open("ypfdb", &db);

    if(rc) {
            fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
            sqlite3_close(db);
    } else {
            printf("You Have Already Successfully!\n");
    }
    sqlite3_close(db);

    return 0;
}

在Qtcreator中选择Qte的编译链,然后进行编译,具体测试跟C代码一样.

附tips:
编译参数讲解:
我们用gcc编译程序时,可能会用到”-I”(大写i),”-L”(大写l),”-l”(小写l)等参数,下面做个记录:
例:
gcc -o hello hello.c -I /home/hello/include -L /home/hello/lib -lworld

上面这句表示在编译hello.c时:
-I /home/hello/include表示将/home/hello/include目录作为第一个寻找头文件的目录,寻找的顺序是:/home/hello/include-->/usr/include-->/usr/local/include
-L /home/hello/lib表示将/home/hello/lib目录作为第一个寻找库文件的目录,寻找的顺序是:/home/hello/lib-->/lib-->/usr/lib-->/usr/local/lib
-lworld表示在上面的lib的路径中寻找libworld.so动态库文件(如果gcc编译选项中加入了”-static”表示寻找libworld.a静态库文件)

执行程序:

[real@/testdb]# ./testdb
You Have Already Successfully!
[real@/testdb]# ls
Makefile libsqlite3.a sqlite3.h testdb.c testdb.pro
a.out libsqlite3.so testdb testdb.o ypfdb
[real@/testdb]#

执行后可以发现出现了ypfdb这个数据库文件,说明成功了.
静态连接可能报错如下:

[root@localhost testdb]# arm-linux-gcc testdb.c -L./ -lsqlite3 -static -o testdb
.//libsqlite3.a(sqlite3.o): In function `unixDlSym':
/root/sqlite-autoconf-3070900/sqlite3.c:29926: undefined reference to `dlsym'
.//libsqlite3.a(sqlite3.o): In function `pthreadMutexLeave':
/root/sqlite-autoconf-3070900/sqlite3.c:17807: undefined reference to `pthread_mutex_unlock'
.//libsqlite3.a(sqlite3.o): In function `pthreadMutexTry':
/root/sqlite-autoconf-3070900/sqlite3.c:17769: undefined reference to `pthread_mutex_trylock'
.//libsqlite3.a(sqlite3.o): In function `pthreadMutexEnter':
/root/sqlite-autoconf-3070900/sqlite3.c:17723: undefined reference to `pthread_mutex_lock'
.//libsqlite3.a(sqlite3.o): In function `pthreadMutexFree':
/root/sqlite-autoconf-3070900/sqlite3.c:17680: undefined reference to `pthread_mutex_destroy'
.//libsqlite3.a(sqlite3.o): In function `pthreadMutexAlloc':
/root/sqlite-autoconf-3070900/sqlite3.c:17654: undefined reference to `pthread_mutex_init'
/root/sqlite-autoconf-3070900/sqlite3.c:17637: undefined reference to `pthread_mutexattr_init'
/root/sqlite-autoconf-3070900/sqlite3.c:17638: undefined reference to `pthread_mutexattr_settype'
/root/sqlite-autoconf-3070900/sqlite3.c:17639: undefined reference to `pthread_mutex_init'
/root/sqlite-autoconf-3070900/sqlite3.c:17640: undefined reference to `pthread_mutexattr_destroy'
.//libsqlite3.a(sqlite3.o): In function `unixDlClose':
/root/sqlite-autoconf-3070900/sqlite3.c:29930: undefined reference to `dlclose'
.//libsqlite3.a(sqlite3.o): In function `unixDlError':
/root/sqlite-autoconf-3070900/sqlite3.c:29899: undefined reference to `dlerror'
.//libsqlite3.a(sqlite3.o): In function `unixDlOpen':
/root/sqlite-autoconf-3070900/sqlite3.c:29885: undefined reference to `dlopen'
collect2: ld returned 1 exit status

解决办法:增加编译参数-lpthread -ldl即可解决.

详细请参考:http://www.jyguagua.com/?p=965

 

原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: [原创]Real210 交叉编译sqlite到ARM支持Qt开发

文章的脚注信息由WordPress的wp-posturl插件自动生成



|2|left
打赏

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: