Compiling and Linking with Shared Libs Under GCC

Compiling and Linking with Shared Libs Under GCC

Post by Bruce W. Bigb » Sat, 29 Mar 1997 04:00:00



I created a simple program under RedHat 4.0 Linux, below, and I am
trying to link it with the libpthread.so.0 library, which is in my /lib
directory.  Here is my link line:

gcc -D_REENTRANT ThreadExample.c -lpthread

Gcc keeps complaining about the pthread_t definition, even though the
pthread.h include file is under the standard /usr/include path.  In
addition, gcc is unable to find the standard shared library
libpthread.so.0, which is in my /lib directory.  I did a ldconfig -p and
libthread.so.0 is a symbolic link to /lib/libthread.so.0.5.  I give...
Can someone please help?  Here is the program:

#include <stdio.h>
#include <pthread.h>

static void* foo(void* arg);

int
main(int argc, char** argv)
{
   pthread_t threads[3];
   int status[3];
   int i = 0;
   int j = 0;

   threads[i++] = pthread_create(&thread1, NULL, foo, "thread1");
   threads[i++] = pthread_create(&thread2, NULL, foo, "thread2");
   threads[i++] = pthread_create(&thread3, NULL, foo, "thread3");

   for (j = 0; j < i; j++) {
      (void) pthread_join(threads[j], &status[j]);
   }
   exit(0);

Quote:}

static
void*
foo(void* arg)
{
   printf("Hi!  I'm thread %s\n", (char*) arg);
   pthread_exit(0);
   return((void*) NULL);

Quote:}

--
Bruce W. Bigby

http://www.frontiernet.net/~bbigby
Rochester, NY
Do for others that which you would have others do for you.
Love your enemies, and be perfect, like your Father in Heaven.  Matthew
5:43-48
 
 
 

Compiling and Linking with Shared Libs Under GCC

Post by Bruce W. Bigb » Sat, 29 Mar 1997 04:00:00



> I created a simple program under RedHat 4.0 Linux, below, and I am
> trying to link it with the libpthread.so.0 library, which is in my /lib
> directory.  Here is my link line:

> gcc -D_REENTRANT ThreadExample.c -lpthread

> Gcc keeps complaining about the pthread_t definition, even though the
> pthread.h include file is under the standard /usr/include path.  In
> addition, gcc is unable to find the standard shared library
> libpthread.so.0, which is in my /lib directory.  I did a ldconfig -p and
> libthread.so.0 is a symbolic link to /lib/libthread.so.0.5.  I give...
> Can someone please help?  Here is the program:

[snip]

I realize that the previous program won't have a chance in hell of
compiling.  Perhaps the following code will:

#include <stdio.h>
#include <pthread.h>

static void* foo(void* arg);

int
main(int argc, char** argv)
{
   pthread_t threads[3];
   int status[3];
   int i = 0;
   int j = 0;

   pthread_create(&threads[i++], NULL, foo, "thread1");
   pthread_create(&threads[i++], NULL, foo, "thread2");
   pthread_create(&threads[i++], NULL, foo, "thread3");

   for (j = 0; j < i; j++) {
      (void) pthread_join(threads[j], &status[j]);
   }
   exit(0);

Quote:}

static
void*
foo(void* arg)
{
   printf("Hi!  I'm thread %s\n", (char*) arg);
   pthread_exit(0);
   return((void*) NULL);

Quote:}

--
Bruce W. Bigby

http://www.frontiernet.net/~bbigby
Rochester, NY
Do for others that which you would have others do for you.
Love your enemies, and be perfect, like your Father in Heaven.  Matthew
5:43-48

 
 
 

Compiling and Linking with Shared Libs Under GCC

Post by Bruce W. Bigb » Sun, 30 Mar 1997 04:00:00




> > I created a simple program under RedHat 4.0 Linux, below, and I am
> > trying to link it with the libpthread.so.0 library, which is in my /lib
> > directory.  Here is my link line:

> > gcc -D_REENTRANT ThreadExample.c -lpthread

