linux strcpy 中文,当我使用模板时,未在此范围GCC Linux环境中声明strerror_s,strcpy_s,localtime_s,sprintf_s?...

在尝试编译一个在Windows上运行的跨平台代码时,在Linux环境下遇到了问题。代码包含获取当前日期和时间的功能,但在使用GCC 4.8.5编译时,出现了有关`localtime_s`、`sprintf_s`等函数未声明的错误。这可能是因为这些函数在Linux的C标准库中不存在,需要替换为POSIX兼容的函数。
摘要由CSDN通过智能技术生成

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

我的代码是

#include

#ifdef WIN32

#include

#else

#include

#endif

#ifdef WIN32

#include

#include

#else

#include

#include

#include

#endif

#include

#include

#include /*atol*/

#include

#include

#include

#include

#include

using namespace std;

template

void GetNowDateTime(char(&c_date)[size], char(&c_time)[size])

{

time_t t;

struct tm now;

strcpy_s(c_date, "00/00/00");

strcpy_s(c_time, "00:00:00");

time(&t);

if (localtime_s(&now, &t) != 0) return;

char temp[3];

sprintf_s(temp, "%.2d", now.tm_mon + 1);

memcpy(c_date, temp, 2);

sprintf_s(temp, "%.2d", now.tm_mday);

memcpy(c_date + 3, temp, 2);

sprintf_s(temp, "%.2d", now.tm_year - 100);

memcpy(c_date + 6, temp, 2);

sprintf_s(temp, "%.2d", now.tm_hour);

memcpy(c_time, temp, 2);

sprintf_s(temp, "%.2d", now.tm_min);

memcpy(c_time + 3, temp, 2);

sprintf_s(temp, "%.2d", now.tm_sec);

memcpy(c_time + 6, temp, 2);

}

int GetToday(void)

{

time_t t;

struct tm now;

time(&t);

if (localtime_s(&now, &t) != 0) return 0;

return (now.tm_year + 1900) * 10000 + (now.tm_mon + 1) * 100 + now.tm_mday;

}

bool OpenOuputFile(ofstream& outputFile)

{

char buf[1024];

#ifdef WIN32

strcpy_s(buf, "C:\Myfolder\output.txt");

#else

strcpy_s(buf, "/home/myfolder/output.txt");

#endif

outputFile.open(buf, ios::out);

if (!outputFile)

{

char szErrorMsg[1024];

strerror_s(szErrorMsg, errno);

cout << "Unable to open input file. " << buf << " Error:" << szErrorMsg << endl;

return 0;

}

return 1;

}

//这是我的主要功能

int main()

{

char today[9];

char time[9];

ofstream outputFile;

GetNowDateTime(today, time);

int yyyymmdd = GetToday();

if (OpenOuputFile(outputFile))

{

outputFile << "GetNowDateTime functions given is:-" << today << "" << time << endl;

outputFile << "GetToday Function given is:-" << yyyymmdd << endl;

}

else

cout << "No output file written" << endl;

return 0;

}

Linux中的错误是

TimeDate.cpp: In function ‘void GetNowDateTime(char (&)[size], char (&)[size])’:

TimeDate.cpp:34:26: error: there are no arguments to ‘localtime_s’ that depend on a template parameter, so a declaration of ‘localtime_s’ must be available [-fpermissive]

if (localtime_s(&now, &t) != 0) return;

TimeDate.cpp:36:40: error: there are no arguments to ‘sprintf_s’ that depend on a template parameter, so a declaration of ‘sprintf_s’ must be available [-fpermissive]

imeDate.cpp: In function ‘int GetToday()’:

TimeDate.cpp:55:26: error: ‘localtime_s’ was not declared in this scope

if (localtime_s(&now, &t) != 0) return 0;

TimeDate.cpp:74:31: error: ‘strerror_s’ was not declared in this scope

strerror_s(szErrorMsg, errno);

TimeDate.cpp:31:29: error: ‘strcpy_s’ was not declared in this scope

strcpy_s(c_date, "00/00/00");

该程序在(windows)visual studio 2015和(Linux)GCC 4.8.5上运行。

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值