Some pthread_ function can't be found in by gollvm

78 views
Skip to first unread message

JX Zhang

unread,
Apr 6, 2021, 9:15:25 AM4/6/21
to golang-nuts
I met some problem about pthread.
I wrote a cgo demo which use some pthread_* function, but it compile failed

ccode.c
```
# include <stdio.h>
# include "ccode.h"
# include <pthread.h>

int try_pthread(){
pthread_t id;
int ret, i = 0;
ret = pthread_create(&id,NULL,(void *)thread,NULL);
for(i=0;i<=5;i++)
printf("This is main thread %d\n",i);
pthread_join(id,NULL);
pthread_once(id, NULL);
return 0;
}

void thread()
{
int i=0;
for(i=0;i<=5;i++)
printf("this is thread %d\n",i);
}
```

main.go
```
package main

/*
#cgo CFLAGS: -I./
#cgo LDFLAGS: -L./
#include "ccode.h"
*/
import "C"

func main() {
_ = C.try_pthread()
}
```

the `pthread_create` can be searched, but the other two functions report this:
ccode.c:11: error: undefined reference to 'pthread_join'
ccode.c:12: error: undefined reference to 'pthread_once'



JX Zhang

unread,
Apr 6, 2021, 9:20:48 AM4/6/21
to golang-nuts
 ✘~/workspace/gowork/src/pthread $  go build .
# pthread
ccode.c:12:15: warning: incompatible integer to pointer conversion passing 'pthread_t' (aka 'unsigned long') to parameter of type 'pthread_once_t *' (aka 'int *') [-Wint-conversion]
/usr/include/pthread.h:495:42: note: passing argument to parameter '__once_control' here
ccode.c:12:23: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
# pthread
ccode.c:11: error: undefined reference to 'pthread_join'
ccode.c:12: error: undefined reference to 'pthread_once'
/usr/lib/gcc/x86_64-linux-gnu/8/libgcc.a(generic-morestack-thread.o):function __wrap_pthread_create: error: undefined reference to 'pthread_once'

--------------------------------
Make  the pthread_join and pthread_once as comments and recompile the file
```bash
~/workspace/gowork/src/pthread $ ldd main
linux-vdso.so.1 (0x00007ffc58db4000)
libgo.so.13git => /home/jxzhang/.local/gollvm/lib64/libgo.so.13git (0x00007fc599db0000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fc599a12000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fc5997fa000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc599409000)
/lib64/ld-linux-x86-64.so.2 (0x00007fc59b6cb000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc5991ea000)
 ~/workspace/gowork/src/pthread $ nm /home/jxzhang/.local/gollvm/lib64/libgo.so.13git | grep -E "pthread_create|pthread_join|pthread_once"
                 w pthread_create
                 w pthread_once
0000000001089d70 t __wrap_pthread_create
 ~/workspace/gowork/src/pthread $ nm /lib/x86_64-linux-gnu/libpthread.so.0 | grep -E "pthread_create|pthread_join|pthread_once"
000000000000f9b0 t __GI___pthread_once
00000000000079b0 t __pthread_create_2_1
00000000000079b0 T pthread_create@@GLIBC_2.2.5
0000000000008b50 t __pthread_join
0000000000008b50 W pthread_join
000000000000f9b0 T __pthread_once
000000000000f9b0 W pthread_once
000000000000f850 t __pthread_once_slow
```

JX Zhang

unread,
Apr 6, 2021, 10:54:39 PM4/6/21
to golang-nuts
Has anyone ever encountered this problem?
Reply all
Reply to author
Forward
0 new messages