开发者

Using SQLite with C on Windows

开发者 https://www.devze.com 2023-01-12 19:28 出处:网络
I am trying to develop a project in C (on Dev-Cpp IDE) using SQLite for data storage, on Windows Vista. I am trying to run this code : Sqlite with C

I am trying to develop a project in C (on Dev-Cpp IDE) using SQLite for data storage, on Windows Vista. I am trying to run this code : Sqlite with C

I moved sqlite3.h in the /lib path. But when I am tryi开发者_开发技巧ng to run the program, I get lots of linker errors, saying "undefined reference to sqlite3_open" ,etc.

Google gives me Linux commands, which cant be used here :( Can somone please guide me through the whole process of using sqlite with C , I am very new to such things. Where do I put the sqlite3.exe ??

Thanks a lot !!


I finally figured out my answer through : http://source.online.free.fr/Windows_HowToCompileSQLite

The above link creates a .dll which has to be put in the /bin, and one .a file which has to be passed as an option to the linker !

Thanks David !!


Documentation on SQLite can be had at the project's home page. There is a quick start guide as well.

However, before that information is useful, you need to understand how to use your compiler and linker to build a program that uses external libraries. From the question you are asking, some of this isn't yet clear, so perhaps a review is in order:

The compiler (which is the MinGW release of GCC from the Dev-Cpp IDE distribution) converts source text conventionally found in .c and .h files into object files which contain machine language with values for some symbols left unspecified. The linker (Gnu's ld is used with MinGW) takes object files from its command line as well as from libraries (generally .a files, where the option -lsqlite3 would refer to the library file named libsqlite3.a) and collects them in to a single executable.

So, in order to use the SQLite library, you need its header files in a place where the compiler can located them to satisfy a #include directive. And you need the matching object library so the linker can satisfy all references to functions in the SQLite API.

One recommended way to include SQLite in a project is to compile it from the single-file amalgamation, and link it directly to your program. By doing it this way, you get a library that was compiled with the same compiler as the rest of your application (which reduces the chance of subtle problems at run time) and don't need to have a DLL (or .so) file in your distribution to cause issues for your users.

It is also recommended that you have a copy of the sqlite3.exe utility available. This is a useful utility that lets you inspect a SQLite database file and make ad-hoc changes. Just put it somewhere in your PATH so that it can be used from a command prompt.

If you must use a pre-build DLL, then one is available from the SQLite project. It happens to be compiled in a way that is compatible with MinGW. Specifically, it uses the same C runtime library used by MinGW. Note that it might not be completely compatible if you were using Visual Studio or another compiler for Windows.


sqlite3.h should be in your include path, not your library path. The library file is a different one; if you're using Dev-Cpp it should be called libsqlite3.a (the .lib file mentioned by @Ignacio is for MSVC). Once you've put that file in the library path, you also need to instruct the compiler to link against it, which should be in the compile options for your Dev-Cpp project. You don't need sqlite3.exe at all; that's for when you want to query the database file manually rather than programmatically.


To use sqlite database in gcc, follow these steps...

  1. download sqlites files from here or my backup here (include sqlite3.h)
  2. unzip the downloaded file.
  3. copyinto your source code folder (.c file included)
  4. sqlite3.dll and sqlite3.h files must be together with your .c files
  5. sample source code can be found here! www (dot) tutorialspoint.com/sqlite/sqlite_c_cpp(dot) htm
  6. But from sample you must change #include to #include"sqlite3.h"
  7. if your source file is test.c then compilation process is like that

    gcc -c test.c gcc -o test.exe test.o sqlite3.dll

Makefile

test.exe: test.o sqlite3.dll
        gcc -o test.exe test.o sqlite3.dll

test.o: test.c
        gcc -c test.c

Code::Blocks Settings>Compiler settings>Linker settings>Add> then find sqlite3.dll and add. Copy sqlite3.h into your source code folder and you must change #include"sqlite3.h".


You don't need sqlite3.exe. You need the DLL to run it, and you need the import .lib to link against in order to build it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号