Undefined Reference to `pthread_init' When Using -lpthread Flag: - linux

I'm compiling using:
gcc -o outfile infile.c -lpthread
and the only undefined reference is pthread_init.
I have tried using -pthread instead of -lpthread, as per some suggestions online.
I am including <pthread.h>
Any ideas?

Don't call pthread_init(). It's not necessary on Linux.

Related

HEXAGON Halide Tools 2.3: /usr/bin/ld: cannot find -lc++abi

I did not get response from Qualcomm forum so I decided to post here. When I was trying to run examples of Halide for Hexagon by running make run as written in the document. Then I got the following issue. The -lc++abi is missing.
clang++ -std=c++11 -I /opt/qcom/Hexagon_SDK/4.3.0.0/tools/HALIDE_Tools/2.3.03/Halide/include -stdlib=libc++ -O3 -g -fno-rtti -rdynamic conv3x3_generator.cpp /opt/qcom/Hexagon_SDK/4.3.0.0/tools/HALIDE_Tools/2.3.03/Halide/lib/libHalide.a /opt/qcom/Hexagon_SDK/4.3.0.0/tools/HALIDE_Tools/2.3.03/Halide/tools/GenGen.cpp -o /opt/qcom/Hexagon_SDK/4.3.0.0/tools/HALIDE_Tools/2.3.03/Halide/Examples/build/offload/hexagon_benchmarks/bin/conv3x3.generator -lz -lrt -ldl -lpthread -lm
/usr/bin/ld: cannot find -lc++abi
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I checked the /usr/lib and find. So it should be there?
./x86_64-linux-gnu/libc++abi.so.1.0
./x86_64-linux-gnu/libc++abi.so.1
./llvm-10/lib/libc++abi.so.1.0
./llvm-10/lib/libc++abi.so.1
Did I miss anything or make anything stupid? Thanks!
ld is your system's host linker -- that's GNU BFD ld. clang++ must be in your PATH but it's the host clang++ and not the hexagon-clang++ that would be with Halide/Hexagon tools.
I contacted the person from Qualcomm. I am not supposed to use either /usr/bin/clang++ nor hexagon-clang++. I downloaded clang+llvm-7.0.1-x86_64-linux-gnu-ubuntu-18.04.tar.xz from https://releases.llvm.org/7.0.1/. You should use clang++ under the bin folder after you extract it. Now it works for me.

GLFW linking, undefined reference to init

So i am trying to compile a simple GLFW window app, and i ran into a linker problem.
gcc -o bin/mtx_gui `pkg-config --libs glfw3 glew` src/main.c
i use this command to compile single main. pkg-config expands into -L/usr/local/lib -lglfw3 -lGLEW -lGLU -lGL which should compile everything. i checked all libs are where they suposed to be. no idea why it is not linking it
main.c:(.text.startup+0x2): undefined reference to `glfwInit'
First, as pointed by G.M in the comment, main.c goes before all libs. second and the most important.
-lglfw3 -lrt -lm -ldl -lpthread -lGL
Libpthread must be also linked, and if you plan on using opengl link Libgl as shown above.

Standalone(static) curl library in linux with just http, https support

I am developing an application in Linux, which requires static library of curl.Only http and https support is required.I have tried many ways like -Wl,-Bstatic -libcurl , linking to libcurl.a etc. Most alarming is that, I haven't seen anybody who has been successful in doing so, in linux.Only thing I found was in Windows,
Linking project with statically build curl
But in linux , almost all answers are like following.
Static linking libcurl using c.
Is it so? Whether it is impossible to make standalone library of curl in linux ?
Following are my build options and results
-Wl,-Bstatic -pthread -lcrypto -Wl,-Bdynamic -ldl -lcurl
No error !!!!!
-Wl,-Bstatic -pthread -lcrypto -ldl -lcurl
errors ... cannot find -lgcc_s
-static -pthread -lcrypto -ldl -lcurl
lot of errors starting with undefined reference "idn_free"
4.-static -pthread -lcrypto -ldl -ldn -lcurl
no change.... lot of errors starting with undefined reference "idn_free"
-static /usr/lib/i386-linux-gnu/libpthread.a /usr/lib/i386-linux-gnu/libcrypto.a /usr/lib/i386-linux-gnu/libdl.a /usr/lib/i386-linux-gnu/libidn.a /usr/lib/i386-linux-gnu/libcurl.a
no change.... lot of errors starting with undefined reference "idn_free"

