c++ - 无法获取ms _tzSet()示例进行编译

标签 c++ c windows c++builder

我在Windows 10专业版上使用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(标签/ RELEASE_701 / final),目标:x86_64-pc-windows-msvc)

关于c++ - 无法获取ms _tzSet()示例进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62018202/

相关文章:

python - CherryPy 作为 Windows 服务 - 示例

c++ - 偏特化不使用它的任何模板参数

c - 测量代码的吞吐量和延迟

c - 使用 GNU C 正则表达式库的字符串正则表达式

c - 如何创建这个公式?

python - 使子进程在 Windows 上找到 git 可执行文件

linux - 从 Linux 切换到 Windows 时的字符编码

c++ - 错误: bool 值不能用 'explicit'说明符声明

c++ - 在信号处理程序中使用长数据。

c++ - "72"对 "gcc72-c++"意味着什么