¿Cómo puedo vincularme con mi propia biblioteca pthread?

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 para 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.

preguntado el 10 de marzo de 12 a las 10:03

4 Respuestas

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

Referirse a http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html para ver más información.

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

respondido 10 mar '12, 11:03

You might consider passing the -rpath option to the linker instead: -Wl,-rpath $MYLIB_DIR, if I've got the syntax right. - Brett Hale

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

respondido 10 mar '12, 11:03

You can confirm this is true with your particular gcc version. Use gcc -dumpspecs | grep "%{pthread". - David Schwartz

turns out you don't really need -D_REENTRANT, ie just -lname-of-library debería ser suficiente - Christoph

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

El -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.

respondido 10 mar '12, 11:03

Unfortunately, while that's the way things debiera ser, 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 DEJA DE AYUDAR A ICE

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 núcleo this broke. - R .. GitHub DEJA DE AYUDAR A ICE

-pthread isn't documented by GCC, so they are free to give it new meanings. - Kaz

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.

respondido 10 mar '12, 11:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.