How can I link with my own pthread library
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.
4 Answers 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:
In this case your lib name should libmypthread.so
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
You might consider passing the -rpath option to the linker instead: -Wl,-rpath $MYLIB_DIR , if I’ve got the syntax right.
-pthread is equivalent to -lpthread -D_REENTRANT with gcc/glibc. You can do the same, but with a different link argument ( -lname-of-library ).
You can confirm this is true with your particular gcc version. Use gcc -dumpspecs | grep «%
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.
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.
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.
How to compile a C program that uses pthread.h?
I’m a beginner and I’m new to Ubuntu. I just installed it and want to run a C program. I have no idea what platform to use or where to write the code. I need to use the pthread.h header file in the program. Can anyone help me?
5 Answers 5
gcc MyProgram.c -o MyProgram -lpthread
and dont forget to include the POSIX library in your code. It will compile your code.
by default GCC does not include the pthread library. so you have to include the library using lpthread argument.
Also note that in gcc-4.8 there is not -lpthread argument in man gcc . But there is a -pthread argument. Both work fine on Ubuntu 14.04 with gcc-4.8.
If you are going to compile a C program with pthread.h in LINUX using GCC or G++ you will have to use –lpthread option after the compile command.
gcc is compiler command (compiler name)
xyz.c is a source file name.
-o is an option to create objcect file.
xyz is the name of object (binary) file.
-lpthread is an option for pthread.h
for more details here is the link conatining complete article on it.
Compiling C program with pthread.h in Linux.
First thing you’ll need in Ubuntu to compile C/C++ programs is installing GCC (Gnu Compiler Collection) which is part of build-essential package , do that by running:
sudo apt-get install build-essential
Then you can test if you have it installed by running gcc . If you you see error like Fatal error: file not provided (not sure exact error message, but should be something similar), that means you have compiler ready.
And for editing your Code, you can use already available Gedit, just search for it in Dash.
Now following is the syntax to compile your C source file, run following where your file is:
gcc MyProgram.c -o MyProgram
Where, switch -o is optional, but provided to mention name of Binary file which should be created out of your source.
Then simply run ./MyProgram to run your binary.
Note that pthread.h as you mentioned (POSIX Thread) should be available by default with GCC, so simply including it in your C file will do that job, in case it’s not available, a simple Google search should help. 😉
Too long, didn’t read? check this. 😀