繁体   English   中英

无法编译 ms _tzSet() 示例

[英]Can't get ms _tzSet() example to compile

我在 Windows 10 pro 上使用 c++ builder 10.2 和 clang 编译器。 谁能告诉我为什么这不能编译?

// crt_tzset.cpp
// This program uses _tzset to set the global variables
// named _daylight, _timezone, and _tzname. Since TZ is
// not being explicitly set, it uses the system time.

#include <time.h>
#include <stdlib.h>
#include <stdio.h>

int main( void )
{
    _tzset();
    int daylight;
    _get_daylight( &daylight );
    printf( "_daylight = %d\n", daylight );
    long timezone;
    _get_timezone( &timezone );
    printf( "_timezone = %ld\n", timezone );
    size_t s;
    char tzname[100];
    _get_tzname( &s, tzname, sizeof(tzname), 0 );
    printf( "_tzname[0] = %s\n", tzname );
    exit( 0 );
}

我收到 3 个与 _get_daylight、_get_timezone 和 _get_tzname 相关的“未解决的外部”错误。

因为我没有“c++builder”,所以我用 MinGW 尝试了这个。

使用如下简单的编译和链接命令:

gcc -Wall -Werror -pedantic -O2 tz.c -o tz

我得到了同样的错误:

C:\Users\###\AppData\Local\Temp\ccI8j8Mj.o:tz.c:(.text.startup+0x1f): undefined reference to `__imp__get_daylight'
C:\Users\###\AppData\Local\Temp\ccI8j8Mj.o:tz.c:(.text.startup+0x3a): undefined reference to `__imp__get_timezone'
C:\Users\###\AppData\Local\Temp\ccI8j8Mj.o:tz.c:(.text.startup+0x61): undefined reference to `__imp__get_tzname'
collect2.exe: error: ld returned 1 exit status

单个 grep 显示库libucrtbase.a (以及其他)包含符号_get_daylight 将此库添加到命令中:

gcc -Wall -Werror -pedantic -O2 tz.c -lucrtbase -o tz

这产生了一个可运行的程序。

其他库都是不同版本的libmsvcr*.a ,我只尝试了其中一个。 这也很成功。


编辑:

使用不太流行的“clang”,我什至不需要添加库。

clang -Wall -Werror -pedantic -O3 tz.c -o tz-clang.exe

这编译和链接没有任何错误并且运行完美。

(clang 版本 7.0.1 (tags/RELEASE_701/final),目标:x86_64-pc-windows-msvc)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM