错误:使用未声明的标识符“ctime_s”

9

当我尝试使用ctime_s编译cpp代码时,出现了“使用未声明的标识符'ctime_s'”的错误。我该如何修复它?

main.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.

4
ctime_s是C11的一部分,但并非所有地方都实现了它。尝试使用-std=c11进行编译;也许你会有好运。 - Wintermute
在C++中还没有ctime_s,它只在C中标准化了。 - Some programmer dude
1个回答

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

    C11标准(ISO / IEC 9899:2011):

    7.27.3.2 ctime函数(p:393)

    K.3.8.2.2 ctime_s函数(p:626)

另外,您的代码中混合使用了C++和C。

参考:

第四个版本的C标准,即C11,于2011年以ISO / IEC 9899:2011的形式发布。 GCC对此标准具有相当完整的支持,可以通过-std = c11或-std = iso9899:2011启用。(在开发过程中,此标准版本的草案被称为C1X。)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接