Recent

Author Topic: Static linking SQLIte Linux  (Read 9069 times)

chrnobel

  • Sr. Member
  • ****
  • Posts: 283
Static linking SQLIte Linux
« on: December 13, 2011, 02:43:22 pm »
Hi all.

I know this question have been raised before, but searching the forum didn't make me much wiser.

The problem is straight forward - having an web application running on Linux as CGI, but not being certain that the SQLite library is there, and which version.

I want to ensure that together with my project is also the SQLite library, typically in the same directory as the program.

So does anyone have an easy to understand, step by step guide, showing how to do this in Lazarus?

Thanks
Christian

jixian.yang

  • Full Member
  • ***
  • Posts: 173
Re: Static linking SQLIte Linux
« Reply #1 on: December 13, 2011, 04:18:06 pm »
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}

interface

uses
{$IFDEF MSWINDOWS}
  Windows,
  Messages,
{$ENDIF}
{$IFDEF LINUX} // for Kylix
  Libc,
  Types,
{$ENDIF}
  Classes,
{$IFDEF USETYPEINFO}
  // this pure pascal version must handle the 64-bits ordinal values and
  // the special layout of the underlying compiler (mostly FPC)
  typinfo,
{$ENDIF}
{$IFNDEF LVCL}
  Contnrs, // for TObjectList
{$ENDIF}
  SysUtils;
{$IFNDEF FPC}
{$L 'OBJ\sqlite3se.obj'}  //for Delphi
{$ELSE}
{$IFDEF MSWINDOWS}
{$linklib libsqlite3se.a}
{$linklib libmsvcrt.a}
{$linklib libkernel32.a}
{$linklib libgcc.a}
{$ELSE}
{$linklib libsqlite3selinux.a}
{$Linklib c}
{$linklib libgcc.a}
{$ENDIF}
{$ENDIF}

and add -Xt in fpc.cfg.

chrnobel

  • Sr. Member
  • ****
  • Posts: 283
Re: Static linking SQLIte Linux
« Reply #2 on: January 06, 2012, 09:56:40 am »
Sorry, might be that I am dumb -  but that did not make much sense, nor did it make me any wiser.

I can get SQLite to work by copying these files to /usr/lib :

libsqlite3.so (the library)
libsqlite3.so.0 (link)
libsqlite3.so.0.8.6 (link)

The solution is 100% Linux only, and no Delphi etc.

So how do I point to the library (which is in same directory as the binary) in FPC/Laz?


BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Static linking SQLIte Linux
« Reply #3 on: January 06, 2012, 10:11:53 am »
I want to ensure that together with my project is also the SQLite library, typically in the same directory as the program.
Instead of trying to get Linux to work like Windows (having the dll in your application directory) why not make sure the sqlite libraries are installed in their proper place? Then you wouldn't need to statically link to sqlite, the sqlite libraries get updated with the rest of the system etc :)

I'd create a package for your distribution's package manager that does that for you... you could e.g. create a .deb file on Debian/Ubuntu or an .rpm on Redhat etc that contains your CGI application and a requirement that the sqlite package be installed...
(Example: the libsqlite3-0 package on my Debian Squeeze x64 provides /usr/lib/libsqlite3.so.0 (symlink) and libsqlite3.so.0.8.6)

If you don't want to use the package manager, I believe there are some "universal" Linux installers around, haven't worked with them, though...

Quote from: chrnobel
So how do I point to the library (which is in same directory as the binary) in FPC/Laz?
Are you saying your program is in /usr/lib, too? That would be a bit unusual?

I haven't used sqlite in FPC on Linux yet, and can't tell you how that should work, but surely there's a wiki page on SQLite that should have that info?
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Static linking SQLIte Linux
« Reply #4 on: January 06, 2012, 10:22:36 am »
Quote
So how do I point to the library (which is in same directory as the binary) in FPC/Laz?
You can add directories to the library search path by setting the LD_LIBRARY_PATH environment variable. To add your current directory to the search path do
Code: [Select]
export LD_LIBRARY_PATH=. or
Code: [Select]
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

chrnobel

  • Sr. Member
  • ****
  • Posts: 283
Re: Static linking SQLIte Linux
« Reply #5 on: January 06, 2012, 10:27:35 am »
Instead of trying to get Linux to work like Windows (having the dll in your application directory) why not make sure the sqlite libraries are installed in their proper place? Then you wouldn't need to statically link to sqlite, the sqlite libraries get updated with the rest of the system etc :)
I can write a program in FPC that can be used on all most any distro, and if I can make a totally self contained product, by going "the Windows way", then the user can just run the program in userspace, and do not have to do any further installation - just like e.g. Firefox does.

Quote
Are you saying your program is in /usr/lib, too? That would be a bit unusual?
No on the contrary, the program shall run only in userspace, and without any requirement for acting as root, it was just to explain which files I am looking at.

chrnobel

  • Sr. Member
  • ****
  • Posts: 283
