1

I'm using homebrew-riscv toolchain on mac machine.

I want to compile a simple multithreading program that is written using pthread library in C using riscv gnu cross compiler. So that, I've used the below command:

riscv64-unknown-elf-gcc -march=rv32i -mabi=ilp32 pthreadExample.c -o pthreadExample -lpthread

And I've got the following warning and error:

warning: implicit declaration of function 'pthread_create' [-Wimplicit-function-declaration]
   32 |         pthread_create(&tid, NULL, myThreadFun, (void *)&tid);
      |         ^~~~~~~~~~~~~~
pthreadExample.c:34:5: warning: implicit declaration of function 'pthread_exit' [-Wimplicit-function-declaration]
   34 |     pthread_exit(NULL);
      |     ^~~~~~~~~~~~
/opt/homebrew/Cellar/riscv-gnu-toolchain/master/lib/gcc/riscv64-unknown-elf/11.1.0/../../../../riscv64-unknown-elf/bin/ld: cannot find -lpthread
collect2: error: ld returned 1 exit status

Does anyone have any ideas for solving this error? Or should I use another option instead of -lpthread for compiling?

Thanks in advance

Sooora
  • 171
  • 9
  • That's a compilation error (-lpthread is a linker command). Try including `` in that source. – Erik Eidt Jan 06 '22 at 15:49
  • @ErikEidt thanks for your comment. `````` is already included in the source code. – Sooora Jan 06 '22 at 16:05
  • Try `-pthread`. See: [Significance of -pthread flag when compiling](https://stackoverflow.com/questions/2127797/significance-of-pthread-flag-when-compiling). – Erik Eidt Jan 06 '22 at 18:06
  • @ErikEidt I've already tried it it led into this error: ```riscv64-unknown-elf-gcc: error: unrecognized command-line option '-pthread'``` – Sooora Jan 06 '22 at 18:17

1 Answers1

1

you are using the bare metal cross compiler, what you need is:

riscv64-unknown-linux-gnu-gcc

additionally, you are using the -march=rv32i flag, but you are using the 64 bit version of the compiler, that doesn't seem correct to me. If the target machine is a 32 bit one, then perhaps you should use the riscv32-unknown-linux-gnu-gcc

vfiskewl
  • 70
  • 7
  • Thanks for your response. Actually, I installed the risc v cross compiler for multilib which also supports 32bit architecture. – Sooora Jan 11 '22 at 16:12
  • 1
    OK that makes sense about the march part but the main point is that you are using the bare metal cross compiler : `riscv64-unknown-elf-gcc` which means you are compiling for a target that is not running an OS, hence no pthreads. If you switch to the `riscv64-unknown-linux-gnu-gcc` you should be able to use the -pthread. – vfiskewl Jan 11 '22 at 20:47