Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/usr/bin/ld: newspost.c:(.text+0x6f): undefined reference to `pthread_rwlock_unlock' solved with other Makefile setting lpthread / pthread #1

Open
sanderjo opened this issue Jun 20, 2019 · 1 comment

Comments

@sanderjo
Copy link

sanderjo commented Jun 20, 2019

TLDR:

sed -i 's/lpthread/pthread/g' Makefile

Long

Problems make-ing on Ubuntu Linux:

sander@witte:~/git/newspost$ make
make main CFLAGS="-O2 -Wall" LIBS="-lpthread"
make[1]: Entering directory '/home/sander/git/newspost'
cd base ; make CC="gcc" CFLAGS="-O2 -Wall"
make[2]: Entering directory '/home/sander/git/newspost/base'
gcc -O2 -Wall -o test test.c
./test
rm -f test test.o
gcc -O2 -Wall   -c -o encode.o encode.c
gcc -O2 -Wall   -c -o nntp.o nntp.c
gcc -O2 -Wall   -c -o newspost.o newspost.c
gcc -O2 -Wall   -c -o socket.o socket.c
gcc -O2 -Wall   -c -o queue.o queue.c
gcc -O2 -Wall   -c -o utils.o utils.c
make[2]: Leaving directory '/home/sander/git/newspost/base'
cd ui ; make CC="gcc" CFLAGS="-O2 -Wall"
make[2]: Entering directory '/home/sander/git/newspost/ui'
gcc -O2 -Wall   -c -o main.o main.c
main.c: In function ‘main’:
main.c:166:5: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]
     system(command->data);
     ^~~~~~~~~~~~~~~~~~~~~
main.c:187:7: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]
       system(command->data);
       ^~~~~~~~~~~~~~~~~~~~~
main.c:230:4: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]
    system(command->data);
    ^~~~~~~~~~~~~~~~~~~~~
main.c: In function ‘signal_handler’:
main.c:304:4: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]
    system(command->data);
    ^~~~~~~~~~~~~~~~~~~~~
gcc -O2 -Wall   -c -o options.o options.c
gcc -O2 -Wall   -c -o ui.o ui.c
make[2]: Leaving directory '/home/sander/git/newspost/ui'
cd enc ; make CC="gcc" CFLAGS="-O2 -Wall"
make[2]: Entering directory '/home/sander/git/newspost/enc'
gcc -O2 -Wall   -c -o uuencode.o uuencode.c
gcc -O2 -Wall   -c -o yencode.o yencode.c
make[2]: Leaving directory '/home/sander/git/newspost/enc'
cd cksfv ; make CC="gcc" CFLAGS="-O2 -Wall"
make[2]: Entering directory '/home/sander/git/newspost/cksfv'
gcc -O2 -Wall   -c -o crc32.o crc32.c
gcc -O2 -Wall   -c -o newsfv.o newsfv.c
make[2]: Leaving directory '/home/sander/git/newspost/cksfv'
cd parchive ; make CC="gcc" CFLAGS="-O2 -Wall"
make[2]: Entering directory '/home/sander/git/newspost/parchive'
gcc -O2 -Wall   -c -o makepar.o makepar.c
gcc -O2 -Wall   -c -o rwpar.o rwpar.c
gcc -O2 -Wall   -c -o rs.o rs.c
gcc -O2 -Wall   -c -o md5.o md5.c
gcc -O2 -Wall   -c -o fileops.o fileops.c
gcc -O2 -Wall   -c -o backend.o backend.c
make[2]: Leaving directory '/home/sander/git/newspost/parchive'
gcc -o newspost -lpthread base/*.o ui/*.o enc/*.o cksfv/*.o \
	parchive/*.o
/usr/bin/ld: base/newspost.o: in function `poster_thread':
newspost.c:(.text+0x5f): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: newspost.c:(.text+0x6f): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: newspost.c:(.text+0x14b): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: newspost.c:(.text+0x15b): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: newspost.c:(.text+0x2de): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: newspost.c:(.text+0x309): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: newspost.c:(.text+0x3c0): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: newspost.c:(.text+0x3d0): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: base/newspost.o: in function `newspost':
newspost.c:(.text+0x7cb): undefined reference to `pthread_rwlock_init'
/usr/bin/ld: newspost.c:(.text+0x7f9): undefined reference to `pthread_create'
/usr/bin/ld: newspost.c:(.text+0x9c8): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: newspost.c:(.text+0x9d8): undefined reference to `pthread_join'
/usr/bin/ld: newspost.c:(.text+0x9e6): undefined reference to `pthread_rwlock_rdlock'
/usr/bin/ld: newspost.c:(.text+0x9f7): undefined reference to `pthread_cancel'
/usr/bin/ld: newspost.c:(.text+0xa2d): undefined reference to `pthread_rwlock_destroy'
/usr/bin/ld: newspost.c:(.text+0xaab): undefined reference to `pthread_rwlock_init'
/usr/bin/ld: newspost.c:(.text+0xbf3): undefined reference to `pthread_rwlock_destroy'
/usr/bin/ld: base/nntp.o: in function `nntp_post':
nntp.c:(.text+0x4d4): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: nntp.c:(.text+0x4e1): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: nntp.c:(.text+0x539): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: nntp.c:(.text+0x542): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: base/utils.o: in function `file_entry_free':
utils.c:(.text+0x338): undefined reference to `pthread_rwlock_destroy'
/usr/bin/ld: ui/options.o: in function `parse_input_files':
options.c:(.text+0x146): undefined reference to `pthread_rwlock_init'
/usr/bin/ld: ui/ui.o: in function `ui_post_start':
ui.c:(.text+0x6c4): undefined reference to `pthread_rwlock_init'
/usr/bin/ld: ui/ui.o: in function `ui_chunk_posted':
ui.c:(.text+0x1188): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: ui.c:(.text+0x11a2): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: ui/ui.o: in function `ui_posting_part_done':
ui.c:(.text+0x1215): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: ui.c:(.text+0x1228): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: ui/ui.o: in function `ui_post_done':
ui.c:(.text+0x13d4): undefined reference to `pthread_rwlock_destroy'
collect2: error: ld returned 1 exit status
make[1]: *** [Makefile:33: main] Error 1
make[1]: Leaving directory '/home/sander/git/newspost'
make: *** [Makefile:43: opt] Error 2
sander@witte:~/git/newspost$ 

