Menu

sqlite3 in dev-c++

qwerty mk
2008-12-31
2012-09-26
  • qwerty mk

    qwerty mk - 2008-12-31

    Hi, I've been searching for days now and I still can't seem to find out how to do the following:
    I am creating a simple program that needs to make a simple sql query for an entry for exaple:
    SELECT * FROM TABLE1 WHERE PRODUCT_ID=USERCHOICE
    I've tried using the sqlite3.h file that sqlite distributes and I keep getting a linker error:
    [Linker error] undefined reference to sqlite3_open' [Linker error] undefined reference tosqlite3_errmsg'
    [Linker error] undefined reference to sqlite3_close' [Linker error] undefined reference tosqlite3_exec'
    [Linker error] undefined reference to `sqlite3_close'

    I got this when trying to compile a sample program just to get the ball rolling, here is the sample program:

    include <stdio.h>

    include <stdlib.h>

    include "sqlite3.h"

    static int callback(void *NotUsed, int argc, char **argv, char **azColName){
      NotUsed=0;
      int i;
      for(i=0; i&lt;argc; i++){
        printf(&quot;%s = %s\n&quot;, azColName[i], argv[i] ? argv[i]: &quot;NULL&quot;);
      }
      printf(&quot;\n&quot;);
      return 0;
    }
    
    int main(int argc, char **argv){
      sqlite3 *db;
      char *zErrMsg = 0;
      int rc;
    
      if( argc!=3 ){
        fprintf(stderr, &quot;Usage: %s DATABASE SQL-STATEMENT\n&quot;, argv[0]);
        exit(1);
      }
      rc = sqlite3_open(argv[1], &amp;db);
      if( rc ){
        fprintf(stderr, &quot;Can't open database: %s\n&quot;, sqlite3_errmsg(db));
        sqlite3_close(db);
        exit(1);
      }
      rc = sqlite3_exec(db, argv[2], callback, 0, &amp;zErrMsg);
      if( rc!=SQLITE_OK ){
        fprintf(stderr, &quot;SQL error: %s\n&quot;, zErrMsg);
        /* This will free zErrMsg if assigned */
        if (zErrMsg)
           free(zErrMsg);
      }
      sqlite3_close(db);
      return 0;
    }
    

    Any help would be greatly appreciated. Thanks alot

     
    • cpns

      cpns - 2009-01-01

      This line:

      > gcc.exe "J:\Documents and Settings\Mike\My Documents\food\main.c"
      > -o "J:\Documents and Settings\Mike\My Documents\food\main.exe"
      > -g3 -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3

      is the invocation of the compiler/linker. And as I suggested it shows no attempt to link any additional libraries. However, if you are creating a build that uses non-standard additional libraries, or other options taht you do not want to apply to all projects, I strongly suggest that you do not compile/link in one step like that, but rather use Dev-C++'s project manager. File->Project->New. Then all the necessary options are stored with a project, isolated from other builds that may not require these changes, and it provides support for separate file compilation allowing you to break your application into more than one source file. When you use the project tool, project options are set in Project->Project options rather than Tools->Compiler options.

      > I checked the sqlite.org site and I can't find any file called libsqlite2.a or 3.a,

      Sorry the "2.a" was my typo. I was guessing at the probable name. I would not have looked at sqllite.org before your own hard drive! You installed the library, where did you put the archive file (whatever it is called)? It will by convention have a lib prefix and a .a extension as in libXXX.a where XXX is teh library name.

      Since this is an open source project, if you merely downloaded the source code, then yes, of course you have to build the library first. That however may not be straight forward since the build may assume a Linux/Unix environment. You should in the first instance try to find binaries specifically built for MinGW/GCC. Failing that find MinGW specific instruction for building the library; for that you may need additional tools from www.mingw.org.

      The simplest possible approach is to use a Dev-C++ specific Devpak. http://devpaks.org/category.php?category=Database you will find several versions (most recent currently being 3.5.6) of the library pre-built and packaged for installation and use with Dev-C++. Simply download the pack and double click the file to get the package manager install it. Often Devpaks install project templates that add the necessary linker options, I don't know about this one. Alternatively download it via Dev-C++'s "Tools->Check for updates and packages" menu, and select the Devpaks.org server.

      Incidentally your log does indicate that you have not read the "PLEASE READ BEFORE POSTING A QUESTION", where you are advised not to use project paths that contain spaces - putting your project in "My Documents" is not a good idea. The fact that you do not understand how to link a library also indicates that you have not read that thread since as I mentioned before it is explained there also.

      Clifford

       
    • cpns

      cpns - 2008-12-31

      Header files are processed by the pre-processor, you have got as far as linking, so no amount of fiddling with header files is going to help, and the code is largely irrelevant too since to get as far as linking it must have compiled.

      What you almost certainly have not done is link the library. The library is probably a file called something like libsqlite2.a and would be linked by adding -lsqlite3 as a linker option. Depending on where you installed teh library, you may also need a -L<path> option.

      ALWAYS post the complete Compile Log text. It shows what options you did use.

      All of the above is explained in the "PLEASE READ BEFORE POSTING A QUESTION" thread.

       
    • qwerty mk

      qwerty mk - 2008-12-31

      Sorry, here is the compile log:

      Compiler: Default compiler
      Executing gcc.exe...
      gcc.exe "J:\Documents and Settings\Mike\My Documents\food\main.c" -o "J:\Documents and Settings\Mike\My Documents\food\main.exe" -g3 -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
      J:\DOCUME~1\Mike\LOCALS~1\Temp/ccc1aaaa.o(.text+0x101): In function main': J:/Documents and Settings/Mike/My Documents/food/main.c:25: undefined reference tosqlite3_open'
      J:\DOCUME~1\Mike\LOCALS~1\Temp/ccc1aaaa.o(.text+0x115):J:/Documents and Settings/Mike/My Documents/food/main.c:27: undefined reference to sqlite3_errmsg' J:\DOCUME~1\Mike\LOCALS~1\Temp/ccc1aaaa.o(.text+0x13c):J:/Documents and Settings/Mike/My Documents/food/main.c:28: undefined reference tosqlite3_close'
      J:\DOCUME~1\Mike\LOCALS~1\Temp/ccc1aaaa.o(.text+0x176):J:/Documents and Settings/Mike/My Documents/food/main.c:31: undefined reference to sqlite3_exec' J:\DOCUME~1\Mike\LOCALS~1\Temp/ccc1aaaa.o(.text+0x1ba):J:/Documents and Settings/Mike/My Documents/food/main.c:38: undefined reference tosqlite3_close'
      collect2: ld returned 1 exit status

      Execution terminated

      I checked the sqlite.org site and I can't find any file called libsqlite2.a or 3.a, do I have to somehow make it myself, if I do I have no idea where to start. Also when I tried putting -lsqlite3 in the linker cmd line options I got this:

      Compiler: Default compiler
      Executing gcc.exe...
      gcc.exe "J:\Documents and Settings\Mike\My Documents\food\main.c" -o "J:\Documents and Settings\Mike\My Documents\food\main.exe" -g3 -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -lsqlite3 -g3
      C:\Dev-Cpp\Bin..\lib\gcc\mingw32\3.4.2........\mingw32\bin\ld.exe: cannot find -lsqlite3
      collect2: ld returned 1 exit status

      Execution terminated

      What do I do from here. BTW is there a tutorial or st that shows how to set up things like this on dev-c++?

      Thanks alot

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.