Why does the default cpath not have lib?.so in it?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Why does the default cpath not have lib?.so in it?

Alex Ames
I feel like I'm probably missing something obvious here, so forgive me
if this is a silly question.

The default package.cpath on linux has ./?.so and a few others, but it
seems like it also ought to have a ./lib?.so variant. Otherwise, I
have to take some awkward extra step depending on how I decide to load
the library.

When I compile a library called foo on my Linux box, I get a libfoo.so
file. If I want to load this into Lua, I thought I could just call
require "foo" as long as the .so file was in the directory with my
script, but that doesn't work because of the lib prefix.
I could instead call require "libfoo", but then I have to change the
name of the C function from luaopen_foo to luaopen_libfoo. And then it
breaks for windows unless I change the .dll to be called libfoo.dll
there too. I guess that's not terrible but that seems strange.
I could ship a foo.lua file with my .so/.dll that runs package.loadlib
on the appropriate file, but that needs to know the exact name of the
library, and I don't know how to have it check which environment it's
running so it can decide whether to attempt to open the dll or the
.so.
I could change the linux .so to not have the lib prefix, but I'm not
even sure how to do this with gcc automatically. AFAIK it
automatically prepends "lib" and I'd have to go change it manually
every time I recompile.

I feel like either I'm missing some connection here, that there ought
to be an easier way to go about this. it seems like if the cpath just
included lib?.so I could make a simple call to require and be done
with it, no need to ship any files except the .so/.dll itself.

Thanks

Alex

Reply | Threaded
Open this post in threaded view
|

Re: Why does the default cpath not have lib?.so in it?

Geoff Leyland
On 13/09/2011, at 5:12 PM, Alex Ames wrote:

> I feel like I'm probably missing something obvious here, so forgive me
> if this is a silly question.

I don't think it's a silly question, but I'm afraid I can't answer most of it, however,

> I could change the linux .so to not have the lib prefix, but I'm not
> even sure how to do this with gcc automatically. AFAIK it
> automatically prepends "lib" and I'd have to go change it manually
> every time I recompile.

How are you invoking cc (ld/install...)?  For me, "cc -shared test.c" creates a library called a.out and "cc -shared test.c -o test.so" makes a library called test.so, not libtest.so.

Just in case, "cc main.c -ltest" does expect a libtest.so to be available, but you usually don't link to Lua modules at compile time, and I don't think that's your problem.

Cheers,
Geoff
Reply | Threaded
Open this post in threaded view
|

Re: Why does the default cpath not have lib?.so in it?

liam mail
On 13 September 2011 06:46, Geoff Leyland <[hidden email]> wrote:

> On 13/09/2011, at 5:12 PM, Alex Ames wrote:
>
>> I feel like I'm probably missing something obvious here, so forgive me
>> if this is a silly question.
>
> I don't think it's a silly question, but I'm afraid I can't answer most of it, however,
>
>> I could change the linux .so to not have the lib prefix, but I'm not
>> even sure how to do this with gcc automatically. AFAIK it
>> automatically prepends "lib" and I'd have to go change it manually
>> every time I recompile.
>
> How are you invoking cc (ld/install...)?  For me, "cc -shared test.c" creates a library called a.out and "cc -shared test.c -o test.so" makes a library called test.so, not libtest.so.
>
> Just in case, "cc main.c -ltest" does expect a libtest.so to be available, but you usually don't link to Lua modules at compile time, and I don't think that's your problem.
>
> Cheers,
> Geoff
>

Yes Lua loadable shared objects drop the prefix of lib and even
require '.so' on Mac Osx, this is not a great problem as the SO is
really only intended to be used by Lua. If for any reason this would
be problematic you could always introduce a symlink foo.so ->
libfoo.dyld. As Geff points out it is very easy to tell gcc what name
to use rather than the default of libName.so or libName.dyld

Liam

Reply | Threaded
Open this post in threaded view
|

Re: Why does the default cpath not have lib?.so in it?

Sebastien Lai
In reply to this post by Alex Ames
> When I compile a library called foo on my Linux box, I get a libfoo.so
file.

Please tell what kind of compiler you use that you get a libfoo.so?

I've never seen something like that before. I suggest you to try to
use a vanilla version of GCC or Clang.

Reply | Threaded
Open this post in threaded view
|

Re: Why does the default cpath not have lib?.so in it?

Luiz Henrique de Figueiredo
In reply to this post by Alex Ames
> The default package.cpath on linux has ./?.so and a few others, but it
> seems like it also ought to have a ./lib?.so variant.

You're free to add this to your package.cpath if you need it.
But one reason not to use libxxx.so is to avoid clashing with
system libraries. For instance, an MD5 library for Lua might
conflict with a libmd5.so from the system. (It actually did
once in an old Linux.) Actually, you're free to call your
libraries anything you want; just adjust package.cpath.
I don't think you even need a .so extension.