CLOCK_REALTIME Is Undefined In Visual Studio But Can Run in Linux Target

Yik Heng Yong 0 Reputation points
2025-01-15T07:03:40.5366667+00:00

Question:

Aside from just adding the error to the ignore list in Visual Studio, is there a way to get Visual Studio to recognize POSIX-related functions?

Visual Studio Version: Community 2022 (64-bit) v17.12.3

Workload: Linux development with C++

Code Snippet:

#include <stdio.h>
#include <time.h>
int main()
{
    struct timespec ts;
    double time;
    
    clock_gettime(CLOCK_REALTIME, &ts);
    time = ts.tv_sec + (ts.tv_nsec / 1e9);
    printf("Time:%f\n", time);
    return 0;
}

Error Code:Error Code

Linux Terminal Result:

pi@bpi-iot-ros-ai:~/projects/ConsoleApplication1$ ./main
Time:1736924024.770033

Troubleshooting Steps That Taken By Myself:

I tried to Update but the error persisted.

Update

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,447 questions
Visual Studio Extensions
Visual Studio Extensions
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Extensions: A program or program module that adds functionality to or extends the effectiveness of a program.
247 questions
Visual Studio Setup
Visual Studio Setup
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Setup: The procedures involved in preparing a software program or application to operate within a computer or mobile device.
1,095 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Yik Heng Yong 0 Reputation points
    2025-02-10T06:39:11.52+00:00

    The issue is resolved by insert #define _POSIX_C_SOURCE 199309L.

    Reference: https://developercommunity.visualstudio.com/t/CLOCK_REALTIME-Is-Undefined-In-Visual-St/10843559

    0 comments No comments

  2. Yik Heng Yong 0 Reputation points
    2025-02-10T06:41:16.8666667+00:00

    The issue is resolved by adding #define _POSIX_C_SOURCE 199309L.

    Reference: https://developercommunity.visualstudio.com/t/CLOCK_REALTIME-Is-Undefined-In-Visual-St/10843559

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.