Coding⏱️ 2 min read📅 2026-06-03

How to Fix: C++ error: undefined reference to 'clock_gettime' and 'clock_settime'

Undefined reference to clock_gettime and clock_settime in C++ on Ubuntu.

Quick Answer: The issue is likely due to the missing link against the libc library, which provides the clock_gettime function. Add -lc to your compiler flags to resolve the error.

The error you are encountering, "undefined reference to 'clock_gettime' and 'clock_settime'," is due to the fact that these functions are part of the POSIX standard, which is not enabled by default on Ubuntu.

🚀 How to Resolve This Issue

Method 1: Enabling POSIX Features

  1. Step 1: Open your terminal and run the command sudo apt-get install libstdc++6-dev to enable POSIX features.

Method 2: Using a Different Compiler

  1. Step 1: Open your terminal and run the command sudo apt-get install gcc-9-dev to install the GNU C++ compiler version 9.

💡 Conclusion

By following these steps, you should be able to resolve the error and successfully compile your C++ program using the clock_gettime function.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions