-lpthread and -pthread option when compiling the pthread code - pthreads

I've tried to compile the simple pthreading code using g++.
To the best of my knowledge, I should use the -lpthread but it cannot make the executable.
Below is the error code (it seems that libpthread doesn't linked):
undefined reference to `pthread_create'
However, interestingly, when I compile with the option -pthread it is correctly compiled and completely working well.
My g++ version is gcc version 5.2.1 20151010 (Ubuntu 5.2.1-22ubuntu2).

the -l option indicates the compiler to link the program with the named library, so -lpthread means to link the pthread library.
-pthread is an option that's required to build
threaded programs in gcc.
-pthread is preferred if available.

Related

Why -pthread working but -lpthread does not?

I have successfully linked my simple gtest test with command
g++ -o build/test1 build/test1.o -pthread -lgtest -lgtest_main
but i'm curios about options -pthread. Why it is not working with -lpthread as
it working with -lgtest.
And why -gtest doest not working but -pthread without "l" is working...
I have successfully linked my simple gtest test with command g++ -o
build/test1 build/test1.o -pthread -lgtest -lgtest_main
but i'm curios about options -pthread. Why it is not working with
-lpthread as it working with -lgtest.
The order of library link options on the command line is significant. I take it that you tried simply changing -pthread to -lpthread in place, to get this:
g++ -o build/test1 build/test1.o -lpthread -lgtest -lgtest_main
But if the gtest or gtest_main library uses any pthreads functions, then -lpthread needs to appear later in the library list than those (libraries can be listed more than once if necessary):
g++ -o build/test1 build/test1.o -lgtest -lgtest_main -lpthread
Although that's not the appropriate way to build pthreads code with g++ (as described in your other answer), it typically does work.
And why -gtest doest not working but -pthread without "l" is
working...
-pthread is a specific compilation option recognized by the GCC suite of compilers. -lgtest is a combination of the general -l option with gtest as an option argument. One can also write the analogous -lpthread option, which, in principle, has a somewhat different meaning than -pthread.
In gcc, the -pthread flag is an indication to the compiler that it should set up things to allow for threaded code. I believe (but I'm not absolutely sure) that one of the things it does is add -lpthread so that the linker will use the relevant libraries when searching for unresolved symbols.
However, it also does other things, like set -D_REENTRANT to specify the use of re-entrant code.
In other words, -lpthread may not be enough on its own, since it only specifies that the threading library should be searched. I tend to use both to be certain that it does the right thing - yes, I am paranoid :-)

Adding run-time shared library search-path to executable at compile-time | clang | Ubuntu

An executable I'm compiling needs the rpath to a library file at runtime. Currently, I'm compiling the executable with,
clang -O3 -mllvm -polly -mllvm -polly-target=gpu vector_add.c -lGPURuntime -ldl
And then using either of the following methods to provide the rpath,
Adding it to LD_LIBRARY_PATH
Using patchelf --set-rpath $RPATH a.out
I need a method to indicate the rpath in the clang .... command itself.
I'm running clang5.0.0-svn(7cf8dd5ce168bed45b57e019149e33300c56f94b) and llvm-svn(85f508cd9dba8a982471d98c4f649fb0d63f3451) with ld.gold in Ubuntu 14.04 x86_64.
Thank You !
Use clang ... -Wl,-rpath,/path/to/run-time/library's/dir/. It's a gcc style option that works in clang too.

How to compile C into a universal library that works with iOS architectures

I'm trying to compile a very small amount of C source into a library that I can use in XCode for an iOS application.
The files are a single .c file and three header files. I'm very new to C, and no matter what I try I can't seem to get them to compile into a library that supports iOS architectures.
The files depend on the openssl library, and I've got that installed and working fine.
I just need to know the process of compiling these four files into a single library. I've found a plethora of information on the subject online, but I can't decipher which parts are necessary for what I'm trying to do.
I've tried the following:
gcc -fPIC -c main.c
gcc -shared -o mylib.so main.o -lcrypto -lssl
which seems to compile it for x86_64, (I've not tested the resulting file, just checked it's arch).
I've also tried
./configure --disable-shared --enable-utf8 --host=arm-apple-darwin CFLAGS="-arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.1.sdk" CXXFLAGS="-arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.1.sdk" LDFLAGS="-L." CC="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc" CXX="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++"
which only gives me errors
checking for arm-apple-darwin-gcc... no
checking for gcc... gcc
checking whether the C compiler works... no
configure: error: in `/Users/nathanf/compilec':
configure: error: C compiler cannot create executables
I've been scouring the internet for the last few days trying to figure this out, but it's so convoluted to cross compile just a few files and I'm frustrated so I came here for some input.
Any help would be greatly appreciated.

MinGW gfortran LAPACK dgsev

I want to use the LAPACK library in my fortran code. I am compiling using gfortran and want to call the dgesv function from the lapack library.
I downloaded lapack3.5.0 and built it using Cmake which also tests it. The build for lapack3.5.0 has created the following libraries: libblas.a, liblapack.a, libtmglib.a
I am using a make file to compile my code using gfortran.
How do I use these libraries with gfortran compiler, I get error of dgsev being undefined when I compile. I have added the path to these libraries to the system path.
I have no clue what to do to use external libraries in gfortran.
You have to use -L option of gfortran linker. Using this you will point the compiler to look for libraries in this path.
For example:
gfortran test.f -L/path/to/libs -llapack -lblas
If you have additional libraries you can setup like this
LLIBS = -L/path/to/libs -llapack -lblas
XLIBS = -L/usr/X11R6/lib64 -lX11 -lpthread
GLIBS = -L/usr/X11R6/lib64 -lGLU -lGL -lX11 -lXext -lpthread
OBJS = test.o
gfortran $(OBJS) $(LLIBS) $(XLIBS) $(GLIBS)
There is no need to add anything to system path.

Pass "-stdlib=libc++" to c++ linker with autotools

I'm trying to compile ImageMagick (in particular Magick++) with clang++ and libc++. Therefore I need to pass -stdlib=libc++ to both compiler and linker.
While CXXFLAGS="-stdlib=libc++" works fine for compiling, LDFLAGS="-stdlib=libc++" seems to only affect the C linker.
How to correctly tell ./configure or make to use this flag when linking C++ libraries?
Use both — CXXFLAGS for the C++ compiler and LDFLAGS for the linker:
./configure CXXFLAGS="-O2 -g -stdlib=libc++" LDFLAGS="-stdlib=libc++"
You could try:
LDFLAGS="-Wl,-stdlib=libc++"

Resources