歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> Linux下sqlite數據庫移植全攻略

Linux下sqlite數據庫移植全攻略

日期:2017/2/27 9:25:05   编辑:更多Linux
  本文討論的是比較流行的嵌入式開發組合ARM+UCLinux,即目標開發板為三星S3C4510,完成sqlite在其uclinux上的移植。  本文假設你已經具備正確編譯uclinux的kernel的能力,即有能力完成make menuconfig;makedep;makelib_only;make user_only;makeromfs;makeimage;make。而且還能將自己寫的類似helloworld程序加到“用戶自定義應用程序”中,即你能完成“uClinux-dist/Documentation/Adding-User-Apps-HOWTO”中所描述的“用戶程序的訂制”。  大多數需要移植sqlite到uclinux的開發者,應該已經具備上面的能力,而只是不清楚如何修改sqlite來完成其在uclinux下的編譯。如果你還不能完成上面的要求,那麼請先做一定的准備工作,因為本范例所涉及到的內容主要是跟sqlite在uclinux下的移植有關,其他的在這個過程中出現的問題,開發者需要自行處理。  本范例使用的uclinux是uClinux-dist-20030522.tar.gz,你可以從http://www.uclinux.org得到適合你的軟件包。  交叉編譯工具是arm-elf-tools-20030314.sh,你也可以在http://www.uclinux.org找到它。  本范例使用的sqlite是sqlite-2.8.15.tar.gz,本文的方法也適合於2.8.x系列的sqlite;可能有部分內容不適用於3.0.x系列的sqlite,因為3.0.x中源代碼有較大的變化。  1、下載sqlite:你可以到http://www.sqlite.org/download.Html,下載sqlite-2.8.15.tar.gz軟件包;  2、 將下載的軟件包解壓縮到uClinux-dist/user目錄下;  命令:  $tar zxvf sqlite-2.8.15.tar.gz -C uClinux-dist/user/  現在在uclinux的user目錄下,你應該可以看到sqlite目錄了。解壓縮到這個user目錄主要是要將sqlite編譯成一個普通的用戶應用程序。  3、 用戶應用程序的有關設置:  按uClinux-dist/Documentation/Adding-User-Apps-HOWTO文檔中說提到的,來添加sqlite作為一個用戶應用程序,將其做成一個shell,這樣就類似於uclinux自己的ps命令。  編輯文件  uClinux-dist/user/Makefile  uClinux-dist/config/Configure.help  uClinux-dist/config/config.in  我是在這些文件裡查找“cpu”有關的項,然後在它的下面,加上自己的sqlite項,這個過程並不復雜。  通過上面的修改後,你現在就可以運行uclinux的makemenuconfig,選中“CustomizeVendor/UserSettings”,再選中“MiscellaneousApplications”,可以看到它現在出現了一個新的“sqlite(NEW)”,這個就是我們剛添加進去的sqlite項。  在稍後的makeromfs中,uclinux會將你的sqlite編譯進來,做成romfs的一部分,因為你在uClinux-dist/user/Makefile中已經加上要編譯sqlite項了。這樣在移植後的uclinux的/bin中將會有sqlite命令可以讓你來執行。  好,現在我們就要對sqlite進行修改,來做移植工作。  在下面的描述中,我們將對以下幾個文件進行一定的添加、修改,從而來完成sqlite在uclinux下的編譯:  sqlite/main.mk 修改  sqlite/Makefile 添加  sqlite/src/os.c 修改  sqlite/src/shell.c 修改  對這幾個文件進行修改時,請自己做好這些文件的備份,比如你可以將它們拷貝一份,改名成文件名後面帶.bak。這個很重要,可以避免你在修改的過程出現問題而無法還原。  一、修改sqlite/main.mk  1、TCCX  將  TCCX = $(TCC) $(OPTS) $(THREADSAFE) $(USLEEP) -I.-I$(TOP)/src  修改為  TCCX = $(TCC) $(OPTS) $(THREADSAFE) $(USLEEP) -I.-I$(TOP)/src$(CFLAGS)  即加上$(CFLAGS)標記。  2、 LIBOBJ  找到 # Object files for the SQLite library.  將其中的tclsqlite.o去掉。即去掉tcl有關的東西。  如果沒有tclsqlite.o,那麼不用處理它。  3、 sqlite$(EXE)  找到類似sqlite$(EXE)的一句,將:  sqlite$(EXE): $(TOP)/src/shell.c libsqlite.a sqlite.h  $(TCCX) $(READLINE_FLAGS) -o sqlite$(EXE) $(TOP)/src/shell.c\\\\\\\\  libsqlite.a $(LIBREADLINE) $(THREADLIB)  替換為:  shell.o: $(TOP)/src/shell.c sqlite.h  $(TCCX) $(READLINE_FLAGS) -c $(TOP)/src/shell.c  sqlite$(EXE): shell.o libsqlite.a  $(TCC) $(LDFLAGS) -o $@ shell.o \\\\\\\\  libsqlite.a $(LIBREADLINE) $(THREADLIB) $(LDLIBS)  即在sqlite$(EXE)上一行加上shell.o,及在其後加上$(LDLIBS)標記。這個是對/src/shell.c的編譯方法的修改。  4、romfs  將:  install: sqlite libsqlite.a sqlite.h  mv sqlite /usr/bin  mv libsqlite.a /usr/lib  mv sqlite.h /usr/include  替換為:  romfs: sqlite  $(ROMFSINST) /bin/sqlite  即去掉make install項,加上makeromfs項。這個很重要,這將在romfs的/bin目錄下生成sqlite。  5、clean  將:  clean:  rm -f *.o sqlite libsqlite.a sqlite.h opcodes.*  rm -f lemon lempar.c parse.* sqlite*.tar.gz  rm -f $(PUBLISH)  rm -f *.da *.bb *.bbg gmon.out  rm -rf tsrc替換為:  clean:  rm -f *.o sqlite libsqlite.a sqlite.h opcodes.* sqlite.gdb  rm -f $(PUBLISH)  rm -f *.da *.bb *.bbg gmon.out  rm -rf tsrc  distclean: clean  rm -f lemon lempar.c parse.* sqlite*.tar.gz  rm -f config.h  即增加make distclean項。熱門推薦: 談超頻的概念及方法 2004年十大熱門顯卡超頻王  1 2 3   二、在sqlite下增加Makefile文件  在sqlite目錄下應該沒有Makefile文件,而只是有一個sqlite/Makefile.linux-gcc文件。我們要移植sqlite到uclinux,那麼就要自己寫一個合適的Makefile。  內容如下:  ===========Makefile內容開始===========  #!/usr/make  #  # Makefile for SQLITE  #  # This is a template makefile for SQLite. Most peoplepreferto  # use the autoconf generated configure script togeneratethe  # makefile automatically. But that does not work foreverybody  # and in every situation. If you are having problems with the  # configure script, you might want to try this makefileasan  # alternative. Create a copy of this file, edit theparameters  # below and type make.  #  #### The toplevel Directory of the source tree. This isthedirectory  # that contains this Makefile.in and theconfigure.inscript.  #  TOP = .  #### C Compiler and options for use in buildingexecutablesthat  # will run on the platform that is doing the build.  #  BCC = gcc -g -O2  #BCC = /opt/ancic/bin/c89 -0  #### If the target operating system supports theusleep()system  # call, then define the HAVE_USLEEP macro for all C modules.  #  #USLEEP =  USLEEP = -DHAVE_USLEEP=1  #### If you want the SQLite library to be safe for usewithina  # multi-threaded program, then define the following macro  # appropriately:  #  #THREADSAFE = -DTHREADSAFE=1  THREADSAFE = -DTHREADSAFE=0  #### Specify any extra linker options needed to makethelibrary  # thread safe  #  #THREADLIB = -lpthread  THREADLIB =  #### Leave MEMORY_DEBUG undefined for maximum speed.UseMEMORY_DEBUG=1  # to check for memory leaks. Use MEMORY_DEBUG=2 to print a logofall  # malloc()s and free()s in order to track down memory leaks.  #  # SQLite uses some eXPensive assert() statements in theinnerloop.  # You can make the library go almost twice as fast ifyoucompile  # with -DNDEBUG=1  #  #OPTS = -DMEMORY_DEBUG=2  #OPTS = -DMEMORY_DEBUG=1  #OPTS = -DNDEBUG=1  OPTS = -DMEMORY_DEBUG=1  #### The suffix to add to executable files. .exeforwindows.  # Nothing for unix.  #  #EXE = .exe  EXE =  #### C Compile and options for use in buildingexecutablesthat  # will run on the target platform. This is usually the same  # as BCC, unless you are cross-compiling.  #  TCC = $(CROSS)gcc  FLTFLAGS += -s 12000  #TCC = gcc -g -O0 -Wall  #TCC = gcc -g -O0 -Wall -fprofile-arcs -ftest-coverage  #TCC = /opt/mingw/bin/i386-mingw32-gcc -O6  #TCC = /opt/ansic/bin/c89 -O +z -Wl,-a,archive  #### Tools used to build a static library.  #  AR = $(CROSS)ar cr  #AR = /opt/mingw/bin/i386-mingw32-ar cr  RANLIB = $(CROSS)ranlib  #RANLIB = /opt/mingw/bin/i386-mingw32-ranlib  #### Extra compiler options needed for programs that use theTCLlibrary.  #  #TCL_FLAGS =  #TCL_FLAGS = -DSTATIC_BUILD=1  #TCL_FLAGS = -I/home/drh/tcltk/8.4linux  #TCL_FLAGS = -I/home/drh/tcltk/8.4win -DSTATIC_BUILD=1  #TCL_FLAGS = -I/home/drh/tcltk/8.3hpux  #### Linker options needed to link against the TCL library.  #  #LIBTCL = -ltcl -lm -ldl  #LIBTCL = /home/drh/tcltk/8.4linux/libtcl8.4g.a -lm -ldl  #LIBTCL = /home/drh/tcltk/8.4win/libtcl84s.a -lmsvcrt  #LIBTCL = /home/drh/tcltk/8.3hpux/libtcl8.3.a -ldld -lm -lc  #### Compiler options needed for programs that use thereadline()library.  #  READLINE_FLAGS =  #READLINE_FLAGS = -DHAVE_READLINE=1 -I/usr/include/readline  #### Linker options needed by programs using readline() mustlinkagainst.  #  #LIBREADLINE =  #LIBREADLINE = -static -lreadline -ltermcap  #### Should the database engine assume text is coded as UTF-8oriso8859?  #  # ENCODING = UTF8  ENCODING = ISO8859  # You should not have to change anything below this line  ###############################################################################  include $(TOP)/main.mk  ===========Makefile內容結束===========  注:  1、 在uclinux下的sqlite的Makefile將不去用到TCL相關的庫。  2、 在uclinux下的sqlite的Makefile將不去用到readline()。  在sqlite/README中有關於Makefile的一段描述:  The configure script uses autoconf 2.50 and libtool. Iftheconfigure script does not work out for you, there is agenericmakefile named Makefile.linux-gcc in the top directory ofthesource tree that you can copy and edit to suite yourneeds.Comments on the generic makefile show what changes areneeded.  你可以用sqlite/Makefile.linux-gcc作為藍本來修改適合你自己的Makefile。  你如果有興趣的話,可以把上面的Makefile的內容和sqlite/Makefile.linux-gcc內容diff對比一下,看看uclinux下的sqlite編譯有哪些不同的地方。  三、修改sqlite/src/os.c  如果你的sqlite包中包括os.c文件那麼就對其進行修改,沒有os.c文件可能是你的sqlite版本比較新,那麼無須修改。  將所有你找到的:  用:  替換。熱門推薦: 談超頻的概念及方法 2004年十大熱門顯卡超頻王  1 2 3




  四、修改sqlite/src/shell.c  1、struct previous_mode_data 結構定義項:  將 int colWidth100;  用 int colWidth20;  替換。  2、struct callback_data 結構定義項  將:  int colWidth100;  int actualWidth100;  char outfileFILENAME_MAX;  用:  int colWidth20;  int actualWidth20;  char *outfilep;  對應替換。  再在結構下面增加:  #ifndef FILENAME_MAX  #define FILENAME_MAX 4095  #endif  char outfilenameFILENAME_MAX; /* Filename for *out */  即  struct callback_data  ...  ;  #ifndef FILENAME_MAX  #define FILENAME_MAX 4095  #endif  char outfilenameFILENAME_MAX; /* Filename for *out */  3、函數do_meta_command(...)  找到類似這樣的一句:  sqlite_exec(p->db, PRAGMA database_list; ,callback,&data, &zErrMsg);  在它的前面有一句  memcpy(&data, p, sizeof(data));  現在在memcpy下面增加一行  data.cnt = 0;  即將結構中cnt的值賦為0 ;  現在代碼會被修改成類似:  open_db(p);  memcpy(&data, p, sizeof(data));  data.cnt = 0;  再繼續。  找到類似這樣的一句:  strcmp(azArg1,stdout)==0  在它的下面的括號中:  將 strcpy(p->outfile,stdout);  用 p->outfilep = stdout;  來替換。  再在它下面的5-6行處  將:  strcpy(p->outfile,azArg1);  用:  strcpy(outfilename,azArg1);  p->outfilep = outfilename;  替換。  再繼續,找到類似這樣的一句:  fprintf(p->out,%9.9s: %s\\\\\\\\n,output,  將:  fprintf(p->out,%9.9s: %s\\\\\\\\n,output, strlen(p->outfile)?p->outfile : stdout);  用:  fprintf(p->out,%9.9s: %s\\\\\\\\n,output,p->outfilep&& strlen(p->outfilep) ? p->outfilep :stdout);  替換。  完成修改。  上面的所有的對sqlite的修改完成後,你就可以make dep;make lib_only;makeuser_only;makeromfs;make image了。  如果你對sqlite的修改,在make user_only過程中出現錯誤的話,你可以忽略makedep;makelib_only命令,直接再次進行make user_only;makeromfs;makeimage;就可以了,而不用重復make dep;make lib_only。  makeimage會幫你生成romfs文件系統。現在在uClinux-dist/images下面就有編譯生成的romfs文件系統了。這個就是我們需要的包含有sqlite的romfs了。  在上面的過程中,你可以不用在“makeimage”後再去“make”生成kernel內核,因為你只需要生成romfs就可以了,它裡面已經有sqlite了。  現在你就可以把你生成的含有sqlite應用程序的romfs下載到開發板上運行一下。  Welcome to  ____ _ _  / ___  _ __ ____ _ _ _ _  _ \\\\\\\\ \\\\\\\\ \\\\\\\\/ /  _ __ _ / \\\\\\\\  ___\\\\\\\\____ __ _ _ \\\\\\\\____ \\\\\\\\_/\\\\\\\\_/  _  GDB/ARMulator support by  For further information check:  http://www.uclinux.org/  Command: /bin/ifconfig eth0 up 10.0.0.2  Execution Finished, Exiting  init: Booting to single user mode  Sash command shell (version 1.1.1)  /> cd bin  /bin> ls -l sqlite  -rwxr-xr-x 1 0 0 327072 Jan 01 00:00 sqlite  /bin >cd /tmp  /tmp>sqlite test.sqlite  sqlite> create table my(name varchar(80), num smallint);  sqlite> insert into my values(yutao, 100);  sqlite> insert into my values(uclinux, 99);  sqlite> select * from my;  yutao 100  uclinux 99  sqlite> .tables  my  sqlite> .schema  create table my(name varchar(80), num smallint);  sqlite> .q  /tmp>ls Cl test.sqlite  你要保證你的/tmp是可寫的目錄。  好,現在你的sqlite就已經在uclinux運行起來了,感覺如何呀,在uclinux也可以玩玩“select*from”,感覺很爽吧。





Copyright © Linux教程網 All Rights Reserved