简体   繁体   English

C ++,mingw和clock_gettime无法编译

[英]C++, mingw and clock_gettime doesn't compile

I'm having quite a bit of difficulty compiling some Linux C++ code using g++.exe from minGW. 使用minGW的g ++。exe编译一些Linux C ++代码时遇到了一些困难。 Specifically, it is unable to understand this code: 具体来说,它无法理解此代码:

  struct timespec now;
  clock_gettime(CLOCK_REALTIME,&now);

I have added necessary header file 我添加了必要的头文件

 #include <ctime>

The compile error is: 编译错误是:

error: aggregate 'special_time2()::timespec now' has incomplete type and cannot be defined
error: 'CLOCK_REALTIME' was not declared in this scope
error: 'clock_gettime' was not declared in this scope

Does anybody know why this is happen and know of a potential workaround? 有谁知道为什么会这样,并知道潜在的解决方法?

clock_gettime is not a standard C or C++ function, so there's no guarantee that it will be available on non-POSIX systems. clock_gettime不是标准的C或C ++函数,因此无法保证它在非POSIX系统上可用。

If you're writing modern C++, then you could use the std::chrono library for high-precision timing. 如果您正在编写现代C ++,那么您可以使用std::chrono库进行高精度计时。

If you're stuck with a pre-2011 library, then your only options are time() , which usually only has a precision of one second, or whatever platform-specific alternatives are available. 如果您坚持使用2011年之前的库,那么您唯一的选择是time() ,通常只有一秒的精度,或者任何特定于平台的替代品都可用。 Unfortunately, I don't know enough about MinGW to help you with that. 不幸的是,我不太了解MinGW对你的帮助。

UPDATE : I just tried the below with MinGW, and it didn't work. 更新 :我刚用MinGW尝试了下面的内容,但它没有用。 As the other answer says, POSIX functions will not necessarily be available under Windows. 正如另一个答案所说,POSIX功能不一定在Windows下可用。 This answer does not solve the OP's problem, but it might be useful for users of other systems. 这个答案并没有解决OP的问题,但它可能对其他系统的用户有用。

clock_gettime is defined by POSIX, not by the language standard. clock_gettime由POSIX定义,而不是由语言标准定义。

The man page on my system (Ubuntu) says: 我系统上的手册页(Ubuntu)说:

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

   clock_getres(), clock_gettime(), clock_settime():
          _POSIX_C_SOURCE >= 199309L

Try adding 尝试添加

#define _POSIX_C_SOURCE 199309L

before the #include <ctime> #include <ctime>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM