Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-28-2009, 12:05 AM   #1
Registered: Oct 2008
Posts: 43

Rep: Reputation: 16
[SOLVED] Linking woes, or, gcc is the devil.

So I don't understand shared objects very well. I'm trying to install a library called luapgsql, hosted here, that allows lua scripts to interface with a PostgreSQL database. It's a little old, but the only alternative is luasql, which doesn't support parametrized queries and is therefore categorically unacceptable. luapgsql supports everything I need, anyway. I'm building this on NetBSD 5.0/amd64.

luapgsql is distributed as a single luapgsql.c file with a single header file luapgsql.h. It links to libpq via #include libpq-fe.h; the libpq libraries are all stored in /usr/pkg/lib, and libpq-fe.h is stored in /usr/pkg/include, as are lua.h and lauxlib.h (also linked to, but I'm not experiencing issues with them... yet). I'm building it like this:

gcc -o -O2 -fPIC -I/usr/pkg/include -shared luapgsql.c -L/usr/pkg/lib -lpq
It builds successfully and creates Now the way lua modules work is you simply copy them into ${prefix}/include/lua/5.1/, and then you can use them via require(<library name>), which is in this case require(pgsql). I copied into /usr/pkg/lib/lua/5.1, and ran the little included test.lua which starts with require(pgsql)

At this point, lua complains "Error in module pgsql: cannot find shared object file". Now, exists in /usr/pkg/lib, which looks like this:

$ ls /usr/pkg/lib
which to me should mean that is in the expected location; unfortunately, lua disagrees. Does anyone have any idea what I might be doing wrong, and how to fix it? 'twould be very much appreciated.


Last edited by irkkaaja; 08-28-2009 at 02:46 AM. Reason: problem solved!
Old 08-28-2009, 02:31 AM   #2
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Stretch (Fluxbox WM)
Posts: 1,389
Blog Entries: 52

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
There is a difference between the path used to access the library definitions at link time, and the path used to dynamically load the library at runtime.

The -L flag you gave to gcc gives it a path to search for static libraries and for dynamic libraries for use by the linker, but this does not affect where the executable will search for dynamic libraries (typically only /lib and /usr/lib).

There are several ways of providing a runtime library path. An older method was to use the environment variable LD_LIBRARY_PATH. Now, if you want to provide global access to the /usr/pkg/lib, you would add it to one of the /etc/ files, and run ldconfig (see 'man ldconfig'). Or if you just want it for a single application you can incorporate it into the executable by using the linker flag '-R /usr/pkg/lib'.
Old 08-28-2009, 02:44 AM   #3
Registered: Oct 2008
Posts: 43

Original Poster
Rep: Reputation: 16
It works!

Thanks a ton.


gcc, linking, lua

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
library linking problem in gcc surya1006 Linux - Newbie 2 09-05-2008 03:43 PM
linking problem with gcc tokernizer Programming 1 11-19-2005 12:56 PM
gcc linking error. ugenn Linux - Software 0 01-22-2003 11:18 PM
Linking without gcc, but with ld gluon Programming 3 05-29-2002 05:11 AM
Linking when using gcc/f2c combination? chemspecialist Programming 0 05-17-2001 12:40 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:44 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration