Why -pthread working but -lpthread does not? - hyperlink

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 :-)

Related

Is it possible to pass an argument to wasm-opt with clang

I'm trying to compile a wasm binary with multivalue and O3, however, I keep getting [wasm-validator error in module] unexpected false: Imported multivalue function requires multivalue [--enable-multivalue].
The current flags that I'm using are --target=wasm32-unknown-unknown-wasm -O3 -nostdlib -funroll-loops -Wall -Wno-comment -mllvm -polly -mmultivalue -Xclang -target-abi -Xclang experimental-mv -ffunction-sections -fdata-sections -flto -s -Wl,--export-dynamic,--allow-undefined,--gc-sections.
No, sadly there is no way (today) to modify the wasm-opt command line that clang uses.
This seems like a bug in llvm and really the target-features section of binary should contains multivalue, which should then enable it automatically in wasm-opt. Could you open an llvm bug about this?
For now your best bet might be to run clang without wasm-opt in your PATH.

Convert LLVM IR to higher optimization level

I have compiled the C code to LLVM IR code with -O0 optimization.
How can I convert this -O0 LLVM IR code to the -O3 LLVM IR code "without the C code"?
I have tried below:
clang -O3 -S -emit-llvm O0.ll -o O3.ll
and
opt -O3 -S O0.ll -o O3.ll
but the output is still -O0 level.
Thank you.
I'm not sure when the change happened (I think it's LLVM 3.9.0 and on), but when you compile to bitcode functions get annotated with the optnone attribute and further optimizations are not performed.
Have a look for a relevant SO discussion here.
What is suggested is to do this:
clang -emit-llvm -O1 -mllvm -disable-llvm-optzns -disable-llvm-passes foo.c -o foo.bc
For LLVM 3.8.0 and earlier (I think) what you have already been doing is enough.
So, once you obtain that bitcode (without the optnone) argument, you can use opt as you've already been doing.

-lpthread and -pthread option when compiling the pthread code

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.

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.

Armadillo Wrapper Linker errors

When using some functions in the Armadillo Algebra package I get the following errors:
armadillo_bits/atlas_wrapper.hpp:188: undefined reference to `clapack_dgetrf'
I have linked like this:
arm-linux-gnueabihf-g++ -march=armv7-a -mthumb-interwork -mfloat-abi=hard
-mfpu=neon
-mtune=cortex-a9 --sysroot=/home/mg/yocto/build/tmp/sysroots/socfpga_cyclone5
-DHAVE_CONFIG_H -I. -I.. --sysroot=/home/mg/yocto/build/tmp/sysroots
/socfpga_cyclone5 -g -O2 -L/opt/altera-linux/linaro/gcc-linaro-arm-linux-gnueabihf-
4.7-2012.11-20121123_linux/arm-linux-gnueabihf/lib -I/opt/altera-linux/linaro
/gcc-linaro-arm-linux-gnueabihf-4.7-2012.11-20121123_linux/arm-linux-gnueabihf
/include -llapack -lf2c -lblas -lm --sysroot=/home/mg/yocto/build/tmp/sysroots
/socfpga_cyclone5 -MT AlgoLibTests.o -MD -MP -MF .deps/AlgoLibTests.Tpo -c -o
AlgoLibTests.o AlgoLibTests.cpp
I followed the instructions here and everything went fine:
Cross-Compiling Armadillo Linear Algebra Library
Any Ideas?
It also took me some days to be able to install armadillo for my Android development. So I want to shared with you the experience to solve your problem.
The error undefined reference to `clapack_dgetrf' is a linking error. This function should be contained in one of these libraries -llapack -lf2c -lblas. Of course it is clapack, so it should be in the lapack library. Use the command nm to check if the lapack library (liblapack.a or liblapack.so) contains this symbol. I am sure you will find out the solution then. Hope it helps.

Resources