Linking to POSIX semaphores

View: New views
4 Messages — Rating Filter:   Alert me  

Linking to POSIX semaphores

by Peter Rockett :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I am trying to use POSIX named semaphores. Stripping it down, if I compile
the program:

#include <fcntl.h>
#include <sys/stat.h>
#include <semaphore.h>

int main()
{
       sem_open("Sem1", O_CREAT, S_IRWXU, 0);
       sem_unlink("Sem1");
       return 0;
}

and link it to /usr/lib/librt.a (which the Linux man pages say is the
necessary library), I get a link error saying that sem_open() and
sem_unlink() are unresolved. Looking inside librt.a using the nm utility, I
cannot find any reference to the POSIX semaphores. Am I doing something
dumb? Where do I find the implementations of the POSIX semaphores? Am I
misinterpreting the man pages somehow?

(I am using Ubuntu 9.1 with gcc-4.4.1.)

Peter


Re: Linking to POSIX semaphores

by Andi Hellmund :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Peter,

my man page says to link to librt _or_ libpthread. libpthread finally
defines the symbols sem_open and sem_unlink.

Andi

Peter Rockett wrote:

> I am trying to use POSIX named semaphores. Stripping it down, if I compile
> the program:
>
> #include <fcntl.h>
> #include <sys/stat.h>
> #include <semaphore.h>
>
> int main()
> {
>        sem_open("Sem1", O_CREAT, S_IRWXU, 0);
>        sem_unlink("Sem1");
>        return 0;
> }
>
> and link it to /usr/lib/librt.a (which the Linux man pages say is the
> necessary library), I get a link error saying that sem_open() and
> sem_unlink() are unresolved. Looking inside librt.a using the nm utility, I
> cannot find any reference to the POSIX semaphores. Am I doing something
> dumb? Where do I find the implementations of the POSIX semaphores? Am I
> misinterpreting the man pages somehow?
>
> (I am using Ubuntu 9.1 with gcc-4.4.1.)
>
> Peter
>
>
>  


Parent Message unknown Re: Linking to POSIX semaphores

by Peter Rockett :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Andi

Yes, the sem_*() functions are indeed defined in libpthread.a. I guess the
Ubuntu man page is out-of-date - I have e-mailed Canonical.

The program now builds OK although it seems also to require linking to
libc.a to resolve things like __syscall_error(), _dl_sysinfo_dso() and
others. But the resulting exe immediately crashes with a segmentation fault.
In fact, removing everything from the body of main(), i.e. compiling int
main(){return 0;} and linking against libc.a also crashes with a
segmentation fault. Any ideas what's causing this?

Peter


Hi Peter,

my man page says to link to librt _or_ libpthread. libpthread finally
defines the symbols sem_open and sem_unlink.

Andi

Peter Rockett wrote:

> I am trying to use POSIX named semaphores. Stripping it down, if I compile
> the program:
>
> #include <fcntl.h>
> #include <sys/stat.h>
> #include <semaphore.h>
>
> int main()
> {
>        sem_open("Sem1", O_CREAT, S_IRWXU, 0);
>        sem_unlink("Sem1");
>        return 0;
> }
>
> and link it to /usr/lib/librt.a (which the Linux man pages say is the
> necessary library), I get a link error saying that sem_open() and
> sem_unlink() are unresolved. Looking inside librt.a using the nm utility,
I
> cannot find any reference to the POSIX semaphores. Am I doing something
> dumb? Where do I find the implementations of the POSIX semaphores? Am I
> misinterpreting the man pages somehow?
>
> (I am using Ubuntu 9.1 with gcc-4.4.1.)
>
> Peter



Re: Linking to POSIX semaphores

by Andi Hellmund :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Peter,

try the -static option to build a statically bound executable. Then,
your executable shouldn't crash anymore.

Andi

Peter Rockett wrote:

> Hi Andi
>
> Yes, the sem_*() functions are indeed defined in libpthread.a. I guess the
> Ubuntu man page is out-of-date - I have e-mailed Canonical.
>
> The program now builds OK although it seems also to require linking to
> libc.a to resolve things like __syscall_error(), _dl_sysinfo_dso() and
> others. But the resulting exe immediately crashes with a segmentation fault.
> In fact, removing everything from the body of main(), i.e. compiling int
> main(){return 0;} and linking against libc.a also crashes with a
> segmentation fault. Any ideas what's causing this?
>
> Peter
>
>
> Hi Peter,
>
> my man page says to link to librt _or_ libpthread. libpthread finally
> defines the symbols sem_open and sem_unlink.
>
> Andi
>
> Peter Rockett wrote:
>  
>> I am trying to use POSIX named semaphores. Stripping it down, if I compile
>> the program:
>>
>> #include <fcntl.h>
>> #include <sys/stat.h>
>> #include <semaphore.h>
>>
>> int main()
>> {
>>        sem_open("Sem1", O_CREAT, S_IRWXU, 0);
>>        sem_unlink("Sem1");
>>        return 0;
>> }
>>
>> and link it to /usr/lib/librt.a (which the Linux man pages say is the
>> necessary library), I get a link error saying that sem_open() and
>> sem_unlink() are unresolved. Looking inside librt.a using the nm utility,
>>    
> I
>  
>> cannot find any reference to the POSIX semaphores. Am I doing something
>> dumb? Where do I find the implementations of the POSIX semaphores? Am I
>> misinterpreting the man pages somehow?
>>
>> (I am using Ubuntu 9.1 with gcc-4.4.1.)
>>
>> Peter
>>    
>
>
>
>