So:

sander@witte:~/git/newspost$ gcc -o newspost -lpthread base/*.o ui/*.o enc/*.o cksfv/*.o parchive/*.o
/usr/bin/ld: base/newspost.o: in function `poster_thread':
newspost.c:(.text+0x5f): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: newspost.c:(.text+0x6f): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: newspost.c:(.text+0x14b): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: newspost.c:(.text+0x15b): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: newspost.c:(.text+0x2de): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: newspost.c:(.text+0x309): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: newspost.c:(.text+0x3c0): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: newspost.c:(.text+0x3d0): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: base/newspost.o: in function `newspost':
newspost.c:(.text+0x7cb): undefined reference to `pthread_rwlock_init'
/usr/bin/ld: newspost.c:(.text+0x7f9): undefined reference to `pthread_create'
/usr/bin/ld: newspost.c:(.text+0x9c8): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: newspost.c:(.text+0x9d8): undefined reference to `pthread_join'
/usr/bin/ld: newspost.c:(.text+0x9e6): undefined reference to `pthread_rwlock_rdlock'
/usr/bin/ld: newspost.c:(.text+0x9f7): undefined reference to `pthread_cancel'
/usr/bin/ld: newspost.c:(.text+0xa2d): undefined reference to `pthread_rwlock_destroy'
/usr/bin/ld: newspost.c:(.text+0xaab): undefined reference to `pthread_rwlock_init'
/usr/bin/ld: newspost.c:(.text+0xbf3): undefined reference to `pthread_rwlock_destroy'
/usr/bin/ld: base/nntp.o: in function `nntp_post':
nntp.c:(.text+0x4d4): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: nntp.c:(.text+0x4e1): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: nntp.c:(.text+0x539): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: nntp.c:(.text+0x542): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: base/utils.o: in function `file_entry_free':
utils.c:(.text+0x338): undefined reference to `pthread_rwlock_destroy'
/usr/bin/ld: ui/options.o: in function `parse_input_files':
options.c:(.text+0x146): undefined reference to `pthread_rwlock_init'
/usr/bin/ld: ui/ui.o: in function `ui_post_start':
ui.c:(.text+0x6c4): undefined reference to `pthread_rwlock_init'
/usr/bin/ld: ui/ui.o: in function `ui_chunk_posted':
ui.c:(.text+0x1188): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: ui.c:(.text+0x11a2): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: ui/ui.o: in function `ui_posting_part_done':
ui.c:(.text+0x1215): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: ui.c:(.text+0x1228): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: ui/ui.o: in function `ui_post_done':
ui.c:(.text+0x13d4): undefined reference to `pthread_rwlock_destroy'
collect2: error: ld returned 1 exit status
sander@witte:~/git/newspost$ 
sander@witte:~/git/newspost$ 

Let's try with -pthread instad of -lpthread:

sander@witte:~/git/newspost$ gcc -o newspost -pthread base/*.o ui/*.o enc/*.o cksfv/*.o parchive/*.o
sander@witte:~/git/newspost$ 
sander@witte:~/git/newspost$ 

Hey ... that works?!

ander@witte:~/git/newspost$ ./newspost 

Newspost version 2.2.1
Copyright (C) 2001 - 2010 Jim Faulkner
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Usage: newspost [OPTIONS [ARGUMENTS]] file1 file2 file3...
Options:
  --host             -i   <string> - hostname or IP of the news server
  --port             -z   <int>    - port number on the news server
  --user             -u   <string> - username on the news server
  --password         -p   <string> - password on the news server
  --threads          -N   <int>    - amount of threads to use for posting

So:

ander@witte:~/git/newspost$ diff Makefile.org Makefile
3c3
< OPT_LIBS = -lpthread
---
> OPT_LIBS = -pthread

Let's try again:

sander@witte:~/git/newspost$ make clean
rm -rf newspost newspost.exe core *~
cd base ; make clean
make[1]: Entering directory '/home/sander/git/newspost/base'
rm -f test *.o *~
make[1]: Leaving directory '/home/sander/git/newspost/base'
cd ui ; make clean
make[1]: Entering directory '/home/sander/git/newspost/ui'
rm -f *.o *~
make[1]: Leaving directory '/home/sander/git/newspost/ui'
cd enc ; make clean
make[1]: Entering directory '/home/sander/git/newspost/enc'
rm -f *.o *~
make[1]: Leaving directory '/home/sander/git/newspost/enc'
cd cksfv ; make clean
make[1]: Entering directory '/home/sander/git/newspost/cksfv'
rm -f *.o *~
make[1]: Leaving directory '/home/sander/git/newspost/cksfv'
cd parchive ; make clean
make[1]: Entering directory '/home/sander/git/newspost/parchive'
rm -f *.o *~
make[1]: Leaving directory '/home/sander/git/newspost/parchive'

and try again:

sander@witte:~/git/newspost$ make
make main CFLAGS="-O2 -Wall" LIBS="-pthread"
make[1]: Entering directory '/home/sander/git/newspost'
cd base ; make CC="gcc" CFLAGS="-O2 -Wall"
make[2]: Entering directory '/home/sander/git/newspost/base'
gcc -O2 -Wall -o test test.c
./test
rm -f test test.o
gcc -O2 -Wall   -c -o encode.o encode.c
gcc -O2 -Wall   -c -o nntp.o nntp.c
gcc -O2 -Wall   -c -o newspost.o newspost.c
gcc -O2 -Wall   -c -o socket.o socket.c
gcc -O2 -Wall   -c -o queue.o queue.c
gcc -O2 -Wall   -c -o utils.o utils.c
make[2]: Leaving directory '/home/sander/git/newspost/base'
cd ui ; make CC="gcc" CFLAGS="-O2 -Wall"
make[2]: Entering directory '/home/sander/git/newspost/ui'
gcc -O2 -Wall   -c -o main.o main.c
main.c: In function ‘main’:
main.c:166:5: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]
     system(command->data);
     ^~~~~~~~~~~~~~~~~~~~~
main.c:187:7: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]
       system(command->data);
       ^~~~~~~~~~~~~~~~~~~~~
main.c:230:4: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]
    system(command->data);
    ^~~~~~~~~~~~~~~~~~~~~
main.c: In function ‘signal_handler’:
main.c:304:4: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]
    system(command->data);
    ^~~~~~~~~~~~~~~~~~~~~
gcc -O2 -Wall   -c -o options.o options.c
gcc -O2 -Wall   -c -o ui.o ui.c
make[2]: Leaving directory '/home/sander/git/newspost/ui'
cd enc ; make CC="gcc" CFLAGS="-O2 -Wall"
make[2]: Entering directory '/home/sander/git/newspost/enc'
gcc -O2 -Wall   -c -o uuencode.o uuencode.c
gcc -O2 -Wall   -c -o yencode.o yencode.c
make[2]: Leaving directory '/home/sander/git/newspost/enc'
cd cksfv ; make CC="gcc" CFLAGS="-O2 -Wall"
make[2]: Entering directory '/home/sander/git/newspost/cksfv'
gcc -O2 -Wall   -c -o crc32.o crc32.c
gcc -O2 -Wall   -c -o newsfv.o newsfv.c
make[2]: Leaving directory '/home/sander/git/newspost/cksfv'
cd parchive ; make CC="gcc" CFLAGS="-O2 -Wall"
make[2]: Entering directory '/home/sander/git/newspost/parchive'
gcc -O2 -Wall   -c -o makepar.o makepar.c
gcc -O2 -Wall   -c -o rwpar.o rwpar.c
gcc -O2 -Wall   -c -o rs.o rs.c
gcc -O2 -Wall   -c -o md5.o md5.c
gcc -O2 -Wall   -c -o fileops.o fileops.c
gcc -O2 -Wall   -c -o backend.o backend.c
make[2]: Leaving directory '/home/sander/git/newspost/parchive'
gcc -o newspost -pthread base/*.o ui/*.o enc/*.o cksfv/*.o \
	parchive/*.o
make[1]: Leaving directory '/home/sander/git/newspost'
strip newspost
sander@witte:~/git/newspost$ 

Done!!!

@sanderjo
Copy link
Author

sanderjo commented Nov 8, 2020

So: not -lpthread but -pthread, and it works.

https://stackoverflow.com/a/23251828/1492917 tells

-pthread tells the compiler to link in the pthread library as well as configure the compilation for threads.

For example, the following shows the macros that get defined when the -pthread option gets used on the GCC package installed on my Ubuntu machine:

Using the -lpthread option only causes the pthread library to be linked - the pre-defined macros don't get defined.

Bottom line: you should use the -pthread option.

Note: the -pthread option is documented as a platform specific option in the GCC docs, so it might not always be available. However, it is available on platforms that the GCC docs don't explicitly list it for (such as i386 and x86-64) - you should use it when available.

Also note that other similar options have been used by GCC, such as -pthreads (listed as a synonym for -pthread on Solaris 2) and -mthread (for MinGW-specific thread support on i386 and x86-64 Windows). My understanding is that GCC is trying to move to using -pthread uniformly going forward.

So I'll send a PR for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant