-1

I'm facing an issue that I'm not sure why is happening or how to solve it: My problem is that I'm trying to compile inferno-os (A distributed OS) on a Virtual Machine, I've had some issues on the process like some libraries not being installed or GCC not being able to perform certain tasks (Inferno-OS is not particularly new), but I've found a way of solving that, all that is mainly because a part of the software is meant to run only on 32-bit mode only (You can check it here), and since I'm using a 64-bit VM (Xubuntu22.04), I did that. Anyway, now, I'm facing an error because of pthread_yield function, basically, it said undefined reference when I was building the OS, so I decided to try the example I found here and it stills give me the undefined reference problem.

I have a file named thread.c where I've got the example I found, and I've tried to get the binary from it with:

cc -m32 thread.c -lpthread
cc -m32 -pthread thread.c

and

cc -m32 thread.c -lthread

but all of those give me the same message "undefined reference to pthread_yield"

Here is a picture of the command that inferno's installation process executes, note that it's in Spanish but the output says basically undefined reference to pthread_yield [Inferno run]

I would really appreciate some help here, I would really like to know why is this happening

Y34x4t3
  • 19
  • 7
  • 1
    I'm uncertain whether it would resolve your issue, but the compile command presented shows explicitly linking against libpthread and *not* using the `-pthread` option, which is contrary to the docs. With GCC, compilation of a pthreads-based program should do the opposite: use `-pthread` (not `-lpthread`), for both compiling and linking, and omit `-lpthread`. – John Bollinger Aug 16 '22 at 14:02
  • 1
    Note also that `pthread_yield()` was never standard and is now deprecated by glibc (the standard alternative is `sched_yield()`). Perhaps that's a factor. – John Bollinger Aug 16 '22 at 14:08
  • Thanks man, as I said before, I also tried compiling a program using the `-pthread` flag but it makes no difference, there is more info [here](https://stackoverflow.com/questions/23250863/difference-between-pthread-and-lpthread-while-compiling) if you want to go and check it out. Anyways I think I assumed the `pthread_yield` function was included with the glibc and now I realize It isn't, thanks for the advice :D – Y34x4t3 Aug 17 '22 at 23:57

1 Answers1

2

You shouldn't ignore compiler warnings, especially those about implicit function declarations. The warning tells you that pthread_yield is an undeclared function. Either build with -D_GNU_SOURCE to get a declaration of pthread_yield, or include <sched.h> and change pthread_yield to the standard sched_yield function.

Florian Weimer
  • 32,022
  • 3
  • 48
  • 92
  • The post describes an "undefined reference" link error, not a warning about calling an implicitly declared function. And GCC will define `_GNU_SOURCE` automatically unless one of its strict(ish) standards modes, such as `-std=c17`, is turned on, which does not appear to be the case for the OP. – John Bollinger Aug 18 '22 at 00:12
  • Current glibc provides a *definition* of `pthread_yield` only with `_GNU_SOURCE`, which leads to a compiler warning and a linker error if missing. The GNU C compiler does not default to `_GNU_SOURCE` in any mode, unlike the C++ compiler. – Florian Weimer Aug 18 '22 at 04:18
  • I can confirm that `-D_GNU_SOURCE` solved my issue! Thanks a lot! – tobiasBora Dec 01 '22 at 02:02