Re: Static linking SQLIte Linux
« Reply #6 on: January 06, 2012, 10:31:41 am »
Quote
So how do I point to the library (which is in same directory as the binary) in FPC/Laz?
You can add directories to the library search path by setting the LD_LIBRARY_PATH environment variable. To add your current directory to the search path do
Code: [Select]
export LD_LIBRARY_PATH=. or
Code: [Select]
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
Can this be done from inside the application - as mentioned I want to be able to handout a program, and no matter where the user copy this program (of course including the required libraries) he can use it without any further configuration/installation?

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Static linking SQLIte Linux
« Reply #7 on: January 06, 2012, 10:52:53 am »
Quote
Can this be done from inside the application - as mentioned I want to be able to handout a program, and no matter where the user copy this program (of course including the required libraries) he can use it without any further configuration/installation?
If you are using a TSQLite3Connection you can set the full library path before it gets loaded. fe:
Code: [Select]
SQLiteLibraryName:='./sqlite3.so';To be done before any connection is established, fe. in an initialization section.

chrnobel

  • Sr. Member
  • ****
  • Posts: 283
Re: Static linking SQLIte Linux
« Reply #8 on: January 06, 2012, 01:19:44 pm »
Quote
Can this be done from inside the application - as mentioned I want to be able to handout a program, and no matter where the user copy this program (of course including the required libraries) he can use it without any further configuration/installation?
If you are using a TSQLite3Connection you can set the full library path before it gets loaded. fe:
Code: [Select]
SQLiteLibraryName:='./sqlite3.so';To be done before any connection is established, fe. in an initialization section.
ludob you made my day - thanks :-))

I have for so long time tried to find a way, and then it can be done so simple - i really don't understand why such a simple and elegant solution is overlooked, and done into rocket science.

So I ended up in this solution:
Code: [Select]
procedure TForm1.setparms(Sender: TObject);
begin
  SQLiteLibraryName:='./libsqlite3.so';
  SQLite3Connection1.DatabaseName:='./test1.sqlite' ;
  SQLite3Connection1.Connected:=true;
  SQLTransaction1.Active:=true;
  SQLQuery1.Active:=true;;
end;

The procedure is called when the form loads, and one shall ensure that the SQLite3Connection.connected is set to false in the IDE.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Static linking SQLIte Linux
« Reply #9 on: January 06, 2012, 02:06:31 pm »
Chrnobel,

Quote
I have for so long time tried to find a way, and then it can be done so simple - i really don't understand why such a simple and elegant solution is overlooked, and done into rocket science.

It does help if you provide a correct topic title and a bit more background information. I was confused by your post as I thought you wanted static linking. You didn't, you wanted to specify the path for a dynamic library.

The first responder, understandably, responded to the static linking part.

Fortunately, the next couple of posts clarified the situation so that Ludo could give you the non-rocket-science solution.

In future, I would gently suggest that you provide a bit more information (what are you trying to do conceptually, what you tried) up front, be as clear as possible - not everybody understands the same level of English - and be a bit more forgiving yourself (saw the "elitist" remark on the future of Lazarus thread)... after all, we're trying to help here  :)

That said, I had a quick look through the wiki and saw the sqlite documentation seems to be a bit of a mess. I also don't think the FPC documentation covers sqlite, so I understand your frustration.

Regards,
BigChimp

Edit: added SQLiteLibraryName to wiki, not in an ideal spot, but this hopefully helps people: http://wiki.lazarus.freepascal.org/Lazarus_Database_Tutorial#Using_the_SQLdb_components_with_TSQLIte3Connection
« Last Edit: January 06, 2012, 02:16:18 pm by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

chrnobel

  • Sr. Member
  • ****
  • Posts: 283
Re: Static linking SQLIte Linux
« Reply #10 on: January 06, 2012, 02:36:27 pm »
Hi BigChimp

I acknowledge that maybe I have misunderstood the Static Linking concept, but I would mean that this question quite clearly explains my problem:
Quote
The problem is straight forward - having an web application running on Linux as CGI, but not being certain that the SQLite library is there, and which version.

I want to ensure that together with my project is also the SQLite library, typically in the same directory as the program.
And I have asked the same question earlier where the topic was something like "SQLite (or was it MySQL) with embedded libfile" (can't quite remember  but it was something like that) - so it is actually quite difficult to make a topic in one line covering the whole problem, and I still consider it much better than just "SQLite3 problem".

And don't get me wrong, I do so definitely appreciate the help given here, but we all have to sometimes understand that not all do have the same skill level, and if we want Lazarus to be a success,  everybody (including me self) have to be a bit humble.

And the elitist remark is not targeted you, but we have to admit that some of the answers given (sometimes) to newbees are so rude that they can scare people away, which is a shame for both parts.

BTW, thanks for the addition to the wiki, thumbs up :-))

/Christian
« Last Edit: January 06, 2012, 02:46:21 pm by chrnobel »

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Static linking SQLIte Linux
« Reply #11 on: January 06, 2012, 03:08:49 pm »
And don't get me wrong, I do so definitely appreciate the help given here, but we all have to sometimes understand that not all do have the same skill level, and if we want Lazarus to be a success,  everybody (including me self) have to be a bit humble.

And the elitist remark is not targeted you, but we have to admit that some of the answers given (sometimes) to newbees are so rude that they can scare people away, which is a shame for both parts.

BTW, thanks for the addition to the wiki, thumbs up :-))

/Christian
Christian,

Thanks for clarifying & the thanks  :D

Ok, all good  - on to the next problem and hopefully solution  :)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018