strerror_s、strcpy_s、localtime_s、sprintf_s 未在此范围内声明?

kus*_*nni 4 c++ linux gcc gnu g++

我编写了一个跨平台代码,给出了当前日期(mm/dd/yy)和时间(hh/mm/ss)和完整日期(yyyymmdd),此代码在Windows(MSVS2015)中工作,但在Linux(GCC)中不起作用4.8.5)。

\n\n

我的代码是

\n\n
#include <iostream> \n#ifdef WIN32\n#include <windows.h>\n#else\n#include <cerrno>\n#endif\n\n#ifdef WIN32\n#include <direct.h>\n#include <io.h>\n#else\n#include <dirent.h>\n#include <stdio.h>\n#include <unistd.h>\n#endif\n#include <ctime>\n#include <bitset>\n#include <cstdlib>  /*atol*/\n#include <iomanip>\n#include <stdexcept>\n#include <cstring>\n#include <cstdio>\n#include <fstream>\nusing namespace std;\n\ntemplate <size_t size>\nvoid GetNowDateTime(char(&c_date)[size], char(&c_time)[size])\n{\n   time_t t;\n   struct tm now;\n   strcpy_s(c_date, "00/00/00");\n   strcpy_s(c_time, "00:00:00");\n   time(&t);\n  if (localtime_s(&now, &t) != 0) return;\n char temp[3];\n sprintf_s(temp, "%.2d", now.tm_mon + 1);\n memcpy(c_date, temp, 2);\n sprintf_s(temp, "%.2d", now.tm_mday);\n memcpy(c_date + 3, temp, 2);\n sprintf_s(temp, "%.2d", now.tm_year - 100);\n memcpy(c_date + 6, temp, 2);\n sprintf_s(temp, "%.2d", now.tm_hour);\n memcpy(c_time, temp, 2);\n sprintf_s(temp, "%.2d", now.tm_min);\n memcpy(c_time + 3, temp, 2);\n sprintf_s(temp, "%.2d", now.tm_sec);\n memcpy(c_time + 6, temp, 2);\n }\n\n  int GetToday(void)\n  {\n    time_t t;\n    struct tm now;\n    time(&t);\n    if (localtime_s(&now, &t) != 0) return 0;\n    return (now.tm_year + 1900) * 10000 + (now.tm_mon + 1) * 100 + now.tm_mday;\n  }\n\n bool OpenOuputFile(ofstream& outputFile)\n {\n\n  char buf[1024];\n\n #ifdef WIN32\n   strcpy_s(buf, "C:\\\\Myfolder\\\\output.txt");\n  #else\n    strcpy_s(buf, "/home/myfolder/output.txt");\n    #endif\n    outputFile.open(buf, ios::out);\n\n    if (!outputFile)\n    {\n    char szErrorMsg[1024];\n    strerror_s(szErrorMsg, errno);\n    cout << "Unable to open input file. " << buf << " Error:" << szErrorMsg << endl;\n    return 0;\n}\nreturn 1;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

//这是我的主要功能

\n\n
int main()\n{\nchar today[9];\nchar time[9];\nofstream outputFile;\nGetNowDateTime(today, time);\nint yyyymmdd = GetToday();\nif (OpenOuputFile(outputFile))\n{\n    outputFile << "GetNowDateTime functions given is:-\\t" << today << "\\t" << time << endl;\n    outputFile << "GetToday Function given is:-\\t" << yyyymmdd << endl;\n   }\n   else\n    cout << "No output file written" << endl;\nreturn 0;\n} \n
Run Code Online (Sandbox Code Playgroud)\n\n

Linux 中的错误是

\n\n
TimeDate.cpp: In function \xe2\x80\x98void GetNowDateTime(char (&)[size], char (&)[size])\xe2\x80\x99:\n\nTimeDate.cpp:34:26: error: there are no arguments to \xe2\x80\x98localtime_s\xe2\x80\x99 that depend on a template parameter, so a declaration of \xe2\x80\x98localtime_s\xe2\x80\x99 must be available [-fpermissive]\n  if (localtime_s(&now, &t) != 0) return;\nTimeDate.cpp:36:40: error: there are no arguments to \xe2\x80\x98sprintf_s\xe2\x80\x99 that depend on a template parameter, so a declaration of \xe2\x80\x98sprintf_s\xe2\x80\x99 must be available [-fpermissive]\nimeDate.cpp: In function \xe2\x80\x98int GetToday()\xe2\x80\x99:\nTimeDate.cpp:55:26: error: \xe2\x80\x98localtime_s\xe2\x80\x99 was not declared in this scope\nif (localtime_s(&now, &t) != 0) return 0;\nTimeDate.cpp:74:31: error: \xe2\x80\x98strerror_s\xe2\x80\x99 was not declared in this scope\nstrerror_s(szErrorMsg, errno);\nTimeDate.cpp:31:29: error: \xe2\x80\x98strcpy_s\xe2\x80\x99 was not declared in this scope\nstrcpy_s(c_date, "00/00/00");\n
Run Code Online (Sandbox Code Playgroud)\n\n

该程序运行在(Windows)Visual Studio 2015和(Linux)GCC 4.8.5上。

\n\n

我包含了所有必需的标头,但在 Linux 中编译时显示错误。

\n\n

为什么在 Linux 中会出现上述错误,请告诉我。

\n

Ala*_*les 5

strcpy_s和朋友是 Microsoft 对 c 的扩展,它们在 C11 中标准化。GCC 4.8.5 不支持它们,较新的版本可能也不支持 C11 的安全字符串函数何时成为 glibc 的一部分?