【问题标题】:Using strptime under cygwin64 on Windows 8 in CodeBlocks在 CodeBlocks 中的 Windows 8 上使用 cygwin64 下的 strptime
【发布时间】:2014-09-02 20:41:37
【问题描述】:

我正在尝试在我的 Windows 机器上编译一些最初用 linux 编写的代码。我已经安装并设置了 Cygwin 以在 CodeBlocks 中使用,它主要工作。除了对 strptime 的调用之外,它以“错误:'strptime' 在此范围内没有被删除”来迎接我。我已经用谷歌搜索了一段时间无济于事,请有人解释一下可能是什么问题?我试过包括 time.h 但没有运气。

#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#endif

#include <ctime>
#include <cstring>

class Date {
private:
  struct tm _tm;
  string strform;

  static void set_zero(struct tm &_tm) {
    memset(&_tm, '\0', sizeof(struct tm));
    //    _tm.hour = 0;
  }

  time_t make_time() {
    return mktime(&_tm);
  }

public:
  Date() {
    time_t current = time(NULL);
    _tm = *gmtime(&current);
    char buffer[1024];
    strftime(buffer, 1024, "%b %e %Y", &_tm);
    strform = buffer;
  }

  Date(time_t current) {
    _tm = *gmtime(&current);
    char buffer[1024];
    strftime(buffer, 1024, "%b %e %Y", &_tm);
    strform = buffer;
  }

  Date(string _strform) {
    strform = _strform;
    set_zero(_tm);
    char *result = strptime(strform.c_str(), "%b %e %Y", &_tm);
    assert(result);
    char buffer[1024];
    strftime(buffer, 1024, "%b %e %Y", &_tm);
    strform = buffer;
    //cout << "strform = " << strform << endl;
    //    cout << "length = " << result - strform.c_str() << endl;
    //    cout << "day = " << _tm.tm_mday << endl;
    //    cout << "month = " << _tm.tm_mon << endl;
    //    cout << "year = " << _tm.tm_year << endl;
    //    char buffer[1024];
    //    strftime(buffer, 1024, "%b %e %Y   %H:%M:%S", &_tm);
    //    cout << "buffer = " << buffer << endl;
  }

  Date operator-(int days) {
    return Date(make_time() - days*86400 );
  }

  time_t operator-(Date &other) {
    return (make_time() - other.make_time())/86400.0;
  }

  bool operator<=(Date &other) {
    return make_time() <= other.make_time();
  }

  bool operator<(Date &other) {
    return make_time() < other.make_time();
  }

  bool operator>=(Date &other) {
    return make_time() >= other.make_time();
  }

  time_t to_seconds() {
    return make_time();
  }

  friend ostream &operator<< (ostream &out, const Date &d) {
    return out << d.strform;
  }

  static Date today() {
    return Date();
  }

  string to_string() const {
    return strform;
  }

  const char *to_cstring() const {
    return strform.c_str();
  }
};

【问题讨论】:

  • strptime 不是标准的 C 函数。在我的项目中,我使用它的公共领域实现。

标签: c++ c cygwin


【解决方案1】:

See this answer on XOPEN_SOURCE。通过将add_definitions(-D_XOPEN_SOURCE=700) 添加到我的 CMakeLists.txt 中,我有一些运气可以编译 strptime。通过在标头中打开功能,编译器可以找到 strptime。

【讨论】:

    【解决方案2】:

    此错误消息表示该函数未在找到时声明。看起来 strptime() 是在 Cygwin 的 time.h 文件中声明的(至少在 v1.7.25 中),所以只需添加:

     #include <time.h>
    

    到您的源文件。请注意,strptime() 不是标准 C 函数。

    【讨论】:

    • 感谢您的回复,我之前确实尝试过,但仍然收到相同的消息。
    • 我建议对包含文件进行 grepping,以确保您的系统上确实有 strptime()。类似grep -r strptime /usr/include/*
    【解决方案3】:

    这只发生在 C++11 及更高版本中。看起来 __STRICT_ANSI__ 正在设置中,而 strptime() 没有在其下定义。一种解决方法是取消定义__STRICT_ANSI__,如下所示。

    #ifdef __CYGWIN__
    #undef __STRICT_ANSI__
    #endif
    

    【讨论】:

      猜你喜欢
      • 2018-10-04
      • 1970-01-01
      • 2013-11-11
      • 2010-09-24
      • 2010-10-14
      • 1970-01-01
      • 2020-12-09
      相关资源
      最近更新 更多