简体   繁体   English

无法编译 ms _tzSet() 示例

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

I'm using c++ builder 10.2 with the clang compiler on Windows 10 pro.我在 Windows 10 pro 上使用 c++ builder 10.2 和 clang 编译器。 Can anyone tell me why this doesn't compile?谁能告诉我为什么这不能编译?

// 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 );
}

I get 3 'Unresolved external' errors relating to _get_daylight, _get_timezone and _get_tzname.我收到 3 个与 _get_daylight、_get_timezone 和 _get_tzname 相关的“未解决的外部”错误。

Since I don't have "c++builder", I tried this with MinGW.因为我没有“c++builder”,所以我用 MinGW 尝试了这个。

With a straightforward compile and link command like this:使用如下简单的编译和链接命令:

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

I got the same errors:我得到了同样的错误:

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

A single grep revealed the library libucrtbase.a (among others) to contain the symbol _get_daylight .单个 grep 显示库libucrtbase.a (以及其他)包含符号_get_daylight Adding this library to the command:将此库添加到命令中:

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

This produced a runnable program.这产生了一个可运行的程序。

The other libraries are all libmsvcr*.a in different versions, I tried just one of them.其他库都是不同版本的libmsvcr*.a ,我只尝试了其中一个。 This was successful, too.这也很成功。


Edit:编辑:

With a not-so-current "clang" I didn't even need to add the library.使用不太流行的“clang”,我什至不需要添加库。

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

This compiled and linked without any error and runs perfectly.这编译和链接没有任何错误并且运行完美。

(clang version 7.0.1 (tags/RELEASE_701/final), Target: x86_64-pc-windows-msvc) (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