2

I want to make some modifications to the pthread library used for my program. That is why I want to link with my own modified pthread library. I can take the source code in glibc for pthread, modify it and use that for my programs.

Normally you use the flag -pthread for linking with the original pthread library. How do I specify in my makefile to link with my own library.

MetallicPriest
  • 29,191
  • 52
  • 200
  • 356

4 Answers4

4

Just use the -L option to specify the directory where your custom lib is present and use -l option to specify the name of your library. For Ex:

-L/root/x/mylib -lmypthread

In this case your lib name should libmypthread.so

Refer to http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html for more details.

To ensure that the library is found for loading when you execute your program, do

export LD_LIBRARY_PATH = $(LD_LIBRARY_PATH):/root/x/mylib
Jay
  • 24,173
  • 25
  • 93
  • 141
  • 1
    You might consider passing the `-rpath` option to the linker instead: `-Wl,-rpath $MYLIB_DIR`, if I've got the syntax right. – Brett Hale Mar 10 '12 at 11:34
2

-pthread is equivalent to -lpthread -D_REENTRANT with gcc/glibc. You can do the same, but with a different link argument (-lname-of-library).

strcat
  • 5,376
  • 1
  • 27
  • 31
2

Don't use -pthread. It is an undocumented GCC option, probably for compatibility with some other (Solaris?) compilers.

The -D_REENTRANT definition which it -pthread enables is completely unnecessary in glibc; none of the headers depend on this macro for thread safety. (The last of such mechanisms were removed from the glibc headers in 1998!) Simply linking in -lpthread is enough to switch glibc functions to thread safe mode, and -lpthread can be substituted with your own library, like the other answer says.

Kaz
  • 55,781
  • 9
  • 100
  • 149
  • Unfortunately, while that's the way things *should be*, it may not be correct for gcc. In theory, gcc supposedly uses a POSIX-incompatible memory model whereby it could perform optimizations that wrongly move accesses across synchronization, and you need `-pthread` or `-std=c11` to make it use the correct memory model. I've never encountered a situation where it matters but I recall reading a thread on one mailing list or on the gcc bug tracker where this was the gcc developers attitude, and it may have involved a real bug... – R.. GitHub STOP HELPING ICE Mar 10 '12 at 12:26
  • Oh, I recall the issue now. It's accessing memory as a larger type, and it affects bitfields. gcc ignores the declared integer type on the bitfield and often performs read/modify/write cycles with a much larger (e.g. 64 bits) load/store operation, which can be extremely dangerous if the data that happens to be adjacent to the bitfield in memory is accessed by another thread (or even a signal handler). Supposedly `-pthread` disables the behavior, and there may now be a separate `-f` option for it. Interestingly, it was the Linux *kernel* this broke. – R.. GitHub STOP HELPING ICE Mar 10 '12 at 12:29
  • `-pthread` isn't documented by GCC, so they are free to give it new meanings. – Kaz Mar 10 '12 at 17:32
0

Compile the library in a different name, e.g., libmypthread.so and put it in one of the directories contained in your LD_LIBRARY_PATH environment variable (or add a new directory). Now you can use -lmypthread to link to your library.

perreal
  • 94,503
  • 21
  • 155
  • 181