sem_open() error: “undefined reference to sem_open()” on linux (Ubuntu 10.10)

后端 未结 3 1280
旧时难觅i
旧时难觅i 2020-12-20 12:11

So I am getting the error: \"undefined reference to sem_open()\" even though I have include the semaphore.h header. The same thing is happening for all my pthread function

相关标签:
3条回答
  • 2020-12-20 12:41

    Including the header does not tell ld about the library. You need to add -lrt to your compilation command line. For threading, you need either -lpthread or -pthread, depending on your platform.

    The library is not the header. The header is not the library. This is an important distinction. See What's the difference between a header file and a library?

    0 讨论(0)
  • 2020-12-20 12:42

    You need link with pthread lib, using -lpthread option.

    0 讨论(0)
  • 2020-12-20 12:53

    The working option in Ubuntu is -lpthread. But if you work on suse or other systems the correct option is -lrt. Also the book Linux Programmin Interface mentions -lrt as the correct option.

    0 讨论(0)
提交回复
热议问题