我在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 (tagsRELEASE_701final), Target: x86_64-pc-windows-msvc)