> > Gcc keeps complaining about the pthread_t definition, even though the
> > pthread.h include file is under the standard /usr/include path.  In
> > addition, gcc is unable to find the standard shared library
> > libpthread.so.0, which is in my /lib directory.  I did a ldconfig -p and
> > libthread.so.0 is a symbolic link to /lib/libthread.so.0.5.  I give...
> > Can someone please help?  Here is the program:

> [snip]

Nevermind.  I solved my problem.  I changed the line,

#include <pthread.h>

to

#include <pthread/mit/pthread.h>

Do I have to do this?  I thought I only had to include <pthread.h>.

As for my troubles linking with -lpthread, I had to add the symbolic
link,

/lib/libpthread.so --> /lib/libpthread.so.0

which is also a link,

/lib/libpthread.so.0 --> /lib/libpthread.so.0.5

By the way, what is the proper declaration for the pthread_join
function?  My function prototypes say the following:

int pthread_join(pthread_t, void**);

However, SunSoft's document on POSIX 1003.4a/D8 threads say that the
proper declaration for pthread_join is

int pthread_join(pthread_t, int*);

Apparently, the int* refers to the exit status of a thread.  However,
how does one use the void** of the former declaration?
--
Bruce W. Bigby

http://www.frontiernet.net/~bbigby
Rochester, NY
Do for others that which you would have others do for you.
Love your enemies, and be perfect, like your Father in Heaven.  Matthew
5:43-48

 
 
 

Compiling and Linking with Shared Libs Under GCC

Post by Bruce W. Bigb » Sun, 30 Mar 1997 04:00:00


[snip]

Quote:

> Nevermind.  I solved my problem.  I changed the line,

> #include <pthread.h>

> to

> #include <pthread/mit/pthread.h>

> Do I have to do this?  I thought I only had to include <pthread.h>.

[snip]
Discovered that I need to define _MIT_POSIX_THREADS in my Makefile:

-D_MIT_POSIX_THREADS

I need some serious documentation.  Gotta go scouting...
--
Bruce W. Bigby

http://www.frontiernet.net/~bbigby
Rochester, NY
Do for others that which you would have others do for you.
Love your enemies, and be perfect, like your Father in Heaven.  Matthew
5:43-48

 
 
 

Compiling and Linking with Shared Libs Under GCC

Post by Wolfram Gloge » Sun, 30 Mar 1997 04:00:00



Quote:> Discovered that I need to define _MIT_POSIX_THREADS in my Makefile:

> -D_MIT_POSIX_THREADS

> I need some serious documentation.  Gotta go scouting...

The appropriate docs come with the LinuxThreads package that you
seem to have installed.  The -D_MIT_POSIX_THREADS define and
in fact all the includes in /usr/include/pthreads/mit are for
a _different_, obsolete user-space thread package.

You probably want to remove /usr/include/pthread.h and replace
it with the pthread.h from LinuxThreads.

Regards,
Wolfram.

 
 
 

1. /usr/lib/gcc-lib/i386-linux and /usr/lib/gcc-lib/i486-linux

Hi Linuxers,

I just got a new copy of gcc-2.4.5 and source code of the pl11.
After having done the installation of gcc-2.4.5, I realised that
I have an extra directory: /usr/lib/gcc-lib/i386-linux.
The question is whether I need to keep the old gcc stuff under
the i386-linux.  I really like to clean it up so as to save 5.1
meg disk space (another poor Linux user  ;-( ).

Your answer is very appreciated.  Please send answer directly
to my address or the corresponding interest parties to reduce
the network load.  Thank you in advance.

Wing



2. NET100 patch. Where are the manual updates ?

3. GCC/ld error linking shared X libs

4. uugetty problems look like they're solved!

5. linking errors building shared object libs with gcc 2.7.0

6. X server problem

7. gcc fails to link to shared libs, why?

8. Alpha stages of formation of Phoenix, AZ USA Linux users group

9. Linking Pilot-link libs in gcc

10. Linking with Shared and Non-shared libs.

11. Help - shared libs lib****.so.4 lib***.so.5

12. Slackware 2.2's GCC doesn't link X libs

13. Shared lib - do I need to link against libc?