gpt4 book ai didi

c++ - 错误 : use of undeclared identifier 'ctime_s'

转载 作者:可可西里 更新时间:2023-11-01 17:56:46 24 4
gpt4 key购买 nike

当我尝试使用 ctime_s 编译 cpp 代码时,使用了未声明的标识符“ctime_s”。我该如何解决?

主要.cpp

#include <iostream>
#include <time.h>

int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
time_t a;
char tt[26];
time(&a);
ctime_s(tt,sizeof(tt),&a);
printf("%s",tt);
return 0;
}

结果

Machida-no-MacBook-Air:KnowledgeBase machidahiroaki$ gcc main.cpp --verbose
Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.10.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 242 -v -dwarf-column-info -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.1.0 -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/machidahiroaki/RubymineProjects/KnowledgeBase/KnowledgeBase -ferror-limit 19 -fmessage-length 160 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.10.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/c6/p5fy29197r167b46tsw36gb00000gn/T/main-50c81e.o -x c++ main.cpp
clang -cc1 version 6.1.0 based upon LLVM 3.6.0svn default target x86_64-apple-darwin14.1.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.1.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
main.cpp:18:2: error: use of undeclared identifier 'ctime_s'
ctime_s(tt,sizeof(tt),&a);
^
1 error generated.

最佳答案

  1. 在 C++ 中 ctime_s 尚未实现。
  2. 这是一个 C 函数,是 c11 的一部分,这意味着您必须将 -std=c11 添加到您的编译器标志中。

    C11 standard (ISO/IEC 9899:2011):

    7.27.3.2 The ctime function (p: 393)

    K.3.8.2.2 The ctime_s function (p: 626)

同样在您的代码中,您将 c++ 与 c 混合使用。

供引用:

A fourth version of the C standard, known as C11, was published in 2011 as ISO/IEC 9899:2011. GCC has substantially complete support for this standard, enabled with -std=c11 or -std=iso9899:2011. (While in development, drafts of this standard version were referred to as C1X.)

关于c++ - 错误 : use of undeclared identifier 'ctime_s' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29699548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com