我在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/