undefined reference to `ERR_load_crypto_strings'

I am trying to get OpenSSL to work, but it seems to have a problem with linking. Here is what I did:
I downloaded OpenSSL for Linux from https://www.openssl.org/source/ I tried versions 0.9.8zc, 1.0.0o and 1.0.1j, all with the same result.
I installed each OpenSSL version using ./config, make and sudo make install.
For debugging purposes, I went to /usr/lib/ssl and used sudo chmod -R 777 * to remove any restrictions that could have caused the error.
I created the following program:
main.c:
#include <errno.h>
#include <malloc.h>
#include <resolv.h>
#include <openssl/bio.h>
#include <openssl/ssl.h>
int main(void) {
SSL_load_error_strings();
return EXIT_SUCCESS;
}
I created the following makefile in the same directory as my .c file:
makefile:
all: main.o
cc -o main main.o -L/usr/local/ssl/lib/ -lcrypto -lssl
main.o: main.c
cc -c -Wall main.c -I/usr/local/ssl/include/ -o main.o
When I run the makefile, I get the following error:
cc -o main main.o -L/usr/local/ssl/lib/ -lcrypto -lssl
/usr/local/ssl/lib//libssl.a(ssl_err2.o): In function SSL_load_error_strings':
ssl_err2.c:(.text+0x4): undefined reference toERR_load_crypto_strings'
/usr/local/ssl/lib//libssl.a(ssl_err.o): In function ERR_load_SSL_strings':
ssl_err.c:(.text+0xc): undefined reference toERR_func_error_string'
ssl_err.c:(.text+0x28): undefined reference to ERR_load_strings'
ssl_err.c:(.text+0x3c): undefined reference toERR_load_strings'
collect2: ld returned 1 exit status
make: *** [all] Error 1
What am I doing wrong?
Cheers
Alex
As answered on the maillist by scott_n but for the record here, swap the order to -lssl -lcrypto.
Explanation: for static C libraries in general on nearly all systems, members of library files like libxxx.a are only pulled in by the linker if they define things referenced from translation units already linked i.e. to the left in the command line. OpenSSL libssl has (numerous) references to libcrypto. If you link -lcrypto first, those references haven't been seen, so the libcrypto files aren't linked; then you link -lssl and create the unsatisfied references. In cases of mutual dependency also called recursive dependency you may need to repeat a library like -lcrypto -lssl -lcrypto but OpenSSL has no such "backward" references.

std.net.curl linker errors in Linux

I'm trying to use std.net.curl on 32-bit Ubuntu Linux (11.10 Ocelot) using DMD 2.058.
When I build (dmd source.d) I get:
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../../lib/libphobos2.a(curl.o): In function `_D3std3net4curl4Curl19_sharedStaticCtor30FZv':
std/net/curl.d:(.text._D3std3net4curl4Curl19_sharedStaticCtor30FZv+0x6): undefined reference to `curl_global_init'
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../../lib/libphobos2.a(curl.o): In function `_D3std3net4curl4Curl19_sharedStaticDtor31FZv':
std/net/curl.d:(.text._D3std3net4curl4Curl19_sharedStaticDtor31FZv+0x4): undefined reference to `curl_global_cleanup'
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../../lib/libphobos2.a(curl_12fd_140.o): In function `_D3std3net4curl4HTTP4Impl6__dtorMFZv':
std/net/curl.d:(.text._D3std3net4curl4HTTP4Impl6__dtorMFZv+0x12): undefined reference to `curl_slist_free_all'
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../../lib/libphobos2.a(curl_12fd_140.o): In function `_D3std3net4curl4HTTP3dupMFZS3std3net4curl4HTTP':
std/net/curl.d:(.text._D3std3net4curl4HTTP3dupMFZS3std3net4curl4HTTP+0x53): undefined reference to `curl_slist_append'
[snip]
I added -L-lcurl to my command line (full command: dmd source.d -L-lcurl) but I get the exact same result. I have libcurl4-openssl installed. I can build a simple C curl program on this computer without issue (gcc simple.c -lcurl). I'm not sure where to look from here to figure this out.
Update: Here is how the linker is invoked according to dmd -v:
gcc source.o -o source -m32 -lcurl -Xlinker -L/usr/lib -Xlinker -L/usr/lib64 -Xlinker --no-warn-search-mismatch -Xlinker --export-dynamic -lphobos2 -lpthread -lm -lrt
As you yourself discovered, the order of libraries is incorrect. A quote from "An Introduction to GCC": "A library which calls an external function defined in another library should appear before the library containing the function." In your case libphobos calls external library's (libcurl) function(s), so I guess libphobos should go before libcurl.
The book is available online. Chapter related to the OT is here: http://www.network-theory.co.uk/docs/gccintro/gccintro_18.html
The DMD compiler is clearly broken in this respect. I published a patch to fix it (maybe not the best) but the maintainer of the compiler is not convinced this needs to be fixed at all so there is no much discussion about it.
https://github.com/D-Programming-Language/dmd/pull/497
http://d.puremagic.com/issues/show_bug.cgi?id=7044
Seemingly the simplest way to fix the phobos2/curl linker order problem, as mentioned elsewhere in this thread:
dmd source.d -L-lphobos2 -L-lcurl
Works like a charm for me.
I figured out if I obtain the linker command through -v and reorder it so the -lcurl is at the end it links without issue.
gcc source.o -o source -m32 -Xlinker -L/usr/lib -Xlinker -L/usr/lib64 -Xlinker --no-warn-search-mismatch -Xlinker --export-dynamic -lphobos2 -lpthread -lm -lrt -lcurl
I'm still curious if there is a way to fix this without manually running the linker.

Resources